2020-08-22 18:35:07 +00:00
|
|
|
import { DuplicateContactException } from 'shared/helpers/CustomErrors';
|
2020-01-01 17:00:43 +00:00
|
|
|
import * as types from '../mutation-types';
|
|
|
|
import ContactAPI from '../../api/contacts';
|
2020-06-02 17:29:02 +00:00
|
|
|
import Vue from 'vue';
|
2020-01-01 17:00:43 +00:00
|
|
|
|
|
|
|
const state = {
|
2020-06-02 17:29:02 +00:00
|
|
|
records: {},
|
2020-01-01 17:00:43 +00:00
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
isFetchingItem: false,
|
|
|
|
isUpdating: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getContacts($state) {
|
2020-06-02 17:29:02 +00:00
|
|
|
return Object.values($state.records);
|
2020-01-01 17:00:43 +00:00
|
|
|
},
|
|
|
|
getUIFlags($state) {
|
|
|
|
return $state.uiFlags;
|
|
|
|
},
|
|
|
|
getContact: $state => id => {
|
2020-06-02 17:29:02 +00:00
|
|
|
const contact = $state.records[id];
|
|
|
|
return contact || {};
|
2020-01-01 17:00:43 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
get: async ({ commit }) => {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetching: true });
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.get();
|
|
|
|
commit(types.default.SET_CONTACTS, response.data.payload);
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetching: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
show: async ({ commit }, { id }) => {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetchingItem: true });
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.show(id);
|
|
|
|
commit(types.default.SET_CONTACT_ITEM, response.data.payload);
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetchingItem: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isFetchingItem: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
update: async ({ commit }, { id, ...updateObj }) => {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isUpdating: true });
|
|
|
|
try {
|
|
|
|
const response = await ContactAPI.update(id, updateObj);
|
|
|
|
commit(types.default.EDIT_CONTACT, response.data.payload);
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_CONTACT_UI_FLAG, { isUpdating: false });
|
2020-08-22 18:35:07 +00:00
|
|
|
if (error.response?.data?.contact) {
|
|
|
|
throw new DuplicateContactException(error.response.data.contact);
|
|
|
|
} else {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
2020-01-01 17:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-04 06:12:47 +00:00
|
|
|
|
|
|
|
updatePresence: ({ commit }, data) => {
|
|
|
|
commit(types.default.UPDATE_CONTACTS_PRESENCE, data);
|
|
|
|
},
|
2020-07-04 14:16:17 +00:00
|
|
|
|
|
|
|
setContact({ commit }, data) {
|
|
|
|
commit(types.default.SET_CONTACT_ITEM, data);
|
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
[types.default.SET_CONTACT_UI_FLAG]($state, data) {
|
|
|
|
$state.uiFlags = {
|
|
|
|
...$state.uiFlags,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
[types.default.SET_CONTACTS]: ($state, data) => {
|
|
|
|
data.forEach(contact => {
|
2020-06-25 15:34:03 +00:00
|
|
|
Vue.set($state.records, contact.id, {
|
|
|
|
...($state.records[contact.id] || {}),
|
|
|
|
...contact,
|
|
|
|
});
|
2020-06-02 17:29:02 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.SET_CONTACT_ITEM]: ($state, data) => {
|
2020-06-25 15:34:03 +00:00
|
|
|
Vue.set($state.records, data.id, {
|
|
|
|
...($state.records[data.id] || {}),
|
|
|
|
...data,
|
|
|
|
});
|
2020-06-02 17:29:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[types.default.EDIT_CONTACT]: ($state, data) => {
|
|
|
|
Vue.set($state.records, data.id, data);
|
|
|
|
},
|
2020-07-04 06:12:47 +00:00
|
|
|
|
|
|
|
[types.default.UPDATE_CONTACTS_PRESENCE]: ($state, data) => {
|
|
|
|
Object.values($state.records).forEach(element => {
|
|
|
|
const availabilityStatus = data[element.id];
|
|
|
|
if (availabilityStatus) {
|
|
|
|
Vue.set(
|
|
|
|
$state.records[element.id],
|
|
|
|
'availability_status',
|
|
|
|
availabilityStatus
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Vue.delete($state.records[element.id], 'availability_status');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2020-01-01 17:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|