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,134 +1,12 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitCustomer = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('customers', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
/**
* Sets the customers table state.
*/
export const setCustomersTableState = (queries) => {
return {
type: t.CUSTOMERS_TABLE_STATE_SET,
payload: { queries },
};
};
export const editCustomer = ({ form, id }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`customers/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchCustomers = ({ query }) => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
const pageQuery = getState().customers.tableQuery;
dispatch({
type: t.CUSTOMERS_TABLE_LOADING,
payload: { loading: true },
});
ApiService.get(`customers`, { params: { ...pageQuery, ...query } })
.then((response) => {
dispatch({
type: t.CUSTOMERS_PAGE_SET,
payload: {
customers: response.data.customers,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
paginationMeta: response.data.pagination,
},
});
dispatch({
type: t.CUSTOMERS_ITEMS_SET,
payload: {
customers: response.data.customers,
},
});
dispatch({
type: t.CUSTOMERS_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.CUSTOMERS_TABLE_LOADING,
payload: { loading: false },
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const fetchCustomer = ({ id }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.get(`customers/${id}`)
.then((response) => {
dispatch({
type: t.CUSTOMER_SET,
payload: {
id,
customer: response.data.customer,
},
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const deleteCustomer = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`customers/${id}`)
.then((response) => {
dispatch({
type: t.CUSTOMER_DELETE,
payload: { id },
});
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const deleteBulkCustomers = ({ ids }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.delete('customers', { params: { ids } })
.then((response) => {
dispatch({
type: t.CUSTOMERS_BULK_DELETE,
payload: { ids },
});
resolve(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};

View File

@@ -1,97 +1,13 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
import {
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import { createTableStateReducers } from 'store/tableState.reducer';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
selectedRows: [],
// Responsible for data fetch query based on this query.
tableQuery: {
page_size: 12,
page: 1,
tableState: {
pageSize: 12,
pageIndex: 0,
},
errors: [],
};
export default createReducer(initialState, {
[t.CUSTOMER_SET]: (state, action) => {
const { id, customer } = action.payload;
const _customers = state.items[id] || {};
state.items[id] = { ..._customers, ...customer };
},
[t.CUSTOMERS_ITEMS_SET]: (state, action) => {
const { customers } = action.payload;
const _customers = {};
customers.forEach((customer) => {
_customers[customer.id] = customer;
});
state.items = {
...state.items,
..._customers,
};
},
[t.CUSTOMERS_PAGE_SET]: (state, action) => {
const { customViewId, customers, paginationMeta } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[paginationMeta.page]: {
ids: customers.map((i) => i.id),
},
},
};
},
[t.CUSTOMER_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.CUSTOMERS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.CUSTOMERS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.CUSTOMERS_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.CUSTOMER_SELECTED_ROWS_SET]: (state, action) => {
const { selectedRows } = action.payload;
state.selectedRows = selectedRows;
},
...viewPaginationSetReducer(t.CUSTOMERS_PAGINATION_SET),
...createTableQueryReducers('CUSTOMERS'),
...createTableStateReducers('CUSTOMERS'),
});
export const getCustomerById = (state, id) => {
return state.customers.items[id];
};

View File

@@ -1,70 +1,16 @@
import { createSelector } from 'reselect';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
import { paginationLocationQuery } from 'store/selectors';
import { createDeepEqualSelector } from 'utils';
const customerTableQuery = (state) => state.customers.tableQuery;
const customerTableStateSelector = (state) => state.customers.tableState;
const customersByIdSelector = (state, props) => {
return state.customers.items[props.customerId];
};
const customersPaginationSelector = (state, props) => {
const viewId = state.customers.currentViewId;
return state.customers.views?.[viewId];
};
const customerPageSelector = (state, props) => {
const viewId = state.customers.currentViewId;
const currentView = state.customers.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const customersItemsSelector = (state) => state.customers.items;
const customersCurrentViewIdSelector = (state) => state.customers.currentViewId;
export const getCustomerTableQueryFactory = () =>
createSelector(
export const getCustomersTableStateFactory = () =>
createDeepEqualSelector(
paginationLocationQuery,
customerTableQuery,
(locationQuery, tableQuery) => {
customerTableStateSelector,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableQuery,
...tableState,
};
},
);
export const getCustomerCurrentPageFactory = () =>
createSelector(
customerPageSelector,
customersItemsSelector,
(customerPage, customersItems) => {
return typeof customerPage === 'object'
? pickItemsFromIds(customersItems, customerPage.ids) || []
: [];
},
);
export const getCustomersByIdFactory = () =>
createSelector(customersByIdSelector, (customer) => {
return customer;
});
export const getCustomerPaginationMetaFactory = () =>
createSelector(customersPaginationSelector, (customerPage) => {
return {
...defaultPaginationMeta(),
...(customerPage?.paginationMeta || {}),
};
});
export const getCustomersCurrentViewIdFactory = () =>
createSelector(customersCurrentViewIdSelector, (currentViewId) => {
return currentViewId;
});

View File

@@ -1,12 +1,3 @@
export default {
CUSTOMERS_ITEMS_SET: 'CUSTOMERS_ITEMS_SET',
CUSTOMER_SET: 'CUSTOMER_SET',
CUSTOMERS_PAGE_SET: 'CUSTOMERS_PAGE_SET',
CUSTOMERS_TABLE_LOADING: 'CUSTOMERS_TABLE_LOADING',
CUSTOMERS_TABLE_QUERIES_ADD: 'CUSTOMERS/TABLE_QUERIES_ADD',
CUSTOMER_DELETE: 'CUSTOMER_DELETE',
CUSTOMERS_BULK_DELETE: 'CUSTOMERS_BULK_DELETE',
CUSTOMERS_PAGINATION_SET: 'CUSTOMERS_PAGINATION_SET',
CUSTOMERS_SET_CURRENT_VIEW: 'CUSTOMERS_SET_CURRENT_VIEW',
CUSTOMER_SELECTED_ROWS_SET: 'CUSTOMER_SELECTED_ROWS_SET',
CUSTOMERS_TABLE_STATE_SET: 'CUSTOMERS/TABLE_STATE_SET'
};