2020-04-06 16:47:07 +00:00
|
|
|
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
|
|
|
|
import * as types from '../mutation-types';
|
|
|
|
import AccountAPI from '../../api/account';
|
2022-07-19 13:34:17 +00:00
|
|
|
import EnterpriseAccountAPI from '../../api/enterprise/account';
|
|
|
|
import { throwErrorMessage } from '../utils/api';
|
2020-04-06 16:47:07 +00:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
records: [],
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: false,
|
|
|
|
isFetchingItem: false,
|
|
|
|
isUpdating: false,
|
2022-07-19 13:34:17 +00:00
|
|
|
isCheckoutInProcess: false,
|
2020-04-06 16:47:07 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
getAccount: $state => id => {
|
|
|
|
return $state.records.find(record => record.id === Number(id));
|
|
|
|
},
|
|
|
|
getUIFlags($state) {
|
|
|
|
return $state.uiFlags;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
get: async ({ commit }) => {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isFetchingItem: true });
|
|
|
|
try {
|
|
|
|
const response = await AccountAPI.get();
|
|
|
|
commit(types.default.ADD_ACCOUNT, response.data);
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, {
|
|
|
|
isFetchingItem: false,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, {
|
|
|
|
isFetchingItem: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: async ({ commit }, updateObj) => {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isUpdating: true });
|
|
|
|
try {
|
|
|
|
await AccountAPI.update('', updateObj);
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isUpdating: false });
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isUpdating: false });
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2020-07-26 07:24:50 +00:00
|
|
|
create: async ({ commit }, accountInfo) => {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: true });
|
|
|
|
try {
|
|
|
|
const response = await AccountAPI.createAccount(accountInfo);
|
|
|
|
const account_id = response.data.data.account_id;
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false });
|
|
|
|
return account_id;
|
|
|
|
} catch (error) {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCreating: false });
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
},
|
2022-07-19 13:34:17 +00:00
|
|
|
|
|
|
|
checkout: async ({ commit }) => {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCheckoutInProcess: true });
|
|
|
|
try {
|
|
|
|
const response = await EnterpriseAccountAPI.checkout();
|
|
|
|
window.location = response.data.redirect_url;
|
|
|
|
} catch (error) {
|
|
|
|
throwErrorMessage(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCheckoutInProcess: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
subscription: async ({ commit }) => {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCheckoutInProcess: true });
|
|
|
|
try {
|
|
|
|
await EnterpriseAccountAPI.subscription();
|
|
|
|
} catch (error) {
|
|
|
|
throwErrorMessage(error);
|
|
|
|
} finally {
|
|
|
|
commit(types.default.SET_ACCOUNT_UI_FLAG, { isCheckoutInProcess: false });
|
|
|
|
}
|
|
|
|
},
|
2020-04-06 16:47:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
[types.default.SET_ACCOUNT_UI_FLAG]($state, data) {
|
|
|
|
$state.uiFlags = {
|
|
|
|
...$state.uiFlags,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
2020-05-06 08:08:36 +00:00
|
|
|
[types.default.ADD_ACCOUNT]: MutationHelpers.setSingleRecord,
|
2020-04-06 16:47:07 +00:00
|
|
|
[types.default.EDIT_ACCOUNT]: MutationHelpers.update,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|