2021-10-25 13:05:58 +00:00
|
|
|
import types from '../mutation-types';
|
2021-06-14 04:19:17 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import ContactNotesAPI from '../../api/contactNotes';
|
|
|
|
|
|
|
|
export const state = {
|
|
|
|
records: {},
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
isCreating: false,
|
|
|
|
isDeleting: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getAllNotesByContact: _state => contactId => {
|
2021-10-25 13:05:58 +00:00
|
|
|
const records = _state.records[contactId] || [];
|
|
|
|
return records.sort((r1, r2) => r2.id - r1.id);
|
2021-06-14 04:19:17 +00:00
|
|
|
},
|
|
|
|
getUIFlags(_state) {
|
|
|
|
return _state.uiFlags;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
async get({ commit }, { contactId }) {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isFetching: true });
|
2021-06-14 04:19:17 +00:00
|
|
|
try {
|
|
|
|
const { data } = await ContactNotesAPI.get(contactId);
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES, { contactId, data });
|
2021-06-14 04:19:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isFetching: false });
|
2021-06-14 04:19:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async create({ commit }, { contactId, content }) {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isCreating: true });
|
2021-06-14 04:19:17 +00:00
|
|
|
try {
|
2021-10-25 13:05:58 +00:00
|
|
|
const { data } = await ContactNotesAPI.create(contactId, content);
|
|
|
|
commit(types.ADD_CONTACT_NOTE, { contactId, data });
|
2021-06-14 04:19:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isCreating: false });
|
2021-06-14 04:19:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async delete({ commit }, { noteId, contactId }) {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isDeleting: true });
|
2021-06-14 04:19:17 +00:00
|
|
|
try {
|
|
|
|
await ContactNotesAPI.delete(contactId, noteId);
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.DELETE_CONTACT_NOTE, { contactId, noteId });
|
2021-06-14 04:19:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
} finally {
|
2021-10-25 13:05:58 +00:00
|
|
|
commit(types.SET_CONTACT_NOTES_UI_FLAG, { isDeleting: false });
|
2021-06-14 04:19:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
2021-10-25 13:05:58 +00:00
|
|
|
[types.SET_CONTACT_NOTES_UI_FLAG](_state, data) {
|
2021-06-14 04:19:17 +00:00
|
|
|
_state.uiFlags = {
|
|
|
|
..._state.uiFlags,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-10-25 13:05:58 +00:00
|
|
|
[types.SET_CONTACT_NOTES]($state, { data, contactId }) {
|
2021-06-14 04:19:17 +00:00
|
|
|
Vue.set($state.records, contactId, data);
|
|
|
|
},
|
2021-10-25 13:05:58 +00:00
|
|
|
[types.ADD_CONTACT_NOTE]($state, { data, contactId }) {
|
2021-06-14 04:19:17 +00:00
|
|
|
const contactNotes = $state.records[contactId] || [];
|
|
|
|
$state.records[contactId] = [...contactNotes, data];
|
|
|
|
},
|
2021-10-25 13:05:58 +00:00
|
|
|
[types.DELETE_CONTACT_NOTE]($state, { noteId, contactId }) {
|
2021-06-14 04:19:17 +00:00
|
|
|
const contactNotes = $state.records[contactId];
|
|
|
|
const withoutDeletedNote = contactNotes.filter(note => note.id !== noteId);
|
|
|
|
$state.records[contactId] = [...withoutDeletedNote];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|