import CaptainSkills from 'dashboard/api/captain/skills'; import { createStore } from '../storeFactory'; const withFlag = (mutations, flag, request) => async (context, payload) => { context.commit(mutations.SET_UI_FLAG, { [flag]: true }); try { return await request(context, payload); } finally { context.commit(mutations.SET_UI_FLAG, { [flag]: false }); } }; let latestListRequest = 0; export default createStore({ name: 'CaptainSkill', API: CaptainSkills, actions: mutations => ({ get: async ({ commit }, params) => { const requestId = ++latestListRequest; commit(mutations.SET_UI_FLAG, { fetchingList: true }); try { const { data } = await CaptainSkills.get(params); if (requestId === latestListRequest) { commit(mutations.SET, data.payload); commit(mutations.SET_META, data.meta); } return data.payload; } finally { if (requestId === latestListRequest) { commit(mutations.SET_UI_FLAG, { fetchingList: false }); } } }, detail: withFlag(mutations, 'fetchingItem', async (_, id) => { return (await CaptainSkills.show(id)).data; }), create: withFlag( mutations, 'creatingItem', async ({ commit }, payload) => { const { data } = await CaptainSkills.create(payload); commit(mutations.UPSERT, data); return data; } ), update: withFlag( mutations, 'updatingItem', async ({ commit }, { id, ...payload }) => { const { data } = await CaptainSkills.update(id, payload); commit(mutations.UPSERT, data); return data; } ), delete: withFlag(mutations, 'deletingItem', async ({ commit }, id) => { await CaptainSkills.delete(id); commit(mutations.DELETE, id); return id; }), bind: withFlag(mutations, 'updatingItem', (_, payload) => CaptainSkills.bind(payload) ), unbind: withFlag(mutations, 'updatingItem', (_, payload) => CaptainSkills.unbind(payload) ), }), });