Chatwoot/app/javascript/dashboard/store/modules/contactLabels.js
Sivin Varghese d21c1c773b
feat: Ability to add label for contact page (#2350)
* feat: Ability to add label for contact page

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
Co-authored-by: Nithin David Thomas <webofnithin@gmail.com>
2021-06-14 10:36:00 +05:30

89 lines
2 KiB
JavaScript

import Vue from 'vue';
import types from '../mutation-types';
import ContactAPI from '../../api/contacts';
const state = {
records: {},
uiFlags: {
isFetching: false,
isUpdating: false,
isError: false,
},
};
export const getters = {
getUIFlags($state) {
return $state.uiFlags;
},
getContactLabels: $state => id => {
return $state.records[Number(id)] || [];
},
};
export const actions = {
get: async ({ commit }, contactId) => {
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isFetching: true,
});
try {
const response = await ContactAPI.getContactLabels(contactId);
commit(types.SET_CONTACT_LABELS, {
id: contactId,
data: response.data.payload,
});
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isFetching: false,
});
} catch (error) {
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isFetching: false,
});
}
},
update: async ({ commit }, { contactId, labels }) => {
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isUpdating: true,
});
try {
const response = await ContactAPI.updateContactLabels(contactId, labels);
commit(types.SET_CONTACT_LABELS, {
id: contactId,
data: response.data.payload,
});
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isUpdating: false,
isError: false,
});
} catch (error) {
commit(types.SET_CONTACT_LABELS_UI_FLAG, {
isUpdating: false,
isError: true,
});
throw new Error(error);
}
},
setContactLabel({ commit }, { id, data }) {
commit(types.SET_CONTACT_LABELS, { id, data });
},
};
export const mutations = {
[types.SET_CONTACT_LABELS_UI_FLAG]($state, data) {
$state.uiFlags = {
...$state.uiFlags,
...data,
};
},
[types.SET_CONTACT_LABELS]: ($state, { id, data }) => {
Vue.set($state.records, id, data);
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};