2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* eslint-env browser */
|
|
|
|
/* eslint no-param-reassign: 0 */
|
|
|
|
|
|
|
|
// import * as types from '../mutation-types';
|
|
|
|
import defaultState from '../../i18n/default-sidebar';
|
|
|
|
import * as types from '../mutation-types';
|
|
|
|
import Account from '../../api/account';
|
|
|
|
import ChannelApi from '../../api/channels';
|
2019-08-25 14:29:28 +00:00
|
|
|
import { frontendURL } from '../../helper/URLHelper';
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
const state = defaultState;
|
|
|
|
// inboxes fetch flag
|
|
|
|
state.inboxesLoading = false;
|
|
|
|
|
|
|
|
const getters = {
|
|
|
|
getMenuItems(_state) {
|
|
|
|
return _state.menuGroup;
|
|
|
|
},
|
|
|
|
getInboxesList(_state) {
|
|
|
|
return _state.menuGroup.common.menuItems.inbox.children;
|
|
|
|
},
|
|
|
|
getInboxLoadingStatus(_state) {
|
|
|
|
return _state.inboxesLoading;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
// Fetch Labels
|
|
|
|
fetchLabels({ commit }) {
|
2019-08-21 07:29:56 +00:00
|
|
|
Account.getLabels()
|
|
|
|
.then(response => {
|
|
|
|
commit(types.default.SET_LABELS, response.data);
|
|
|
|
})
|
|
|
|
.catch();
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
// Fetch Inboxes
|
|
|
|
fetchInboxes({ commit }) {
|
|
|
|
commit(types.default.INBOXES_LOADING, true);
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
Account.getInboxes()
|
|
|
|
.then(response => {
|
|
|
|
commit(types.default.INBOXES_LOADING, false);
|
|
|
|
commit(types.default.SET_INBOXES, response.data);
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
commit(types.default.INBOXES_LOADING, false);
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteInbox({ commit }, id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Account.deleteInbox(id)
|
2019-08-21 07:29:56 +00:00
|
|
|
.then(response => {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (response.status === 200) {
|
|
|
|
commit(types.default.DELETE_INBOX, id);
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
})
|
2019-08-21 07:29:56 +00:00
|
|
|
.catch(error => {
|
2019-08-14 09:48:44 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
addInboxItem({ commit }, { channel, params }) {
|
2019-08-21 07:29:56 +00:00
|
|
|
const donePromise = new Promise(resolve => {
|
|
|
|
ChannelApi.createChannel(channel, params)
|
|
|
|
.then(response => {
|
|
|
|
commit(types.default.SET_INBOX_ITEM, response);
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
return donePromise;
|
|
|
|
},
|
2019-08-21 07:29:56 +00:00
|
|
|
listInboxAgents(_, { inboxId }) {
|
2019-08-14 09:48:44 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Account.listInboxAgents(inboxId)
|
2019-08-21 07:29:56 +00:00
|
|
|
.then(response => {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (response.status === 200) {
|
|
|
|
resolve(response.data);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
})
|
2019-08-21 07:29:56 +00:00
|
|
|
.catch(error => {
|
2019-08-14 09:48:44 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-08-21 07:29:56 +00:00
|
|
|
updateInboxAgents(_, { inboxId, agentList }) {
|
2019-08-14 09:48:44 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Account.updateInboxAgents(inboxId, agentList)
|
2019-08-21 07:29:56 +00:00
|
|
|
.then(response => {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (response.status === 200) {
|
|
|
|
resolve(response.data);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
})
|
2019-08-21 07:29:56 +00:00
|
|
|
.catch(error => {
|
2019-08-14 09:48:44 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutations = {
|
|
|
|
// Set Labels
|
|
|
|
[types.default.SET_LABELS](_state, data) {
|
|
|
|
let payload = data.data.payload.labels;
|
|
|
|
payload = payload.map(item => ({
|
|
|
|
label: item,
|
|
|
|
toState: `/#/${item}`,
|
|
|
|
}));
|
|
|
|
// Identify menuItem to update
|
|
|
|
// May have more than one object to update
|
|
|
|
// Iterate it accordingly. Updating commmon sidebar now.
|
2019-08-21 07:29:56 +00:00
|
|
|
const { menuItems } = _state.menuGroup.common;
|
2019-08-14 09:48:44 +00:00
|
|
|
// Update children for key `label`
|
|
|
|
menuItems.labels.children = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.INBOXES_LOADING](_state, flag) {
|
|
|
|
_state.inboxesLoading = flag;
|
|
|
|
},
|
|
|
|
// Set Inboxes
|
|
|
|
[types.default.SET_INBOXES](_state, data) {
|
2019-08-21 07:29:56 +00:00
|
|
|
let { payload } = data.data;
|
2019-08-14 09:48:44 +00:00
|
|
|
payload = payload.map(item => ({
|
|
|
|
channel_id: item.id,
|
|
|
|
label: item.name,
|
2019-08-25 14:29:28 +00:00
|
|
|
toState: frontendURL(`inbox/${item.id}`),
|
2019-08-14 09:48:44 +00:00
|
|
|
channelType: item.channelType,
|
|
|
|
avatarUrl: item.avatar_url,
|
|
|
|
pageId: item.page_id,
|
|
|
|
}));
|
|
|
|
// Identify menuItem to update
|
|
|
|
// May have more than one object to update
|
|
|
|
// Iterate it accordingly. Updating commmon sidebar now.
|
2019-08-21 07:29:56 +00:00
|
|
|
const { menuItems } = _state.menuGroup.common;
|
2019-08-14 09:48:44 +00:00
|
|
|
// Update children for key `inbox`
|
|
|
|
menuItems.inbox.children = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_INBOX_ITEM](_state, { data }) {
|
2019-08-21 07:29:56 +00:00
|
|
|
const { menuItems } = _state.menuGroup.common;
|
2019-08-14 09:48:44 +00:00
|
|
|
// Update children for key `inbox`
|
|
|
|
menuItems.inbox.children.push({
|
|
|
|
channel_id: data.id,
|
|
|
|
label: data.name,
|
2019-08-25 14:29:28 +00:00
|
|
|
toState: frontendURL(`inbox/${data.id}`),
|
2019-08-14 09:48:44 +00:00
|
|
|
channelType: data.channelType,
|
|
|
|
avatarUrl: data.avatar_url === undefined ? null : data.avatar_url,
|
|
|
|
pageId: data.page_id,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.DELETE_INBOX](_state, id) {
|
2019-08-21 07:29:56 +00:00
|
|
|
const { menuItems } = _state.menuGroup.common;
|
2019-08-14 09:48:44 +00:00
|
|
|
let inboxList = menuItems.inbox.children;
|
|
|
|
inboxList = inboxList.filter(inbox => inbox.channel_id !== id);
|
|
|
|
menuItems.inbox.children = inboxList;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|