2020-10-31 20:30:02 +00:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
|
|
|
|
import { StateInterface } from 'src/store';
|
|
|
|
import { axios } from 'src/boot/axios';
|
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
interface BalanceResponse {
|
|
|
|
balance: number;
|
|
|
|
credit: number;
|
|
|
|
debit: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BalanceInterface extends BalanceResponse {
|
|
|
|
limit: number;
|
2020-11-15 13:18:28 +00:00
|
|
|
loading: number;
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const state: BalanceInterface = {
|
|
|
|
balance: 0,
|
|
|
|
credit: 0,
|
|
|
|
debit: 0,
|
2020-11-15 13:18:28 +00:00
|
|
|
limit: 0,
|
|
|
|
loading: 0
|
2020-10-31 20:30:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutations: MutationTree<BalanceInterface> = {
|
|
|
|
setBalance(state, data: number) {
|
|
|
|
state.balance = data;
|
|
|
|
},
|
|
|
|
setCredit(state, data: number) {
|
|
|
|
state.credit = data;
|
|
|
|
},
|
|
|
|
setDebit(state, data: number) {
|
|
|
|
state.debit = data;
|
|
|
|
},
|
|
|
|
setLimit(state, data: number) {
|
|
|
|
state.limit = data;
|
2020-11-15 13:18:28 +00:00
|
|
|
},
|
|
|
|
setLoading(state, data = true) {
|
|
|
|
if (data) state.loading += 1;
|
|
|
|
else state.loading -= 1;
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<BalanceInterface, StateInterface> = {
|
|
|
|
getBalance({ commit, rootState }) {
|
2020-11-15 13:18:28 +00:00
|
|
|
commit('setLoading');
|
2020-10-31 20:30:02 +00:00
|
|
|
axios
|
2020-11-04 22:53:10 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
|
|
|
|
.get(`/users/${rootState.user.currentUser?.userid}/balance`)
|
2020-10-31 20:30:02 +00:00
|
|
|
.then(({ data }: AxiosResponse<BalanceResponse>) => {
|
|
|
|
commit('setBalance', data.balance);
|
|
|
|
commit('setCredit', data.credit);
|
|
|
|
commit('setDebit', data.debit);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.warn(err);
|
2020-11-15 13:18:28 +00:00
|
|
|
})
|
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2020-11-15 13:18:28 +00:00
|
|
|
getLimit({ rootState, commit }) {
|
|
|
|
commit('setLoading');
|
2020-10-31 20:30:02 +00:00
|
|
|
axios
|
2020-11-04 22:53:10 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
|
|
|
|
.get(`/users/${rootState.user.currentUser?.userid}/balance/limit`)
|
2020-10-31 20:30:02 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
console.log(data);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.warn(err);
|
2020-11-15 13:18:28 +00:00
|
|
|
})
|
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
},
|
2020-11-15 13:18:28 +00:00
|
|
|
changeBalance({ rootState, dispatch, commit }, amount: number) {
|
|
|
|
commit('setLoading');
|
2020-10-31 20:30:02 +00:00
|
|
|
axios
|
2020-11-04 22:53:10 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
|
|
|
|
.put(`/users/${rootState.user.currentUser?.userid}/balance`, <
|
|
|
|
{ amount: number }
|
|
|
|
>{
|
2020-10-31 20:30:02 +00:00
|
|
|
amount: amount
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
dispatch('getBalance').catch(err => console.warn(err));
|
|
|
|
})
|
2020-11-15 13:18:28 +00:00
|
|
|
.catch(err => console.warn(err))
|
|
|
|
.finally(() => commit('setLoading', false));
|
2020-10-31 20:30:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getters: GetterTree<BalanceInterface, StateInterface> = {};
|
|
|
|
|
|
|
|
const balance: Module<BalanceInterface, StateInterface> = {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
getters
|
|
|
|
};
|
|
|
|
|
|
|
|
export default balance;
|