mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
WIP
This commit is contained in:
108
client/src/store/accounts/accounts.actions.js
Normal file
108
client/src/store/accounts/accounts.actions.js
Normal file
@@ -0,0 +1,108 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const fetchAccountTypes = () => {
|
||||
return (dispatch, getState) => new Promise((resolve, reject) => {
|
||||
ApiService.get('account_types').then((response) => {
|
||||
dispatch({
|
||||
type: t.ACCOUNT_TYPES_LIST_SET,
|
||||
account_types: response.data.account_types,
|
||||
});
|
||||
resolve(response);
|
||||
}).catch((error) => { reject(error); });
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchAccountsList = ({ query } = {}) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.get('accounts', { params: query }).then((response) => {
|
||||
dispatch({
|
||||
type: t.ACCOUNTS_PAGE_SET,
|
||||
accounts: response.data.accounts,
|
||||
customViewId: response.data.customViewId,
|
||||
});
|
||||
dispatch({
|
||||
type: t.ACCOUNTS_ITEMS_SET,
|
||||
accounts: response.data.accounts,
|
||||
});
|
||||
resolve(response);
|
||||
}).catch((error) => { reject(error); });
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchAccountsDataTable = ({ query }) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.get('accounts', ).then((response) => {
|
||||
dispatch({
|
||||
type: t.ACCOUNTS_DATA_TABLE,
|
||||
data: response.data,
|
||||
})
|
||||
}).catch((error) => { reject(error); })
|
||||
})
|
||||
};
|
||||
|
||||
export const submitAccount = ({ form }) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.post('accounts', form).then((response) => {
|
||||
dispatch({ type: t.CLEAR_ACCOUNT_FORM_ERRORS });
|
||||
resolve(response);
|
||||
}).catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
const { errors } = data;
|
||||
|
||||
dispatch({ type: t.CLEAR_ACCOUNT_FORM_ERRORS });
|
||||
if (errors){
|
||||
dispatch({ type: t.ACCOUNT_FORM_ERRORS, errors });
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editAccount = ({ id, form }) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.post(`accounts/${id}`, form).then((response) => {
|
||||
dispatch({type: t.CLEAR_ACCOUNT_FORM_ERRORS});
|
||||
resolve(response);
|
||||
}).catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
const { errors } = data;
|
||||
|
||||
dispatch({type: t.CLEAR_ACCOUNT_FORM_ERRORS});
|
||||
if (errors){
|
||||
dispatch({ type: t.ACCOUNT_FORM_ERRORS, errors });
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const activeAccount = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`accounts/${id}/active`);
|
||||
};
|
||||
|
||||
export const inactiveAccount = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`accounts/${id}/inactive`);
|
||||
};
|
||||
|
||||
export const deleteAccount = ({ id }) => {
|
||||
return (dispatch) => ApiService.delete(`accounts/${id}`);
|
||||
};
|
||||
|
||||
export const deleteBulkAccounts = ({ ids }) => {
|
||||
|
||||
};
|
||||
|
||||
export const fetchAccount = ({ id }) => {
|
||||
return (dispatch) => new Promise((resolve, reject) => {
|
||||
ApiService.get(`accounts/${id}`).then((response) => {
|
||||
dispatch({
|
||||
type: t.ACCOUNT_SET,
|
||||
account: response.data.account,
|
||||
});
|
||||
resolve(response);
|
||||
}).catch((error) => { reject(error); });
|
||||
});
|
||||
}
|
||||
62
client/src/store/accounts/accounts.reducer.js
Normal file
62
client/src/store/accounts/accounts.reducer.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import t from 'store/types';
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
accountsTypes: [],
|
||||
accountsById: {},
|
||||
accountFormErrors: [],
|
||||
datatableQuery: {},
|
||||
currentViewId: -1,
|
||||
bulkActions: {},
|
||||
};
|
||||
|
||||
export default createReducer(initialState, {
|
||||
[t.ACCOUNTS_ITEMS_SET]: (state, action) => {
|
||||
const _items = {};
|
||||
|
||||
action.accounts.forEach((account) => {
|
||||
_items[account.id] = account;
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._items,
|
||||
};
|
||||
},
|
||||
|
||||
[t.ACCOUNTS_PAGE_SET]: (state, action) => {
|
||||
const viewId = action.customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
ids: action.accounts.map(i => i.id),
|
||||
};
|
||||
state.accounts = action.accounts;
|
||||
},
|
||||
|
||||
[t.ACCOUNT_TYPES_LIST_SET]: (state, action) => {
|
||||
state.accountsTypes = action.account_types;
|
||||
},
|
||||
|
||||
[t.ACCOUNT_SET]: (state, action) => {
|
||||
state.accountsById[action.account.id] = action.account;
|
||||
},
|
||||
|
||||
[t.ACCOUNT_BULK_ACTION_ADD]: (state, action) => {
|
||||
state.bulkActions[action.account_id] = true;
|
||||
},
|
||||
|
||||
[t.ACCOUNT_BULK_ACTION_REMOVE]: (state, action) => {
|
||||
delete state.bulkActions[action.account_id];
|
||||
},
|
||||
|
||||
[t.ACCOUNTS_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
}
|
||||
});
|
||||
|
||||
export const getAccountById = (state, id) => {
|
||||
return state.accounts.accountsById[id];
|
||||
};
|
||||
9
client/src/store/accounts/accounts.selectors.js
Normal file
9
client/src/store/accounts/accounts.selectors.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import {pickItemsFromIds} from 'store/selectors';
|
||||
|
||||
export const getAccountsItems = (state, viewId) => {
|
||||
const accountsView = state.accounts.views[(viewId || -1)];
|
||||
const accountsItems = state.accounts.items;
|
||||
|
||||
return (typeof accountsView === 'object')
|
||||
? (pickItemsFromIds(accountsItems, accountsView.ids) || []) : [];
|
||||
}
|
||||
15
client/src/store/accounts/accounts.types.js
Normal file
15
client/src/store/accounts/accounts.types.js
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
|
||||
export default {
|
||||
ACCOUNT_TYPES_LIST_SET: 'ACCOUNT_TYPES_LIST_SET',
|
||||
ACCOUNTS_PAGE_SET: 'ACCOUNTS_PAGE_SET',
|
||||
ACCOUNTS_ITEMS_SET: 'ACCOUNTS_ITEMS_SET',
|
||||
ACCOUNT_SET: 'ACCOUNT_SET',
|
||||
ACCOUNT_FORM_ERRORS: 'ACCOUNT_FORM_ERRORS',
|
||||
CLEAR_ACCOUNT_FORM_ERRORS: 'CLEAR_ACCOUNT_FORM_ERRORS',
|
||||
|
||||
ACCOUNT_BULK_ACTION_ADD: 'ACCOUNT_BULK_ACTION_ADD',
|
||||
ACCOUNT_BULK_ACTION_REMOVE: 'ACCOUNT_BULK_ACTION_REMOVE',
|
||||
|
||||
ACCOUNTS_SET_CURRENT_VIEW: 'ACCOUNTS_SET_CURRENT_VIEW',
|
||||
};
|
||||
Reference in New Issue
Block a user