import Vue from 'vue'; import * as types from '../mutation-types'; import ConversationAPI from '../../api/conversations'; const state = { records: {}, uiFlags: { isFetching: false, isUpdating: false, isError: false, }, }; export const getters = { getUIFlags($state) { return $state.uiFlags; }, getConversationLabels: $state => id => { return $state.records[Number(id)] || []; }, }; export const actions = { get: async ({ commit }, conversationId) => { commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isFetching: true, }); try { const response = await ConversationAPI.getLabels(conversationId); commit(types.default.SET_CONVERSATION_LABELS, { id: conversationId, data: response.data.payload, }); commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isFetching: false, }); } catch (error) { commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isFetching: false, }); } }, update: async ({ commit }, { conversationId, labels }) => { commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isUpdating: true, }); try { const response = await ConversationAPI.updateLabels( conversationId, labels ); commit(types.default.SET_CONVERSATION_LABELS, { id: conversationId, data: response.data.payload, }); commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isUpdating: false, isError: false, }); } catch (error) { commit(types.default.SET_CONVERSATION_LABELS_UI_FLAG, { isUpdating: false, isError: true, }); } }, }; export const mutations = { [types.default.SET_CONVERSATION_LABELS_UI_FLAG]($state, data) { $state.uiFlags = { ...$state.uiFlags, ...data, }; }, [types.default.SET_CONVERSATION_LABELS]: ($state, { id, data }) => { Vue.set($state.records, id, data); }, }; export default { namespaced: true, state, getters, actions, mutations, };