refactoring: expenses landing list.

refactoring: customers landing list.
refactoring: vendors landing list.
refactoring: manual journals landing list.
This commit is contained in:
a.bouhuolia
2021-02-10 18:35:19 +02:00
parent 6e10ed0721
commit c68b4ca9ba
170 changed files with 2835 additions and 4430 deletions

View File

@@ -1,262 +1,10 @@
import { batch } from 'react-redux'
import { omit } from 'lodash';
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 setAccountsTableState = (queries) => {
return {
type: t.ACCOUNTS_TABLE_STATE_SET,
payload: { queries },
};
};
export const fetchAccountsList = () => {
return (dispatch) =>
new Promise((resolve, reject) => {
const query = { column_sort_by: 'name', sort_order: 'asc' };
ApiService.get('accounts', { params: query })
.then((response) => {
batch(() => {
dispatch({
type: t.ACCOUNTS_ITEMS_SET,
accounts: response.data.accounts,
});
dispatch({
type: t.ACCOUNTS_LIST_SET,
payload: {
accounts: response.data.accounts,
}
});
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const fetchAccountsTable = ({ query } = {}) => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
let pageQuery = getState().accounts.tableQuery;
if (pageQuery.filter_roles) {
pageQuery = {
...omit(pageQuery, ['filter_roles']),
stringified_filter_roles:
JSON.stringify(pageQuery.filter_roles) || '',
};
}
dispatch({
type: t.ACCOUNTS_TABLE_LOADING,
loading: true,
});
ApiService.get('accounts', { params: { ...pageQuery, ...query } })
.then((response) => {
batch(() => {
dispatch({
type: t.ACCOUNTS_PAGE_SET,
accounts: response.data.accounts,
customViewId: response.data?.filter_meta?.view?.custom_view_id,
});
dispatch({
type: t.ACCOUNTS_ITEMS_SET,
accounts: response.data.accounts,
});
dispatch({
type: t.ACCOUNTS_SET_CURRENT_VIEW,
currentViewId: parseInt(response.data?.filter_meta?.view?.custom_view_id, 10),
});
dispatch({
type: t.ACCOUNTS_TABLE_LOADING,
loading: false,
});
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.ACCOUNT_ERRORS_CLEAR,
});
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
dispatch({
type: t.ACCOUNT_ERRORS_CLEAR,
});
if (error) {
dispatch({
type: t.ACCOUNT_ERRORS_SET,
payload: { error },
});
}
reject(data?.errors);
});
});
};
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;
dispatch({ type: t.CLEAR_ACCOUNT_FORM_ERRORS });
if (error) {
dispatch({ type: t.ACCOUNT_FORM_ERRORS, error });
}
reject(data?.errors);
});
});
};
export const activateAccount = ({ id }) => {
return (dispatch) => ApiService.post(`accounts/${id}/activate`);
};
export const inactiveAccount = ({ id }) => {
return (dispatch) => ApiService.post(`accounts/${id}/inactivate`);
};
export const bulkActivateAccounts = ({ ids }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`accounts/bulk/activate`, null, { params: { ids } })
.then((response) => {
dispatch({
type: t.BULK_ACTIVATE_ACCOUNTS,
payload: { ids },
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const bulkInactiveAccounts = ({ ids }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`accounts/bulk/inactivate`, null, { params: { ids } })
.then((response) => {
dispatch({
type: t.BULK_INACTIVATE_ACCOUNTS,
payload: { ids },
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const deleteAccount = ({ 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.response.data.errors || []);
});
});
};
export const deleteBulkAccounts = ({ ids }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.delete(`accounts`, { params: { ids } })
.then((response) => {
dispatch({
type: t.ACCOUNTS_BULK_DELETE,
payload: { ids },
});
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
// const { errors } = data;
reject(data?.errors);
});
});
};
export const fetchAccount = ({ id }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.get(`accounts/${id}`)
.then((response) => {
dispatch({
type: t.ACCOUNT_SET,
payload: {
account: response.data.account,
}
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const setBulkAction = ({ action }) => {
return (dispatch) => dispatch({
type: t.ACCOUNTS_BULK_ACTION,
payload: { action }
});
}
export const setSelectedRowsItems = () => {};

View File

@@ -1,119 +1,14 @@
import t from 'store/types';
import { createReducer} from '@reduxjs/toolkit';
import { listToTree } from 'utils';
import {
createTableQueryReducers,
} from 'store/journalNumber.reducer';
createTableStateReducers,
} from 'store/tableState.reducer';
const initialState = {
items: {},
views: {},
list: [],
listTree: [],
accountsTypes: [],
accountsById: {},
tableQuery: {
pageSize: 2,
page: 1,
tableState: {
},
currentViewId: -1,
selectedRows: [],
loading: false,
errors: [],
};
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),
};
},
[t.ACCOUNTS_LIST_SET]: (state, action) => {
const { accounts } = action.payload;
state.list = accounts.map(account => account.id);
state.listTree = listToTree(accounts, {
parentFieldKey: 'parent_account_id',
idFieldKey: 'id',
nodeMapper: (item) => ({
id: item.id,
parent_account_id: item.parent_account_id,
children: [],
}),
});
},
[t.ACCOUNT_TYPES_LIST_SET]: (state, action) => {
state.accountsTypes = action.account_types;
},
[t.ACCOUNT_SET]: (state, action) => {
const { account } = action.payload;
state.items[account.id] = {
...(state.items[account.id] || {}),
...account,
};
},
[t.ACCOUNT_DELETE]: (state, action) => {
if (typeof state.items[action.id] !== 'undefined'){
delete state.items[action.id];
}
},
[t.ACCOUNTS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.ACCOUNTS_TABLE_LOADING]: (state, action) => {
state.loading = action.loading;
},
[t.ACCOUNT_ERRORS_SET]: (state, action) => {
const { errors } = action.payload;
state.errors = errors;
},
[t.ACCOUNT_ERRORS_CLEAR]: (state, action) => {
state.errors = [];
},
[t.ACCOUNTS_BULK_DELETE]: (state, action) => {
const { ids } = action.payload;
const items = { ...state.items };
ids.forEach((id) => {
if (typeof items[id] !== 'undefined') {
delete items[id];
}
});
state.items = items;
},
[t.ACCOUNTS_SELECTED_ROWS_SET]: (state, action) => {
const { selectedRows } = action.payload;
state.selectedRows = selectedRows;
},
...createTableQueryReducers('ACCOUNTS'),
...createTableStateReducers('ACCOUNTS'),
});
export const getAccountById = (state, id) => {
return state.accounts.accountsById[id];
};

View File

@@ -1,79 +1,18 @@
import { createSelector, createSelectorCreator, defaultMemoize } from 'reselect';
import { repeat, isEqual } from 'lodash';
import {
pickItemsFromIds,
getItemById,
} from 'store/selectors';
import { flatToNestedArray, treeToList } from 'utils';
import { paginationLocationQuery } from 'store/selectors';
import { createDeepEqualSelector } from 'utils';
const createDeepEqualSelector = createSelectorCreator(
defaultMemoize,
isEqual
);
const accountsViewsSelector = (state) => state.accounts.views;
const accountsDataSelector = (state) => state.accounts.items;
const accountsCurrentViewSelector = (state) => state.accounts.currentViewId;
const accountIdPropSelector = (state, props) => props.accountId;
const accountsListSelector = (state) => state.accounts.listTree;
const accountsTableQuery = (state, props) => state.accounts.tableQuery;
// Accounts table state selector
const accountsTableStateSelector = (state, props) => state.accounts.tableState;
export const getAccountsTableQuery = createSelector(
accountsTableQuery,
(tableQuery) => {
return {
...tableQuery,
};
},
);
export const getAccountsItems = createDeepEqualSelector(
accountsViewsSelector,
accountsDataSelector,
accountsCurrentViewSelector,
(accountsViews, accountsItems, viewId) => {
const accountsView = accountsViews[viewId || -1];
const config = { id: 'id', parentId: 'parent_account_id' };
const accounts =
typeof accountsView === 'object'
? pickItemsFromIds(accountsItems, accountsView.ids) || []
: [];
return flatToNestedArray(
accounts.map((a) => ({ ...a })),
config,
);
},
);
export const getAccountsListFactory = () =>
// Get accounts table state marged with location query.
export const getAccountsTableStateFactory = () =>
createDeepEqualSelector(
accountsListSelector,
accountsDataSelector,
(accountsTree, accountsItems) => {
return treeToList(accountsTree, {
idFieldKey: 'id',
childrenFieldKey: 'children',
nodeFilter: (node, depth) => accountsItems[node.id],
nodeMapper: (node, depth) => {
const account = accountsItems[node.id];
const spaceChar = String.fromCharCode(160);
return {
...account,
htmlName:
depth > 1
? `${repeat(spaceChar, (depth - 1) * 2)}${account.name}`
: account.name,
depth,
};
},
});
paginationLocationQuery,
accountsTableStateSelector,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableState,
};
},
);
export const getAccountById = createSelector(
accountsDataSelector,
accountIdPropSelector,
(accountsItems, accountId) => {
return getItemById(accountsItems, accountId);
},
);

View File

@@ -1,31 +1,4 @@
export default {
ACCOUNT_TYPES_LIST_SET: 'ACCOUNT_TYPES_LIST_SET',
ACCOUNTS_PAGE_SET: 'ACCOUNTS_PAGE_SET',
ACCOUNTS_LIST_SET: 'ACCOUNTS_LIST_SET',
ACCOUNTS_ITEMS_SET: 'ACCOUNTS_ITEMS_SET',
ACCOUNT_SET: 'ACCOUNT_SET',
ACCOUNT_DELETE: 'ACCOUNT_DELETE',
ACCOUNT_FORM_ERRORS: 'ACCOUNT_FORM_ERRORS',
ACCOUNTS_BULK_ACTION: 'ACCOUNTS_BULK_ACTION',
CLEAR_ACCOUNT_FORM_ERRORS: 'CLEAR_ACCOUNT_FORM_ERRORS',
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_QUERIES_ADD:'ACCOUNTS/TABLE_QUERIES_ADD',
ACCOUNTS_TABLE_LOADING: 'ACCOUNTS_TABLE_LOADING',
ACCOUNT_ERRORS_SET: 'ACCOUNT_ERRORS_SET',
ACCOUNT_ERRORS_CLEAR: 'ACCOUNT_ERRORS_CLEAR',
ACCOUNTS_BULK_DELETE: 'ACCOUNTS_BULK_DELETE',
BULK_ACTIVATE_ACCOUNTS:'BULK_ACTIVATE_ACCOUNTS',
BULK_INACTIVATE_ACCOUNTS:'BULK_INACTIVATE_ACCOUNTS'
ACCOUNTS_TABLE_STATE_SET: 'ACCOUNTS/TABLE_STATE_SET',
};