mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
refactoring: sales tables.
refacoring: purchases tables.
This commit is contained in:
@@ -1,135 +1,10 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitEstimate = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post('sales/estimates', form)
|
||||
.then((response) => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
export const setEstimatesTableState = (queries) => {
|
||||
return {
|
||||
type: t.ESTIMATES_TABLE_STATE_SET,
|
||||
payload: { queries },
|
||||
};
|
||||
};
|
||||
|
||||
export const editEstimate = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post(`sales/estimates/${id}`, form)
|
||||
.then((response) => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteEstimate = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`sales/estimates/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.ESTIMATE_DELETE,
|
||||
payload: { id },
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchEstimate = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`sales/estimates/${id}`)
|
||||
.then((response) => {
|
||||
const { estimate } = response.data;
|
||||
dispatch({
|
||||
type: t.ESTIMATE_SET,
|
||||
payload: {
|
||||
id,
|
||||
estimate,
|
||||
},
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchEstimatesTable = ({ query = {} }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const pageQuery = getState().salesEstimates.tableQuery;
|
||||
dispatch({
|
||||
type: t.ESTIMATES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('sales/estimates', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.ESTIMATES_PAGE_SET,
|
||||
payload: {
|
||||
sales_estimates: response.data.sales_estimates,
|
||||
pagination: response.data.pagination,
|
||||
customViewId:
|
||||
response.data?.filter_meta?.view?.custom_view_id || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_ITEMS_SET,
|
||||
payload: {
|
||||
sales_estimates: response.data.sales_estimates,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.pagination,
|
||||
customViewId:
|
||||
response.data?.filter_meta?.view?.custom_view_id || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deliverEstimate = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`sales/estimates/${id}/deliver`);
|
||||
};
|
||||
export const approveEstimate = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`sales/estimates/${id}/approve`);
|
||||
};
|
||||
export const rejectEstimate = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`sales/estimates/${id}/reject`);
|
||||
};
|
||||
export const setSelectedRowsItems = () => {};
|
||||
|
||||
@@ -1,116 +1,15 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import {
|
||||
journalNumberChangedReducer,
|
||||
createTableQueryReducers,
|
||||
} from 'store/journalNumber.reducer';
|
||||
|
||||
import t from 'store/types';
|
||||
createTableStateReducers,
|
||||
} from 'store/tableState.reducer';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
tableQuery: {
|
||||
page_size: 12,
|
||||
page: 1,
|
||||
tableState: {
|
||||
pageSize: 12,
|
||||
pageIndex: 0,
|
||||
},
|
||||
currentViewId: -1,
|
||||
selectedRows: [],
|
||||
};
|
||||
|
||||
const defaultEstimate = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
export default createReducer(initialState, {
|
||||
[t.ESTIMATE_SET]: (state, action) => {
|
||||
const { id, estimate } = action.payload;
|
||||
const _estimate = state.items[id] || {};
|
||||
|
||||
state.items[id] = { ...defaultEstimate, ..._estimate, ...estimate };
|
||||
},
|
||||
|
||||
[t.ESTIMATES_ITEMS_SET]: (state, action) => {
|
||||
const { sales_estimates } = action.payload;
|
||||
const _estimates = {};
|
||||
sales_estimates.forEach((estimate) => {
|
||||
_estimates[estimate.id] = {
|
||||
...defaultEstimate,
|
||||
...estimate,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._estimates,
|
||||
};
|
||||
},
|
||||
|
||||
[t.ESTIMATES_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.ESTIMATES_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.ESTIMATE_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.ESTIMATES_PAGE_SET]: (state, action) => {
|
||||
// @todo camelCase keys.
|
||||
const { customViewId, sales_estimates, pagination } = action.payload;
|
||||
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...(state.views?.[viewId]?.pages || {}),
|
||||
[pagination.page]: {
|
||||
ids: sales_estimates.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.ESTIMATES_PAGINATION_SET]: (state, action) => {
|
||||
const { pagination, customViewId } = action.payload;
|
||||
|
||||
const mapped = {
|
||||
pageSize: parseInt(pagination.pageSize, 10),
|
||||
page: parseInt(pagination.page, 10),
|
||||
total: parseInt(pagination.total, 10),
|
||||
};
|
||||
const paginationMeta = {
|
||||
...mapped,
|
||||
pagesCount: Math.ceil(mapped.total / mapped.pageSize),
|
||||
pageIndex: Math.max(mapped.page - 1, 0),
|
||||
};
|
||||
|
||||
state.views = {
|
||||
...state.views,
|
||||
[customViewId]: {
|
||||
...(state.views?.[customViewId] || {}),
|
||||
paginationMeta,
|
||||
},
|
||||
};
|
||||
},
|
||||
[t.ESTIMATES_SELECTED_ROWS_SET]: (state, action) => {
|
||||
const { selectedRows } = action.payload;
|
||||
state.selectedRows = selectedRows;
|
||||
},
|
||||
|
||||
...journalNumberChangedReducer(t.ESTIMATE_NUMBER_CHANGED),
|
||||
...createTableQueryReducers('ESTIMATES'),
|
||||
...createTableStateReducers('ESTIMATES'),
|
||||
});
|
||||
|
||||
export const getEstimateById = (state, id) => {
|
||||
return state.sales_estimates.items[id];
|
||||
};
|
||||
|
||||
@@ -1,73 +1,17 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery, defaultPaginationMeta } from 'store/selectors';
|
||||
|
||||
const estimateTableQuery = (state) => state.salesEstimates.tableQuery;
|
||||
|
||||
const estimateByIdSelector = (state, props) =>
|
||||
state.salesEstimates.items[props.estimateId];
|
||||
|
||||
const estimatesCurrentViewSelector = (state, props) => {
|
||||
const viewId = state.salesEstimates.currentViewId;
|
||||
return state.salesEstimates.views?.[viewId];
|
||||
};
|
||||
const estimateItemsSelector = (state) => state.salesEstimates.items;
|
||||
|
||||
const estimatesPageSelector = (state, props, query) => {
|
||||
const viewId = state.salesEstimates.currentViewId;
|
||||
const currentPageId = state.salesEstimates.views?.[viewId]?.paginationMeta?.page;
|
||||
|
||||
return state.salesEstimates.views?.[viewId]?.pages?.[currentPageId];
|
||||
};
|
||||
|
||||
const getEstimatesCurrentViewIdSelector = (state, props) => {
|
||||
return state.salesEstimates.currentViewId;
|
||||
};
|
||||
import { createDeepEqualSelector } from 'utils';
|
||||
import { paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const estimatesTableState = (state) => state.salesEstimates.tableState;
|
||||
|
||||
// Retrieve estimates table query.
|
||||
export const getEstimatesTableQueryFactory = () =>
|
||||
createSelector(
|
||||
export const getEstimatesTableStateFactory = () =>
|
||||
createDeepEqualSelector(
|
||||
paginationLocationQuery,
|
||||
estimateTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
estimatesTableState,
|
||||
(locationQuery, tableState) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
...tableState,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// Retreive estimate results of the current page.
|
||||
export const getEstimateCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
estimatesPageSelector,
|
||||
estimateItemsSelector,
|
||||
(estimatePage, estimateItems) => {
|
||||
return typeof estimatePage === 'object'
|
||||
? pickItemsFromIds(estimateItems, estimatePage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
// Retrieve specific estimate by the passed estimate id.
|
||||
export const getEstimateByIdFactory = () =>
|
||||
createSelector(estimateByIdSelector, (estimate) => {
|
||||
return estimate;
|
||||
});
|
||||
|
||||
// Retrieve estimates pagination meta.
|
||||
export const getEstimatesPaginationMetaFactory = () =>
|
||||
createSelector(estimatesCurrentViewSelector, (estimateView) => {
|
||||
return {
|
||||
...defaultPaginationMeta(),
|
||||
...(estimateView?.paginationMeta || {}),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
// Retrieve estimates current view id.
|
||||
export const getEstimatesCurrentViewIdFactory = () =>
|
||||
createSelector(
|
||||
getEstimatesCurrentViewIdSelector,
|
||||
(currentViewId) => {
|
||||
return currentViewId;
|
||||
});
|
||||
@@ -1,14 +1,3 @@
|
||||
export default {
|
||||
ESTIMATES_LIST_SET: 'ESTIMATES_LIST_SET',
|
||||
ESTIMATE_SET: 'ESTIMATE_SET',
|
||||
ESTIMATE_DELETE: 'ESTIMATE_DELETE',
|
||||
ESTIMATES_BULK_DELETE: 'ESTIMATES_BULK_DELETE',
|
||||
ESTIMATES_SET_CURRENT_VIEW: 'ESTIMATES/SET_CURRENT_VIEW',
|
||||
ESTIMATES_TABLE_QUERIES_ADD: 'ESTIMATES/TABLE_QUERIES_ADD',
|
||||
ESTIMATES_TABLE_LOADING: 'ESTIMATES_TABLE_LOADING',
|
||||
ESTIMATES_PAGINATION_SET: 'ESTIMATES_PAGINATION_SET',
|
||||
ESTIMATES_PAGE_SET: 'ESTIMATES_PAGE_SET',
|
||||
ESTIMATES_ITEMS_SET: 'ESTIMATES_ITEMS_SET',
|
||||
ESTIMATE_NUMBER_CHANGED: 'ESTIMATE_NUMBER_CHANGED',
|
||||
ESTIMATES_SELECTED_ROWS_SET: 'ESTIMATES_SELECTED_ROWS_SET',
|
||||
ESTIMATES_TABLE_STATE_SET: 'ESTIMATES/TABLE_STATE_SET',
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user