feat: Accounts datatable.

This commit is contained in:
Ahmed Bouhuolia
2020-03-30 00:13:45 +02:00
parent d695188d3a
commit d9e10fd6b4
19 changed files with 543 additions and 294 deletions

View File

@@ -12,9 +12,7 @@ export const fetchAccountTypes = () => {
});
resolve(response);
})
.catch(error => {
reject(error);
});
.catch(error => { reject(error); });
});
};
@@ -40,6 +38,38 @@ export const fetchAccountsList = ({ query } = {}) => {
});
};
export const fetchAccountsTable = ({ query } = {}) => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
const pageQuery = getState().accounts.tableQuery;
dispatch({
type: t.ACCOUNTS_TABLE_LOADING,
loading: true,
});
ApiService.get('accounts', { params: { ...pageQuery, ...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
});
dispatch({
type: t.ACCOUNTS_TABLE_LOADING,
loading: false,
});
resolve(response);
})
.catch(error => {
reject(error);
});
});
};
export const fetchAccountsDataTable = ({ query }) => {
return dispatch =>
new Promise((resolve, reject) => {
@@ -109,7 +139,12 @@ export const inactiveAccount = ({ id }) => {
};
export const deleteAccount = ({ id }) => {
return dispatch => ApiService.delete(`accounts/${id}`);
return dispatch => new Promise((resolve, reject) => {
ApiService.delete(`accounts/${id}`).then((response) => {
dispatch({ type: t.ACCOUNT_DELETE, id });
resolve(response);
}).catch(error => { reject(error); });
});
};
export const deleteBulkAccounts = ({ ids }) => {};

View File

@@ -1,5 +1,6 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
import { createReducer, combineReducers } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
const initialState = {
items: {},
@@ -9,10 +10,11 @@ const initialState = {
accountFormErrors: [],
datatableQuery: {},
currentViewId: -1,
bulkActions: {},
selectedRows: [],
loading: false,
};
export default createReducer(initialState, {
const accountsReducer = createReducer(initialState, {
[t.ACCOUNTS_ITEMS_SET]: (state, action) => {
const _items = {};
@@ -44,19 +46,32 @@ export default createReducer(initialState, {
state.accountsById[action.account.id] = action.account;
},
[t.ACCOUNT_BULK_ACTION_ADD]: (state, action) => {
state.bulkActions[action.account_id] = true;
[t.ACCOUNT_DELETE]: (state, action) => {
if (typeof state.items[action.id] !== 'undefined'){
delete state.items[action.id];
}
},
[t.ACCOUNT_BULK_ACTION_REMOVE]: (state, action) => {
delete state.bulkActions[action.account_id];
[t.ACCOUNTS_SELECTED_ROWS_SET]: (state, action) => {
state.selectedRows.push(...action.ids);
},
[t.ACCOUNTS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
}
},
[t.ACCOUNTS_TABLE_LOADING]: (state, action) => {
state.loading = action.loading;
},
});
export default createTableQueryReducers('accounts', accountsReducer);
export const getAccountById = (state, id) => {
return state.accounts.accountsById[id];
};
};
// export default {
// // ...accountsReducer,
// // testReducer,
// }

View File

@@ -5,11 +5,16 @@ export default {
ACCOUNTS_PAGE_SET: 'ACCOUNTS_PAGE_SET',
ACCOUNTS_ITEMS_SET: 'ACCOUNTS_ITEMS_SET',
ACCOUNT_SET: 'ACCOUNT_SET',
ACCOUNT_DELETE: 'ACCOUNT_DELETE',
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_SELECTED_ROWS_SET: 'ACCOUNTS_SELECTED_ROWS_SET',
ACCOUNTS_SET_CURRENT_VIEW: 'ACCOUNTS_SET_CURRENT_VIEW',
ACCOUNTS_TABLE_QUERY_SET: 'ACCOUNTS_TABLE_QUERY_SET',
ACCOUNTS_TABLE_QUERIES_SET: 'ACCOUNTS_TABLE_QUERIES_SET',
ACCOUNTS_TABLE_LOADING: 'ACCOUNTS_TABLE_LOADING',
};

View File

@@ -0,0 +1,28 @@
export const createTableQueryReducers =
(resourceName = '', reducer) =>
(state, action) => {
const RESOURCE_NAME = resourceName.toUpperCase();
switch (action.type) {
case `${RESOURCE_NAME}_TABLE_QUERY_SET`:
return {
...state,
tableQuery: {
...state.tableQuery,
[state.key]: state.value,
}
};
case `${RESOURCE_NAME}_TABLE_QUERIES_ADD`:
return {
...state,
tableQuery: {
...state.tableQuery,
...action.queries
},
};
default:
return reducer(state, action);
}
}