mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
feat: Merge sales branch
This commit is contained in:
95
client/src/store/Bills/bills.actions.js
Normal file
95
client/src/store/Bills/bills.actions.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const fetchBillsTable = ({ query = {} }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, rejcet) => {
|
||||
const pageQuery = getState().bills.tableQuery;
|
||||
|
||||
dispatch({
|
||||
type: t.BILLS_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('purchases/bills', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.BILLS_PAGE_SET,
|
||||
payload: {
|
||||
bills: response.data.bills.results,
|
||||
pagination: response.data.bills.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.BILLS_ITEMS_SET,
|
||||
payload: {
|
||||
bills: response.data.bills.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.BILLS_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.bills.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.BILLS_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
rejcet(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBill = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`purchases/bills/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.BILL_DELETE, payload: { id } });
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const submitBill = ({ form }) => {
|
||||
return (dispatch) => ApiService.post('purchases/bills', form);
|
||||
};
|
||||
|
||||
export const fetchBill = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`purchases/bills/${id}`)
|
||||
.then((response) => {
|
||||
const { bill } = response.data;
|
||||
|
||||
dispatch({
|
||||
type: t.BILL_SET,
|
||||
payload: { id, bill },
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editBill = (id, form) => {
|
||||
return (dispatch) => ApiService.post(`purchases/bills/${id}`, form);
|
||||
};
|
||||
104
client/src/store/Bills/bills.reducer.js
Normal file
104
client/src/store/Bills/bills.reducer.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
currentViewId: -1,
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
},
|
||||
};
|
||||
|
||||
const defaultBill = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
[t.BILL_SET]: (state, action) => {
|
||||
const { id, bill } = action.payload;
|
||||
const _bill = state.items[id] || {};
|
||||
|
||||
state.items[id] = { ...defaultBill, ..._bill, ...bill };
|
||||
},
|
||||
|
||||
[t.BILLS_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.BILLS_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.BILLS_ITEMS_SET]: (state, action) => {
|
||||
const { bills } = action.payload;
|
||||
const _bills = {};
|
||||
|
||||
bills.forEach((bill) => {
|
||||
const oldBill = state.items[bill.id] || {};
|
||||
|
||||
_bills[bill.id] = {
|
||||
...defaultBill,
|
||||
...oldBill,
|
||||
...bill,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._bills,
|
||||
};
|
||||
},
|
||||
|
||||
[t.BILL_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.BILLS_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, bills, pagination } = action.payload;
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...(state.views?.[viewId]?.pages || {}),
|
||||
[pagination.page]: {
|
||||
ids: bills.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.BILLS_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,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('bills', reducer);
|
||||
53
client/src/store/Bills/bills.selectors.js
Normal file
53
client/src/store/Bills/bills.selectors.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const billTableQuery = (state) => state.bills.tableQuery;
|
||||
|
||||
const billPageSelector = (state, props, query) => {
|
||||
const viewId = state.bills.currentViewId;
|
||||
return state.bills.views?.[viewId]?.pages?.[query.page];
|
||||
};
|
||||
const billItemsSelector = (state) => state.bills.items;
|
||||
|
||||
const billByIdSelector = (state, props) => state.bills.items[props.billId];
|
||||
|
||||
const billPaginationSelector = (state, props) => {
|
||||
const viewId = state.bills.currentViewId;
|
||||
return state.bills.views?.[viewId];
|
||||
};
|
||||
|
||||
export const getBillTableQueryFactory = () =>
|
||||
createSelector(
|
||||
paginationLocationQuery,
|
||||
billTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Get current page bills items.
|
||||
* @return {Array}
|
||||
*/
|
||||
export const getBillCurrentPageFactory = () =>
|
||||
createSelector(billPageSelector, billItemsSelector, (billPage, billItems) => {
|
||||
return typeof billPage === 'object'
|
||||
? pickItemsFromIds(billItems, billPage.ids) || []
|
||||
: [];
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieve bill details of the given bill id.
|
||||
*/
|
||||
export const getBillByIdFactory = () =>
|
||||
createSelector(billByIdSelector, (bill) => {
|
||||
return bill;
|
||||
});
|
||||
|
||||
export const getBillPaginationMetaFactory = () =>
|
||||
createSelector(billPaginationSelector, (billPage) => {
|
||||
return billPage?.paginationMeta || {};
|
||||
});
|
||||
12
client/src/store/Bills/bills.type.js
Normal file
12
client/src/store/Bills/bills.type.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export default {
|
||||
BILL_DELETE: 'BILL_DELETE',
|
||||
BILLS_BULK_DELETE: 'BILLS_BULK_DELETE',
|
||||
BILLS_LIST_SET: 'BILLS_LIST_SET',
|
||||
BILL_SET: 'BILL_SET',
|
||||
BILLS_SET_CURRENT_VIEW: 'BILLS_SET_CURRENT_VIEW',
|
||||
BILLS_TABLE_QUERIES_ADD: 'BILLS_TABLE_QUERIES_ADD',
|
||||
BILLS_TABLE_LOADING: 'BILLS_TABLE_LOADING',
|
||||
BILLS_PAGINATION_SET: 'BILLS_PAGINATION_SET',
|
||||
BILLS_PAGE_SET: 'BILLS_PAGE_SET',
|
||||
BILLS_ITEMS_SET: 'BILLS_ITEMS_SET',
|
||||
};
|
||||
140
client/src/store/Estimate/estimates.actions.js
Normal file
140
client/src/store/Estimate/estimates.actions.js
Normal file
@@ -0,0 +1,140 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitEstimate = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post('sales/estimates', form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editEstimate = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post(`sales/estimates/${id}`, form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
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, rejcet) => {
|
||||
const pageQuery = getState().sales_estimates.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.results,
|
||||
pagination: response.data.sales_estimates.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_ITEMS_SET,
|
||||
payload: {
|
||||
sales_estimates: response.data.sales_estimates.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.sales_estimates.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.ESTIMATES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
rejcet(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
107
client/src/store/Estimate/estimates.reducer.js
Normal file
107
client/src/store/Estimate/estimates.reducer.js
Normal file
@@ -0,0 +1,107 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
},
|
||||
currentViewId: -1,
|
||||
};
|
||||
|
||||
const defaultEstimate = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
const reducer = 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,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('sales_estimates', reducer);
|
||||
|
||||
export const getEstimateById = (state, id) => {
|
||||
return state.sales_estimates.items[id];
|
||||
};
|
||||
51
client/src/store/Estimate/estimates.selectors.js
Normal file
51
client/src/store/Estimate/estimates.selectors.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const estimateTableQuery = (state) => state.sales_estimates.tableQuery;
|
||||
|
||||
const estimateByIdSelector = (state, props) =>
|
||||
state.sales_estimates.items[props.estimateId];
|
||||
|
||||
const estimatesCurrentViewSelector = (state, props) => {
|
||||
const viewId = state.sales_estimates.currentViewId;
|
||||
return state.sales_estimates.views?.[viewId];
|
||||
};
|
||||
const estimateItemsSelector = (state) => state.sales_estimates.items;
|
||||
|
||||
const estimatesPageSelector = (state, props, query) => {
|
||||
const viewId = state.sales_estimates.currentViewId;
|
||||
return state.sales_estimates.views?.[viewId]?.pages?.[query.page];
|
||||
};
|
||||
|
||||
export const getEstimatesTableQueryFactory = () =>
|
||||
createSelector(
|
||||
paginationLocationQuery,
|
||||
estimateTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
export const getEstimateCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
estimatesPageSelector,
|
||||
estimateItemsSelector,
|
||||
(estimatePage, estimateItems) => {
|
||||
return typeof estimatePage === 'object'
|
||||
? pickItemsFromIds(estimateItems, estimatePage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
export const getEstimateByIdFactory = () =>
|
||||
createSelector(estimateByIdSelector, (estimate) => {
|
||||
return estimate;
|
||||
});
|
||||
|
||||
export const getEstimatesPaginationMetaFactory = () =>
|
||||
createSelector(estimatesCurrentViewSelector, (estimateView) => {
|
||||
return estimateView?.paginationMeta || {};
|
||||
});
|
||||
13
client/src/store/Estimate/estimates.types.js
Normal file
13
client/src/store/Estimate/estimates.types.js
Normal file
@@ -0,0 +1,13 @@
|
||||
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',
|
||||
|
||||
};
|
||||
145
client/src/store/Invoice/invoices.actions.js
Normal file
145
client/src/store/Invoice/invoices.actions.js
Normal file
@@ -0,0 +1,145 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitInvoice = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
// @todo remove dead-code
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post('sales/invoices', form)
|
||||
.then((response) => {
|
||||
// @todo remove dead-code
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
// @todo remove dead-code
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteInvoice = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`sales/invoices/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICE_DELETE,
|
||||
payload: { id },
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editInvoice = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post(`sales/invoices/${id}`, form)
|
||||
.then((response) => {
|
||||
// @todo remove dead-code
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
// @todo remove dead-code
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchInvoicesTable = ({ query } = {}) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const pageQuery = getState().sales_invoices.tableQuery;
|
||||
dispatch({
|
||||
type: t.INVOICES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('sales/invoices', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICES_PAGE_SET,
|
||||
payload: {
|
||||
sales_invoices: response.data.sales_invoices.results,
|
||||
pagination: response.data.sales_invoices.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_ITEMS_SET,
|
||||
payload: {
|
||||
sales_invoices: response.data.sales_invoices.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.sales_invoices.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchInvoice = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`sales/invoices/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICE_SET,
|
||||
payload: {
|
||||
id,
|
||||
sale_invoice: response.data.sale_invoice,
|
||||
},
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
105
client/src/store/Invoice/invoices.reducer.js
Normal file
105
client/src/store/Invoice/invoices.reducer.js
Normal file
@@ -0,0 +1,105 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
currentViewId: -1,
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
},
|
||||
};
|
||||
|
||||
const defaultInvoice = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
[t.INVOICE_SET]: (state, action) => {
|
||||
const { id, sale_invoice } = action.payload;
|
||||
const _invoice = state.items[id] || {};
|
||||
|
||||
state.items[id] = { ...defaultInvoice, ..._invoice, ...sale_invoice };
|
||||
},
|
||||
|
||||
[t.INVOICES_ITEMS_SET]: (state, action) => {
|
||||
const { sales_invoices } = action.payload;
|
||||
const _invoices = {};
|
||||
sales_invoices.forEach((invoice) => {
|
||||
_invoices[invoice.id] = {
|
||||
...defaultInvoice,
|
||||
...invoice,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._invoices,
|
||||
};
|
||||
},
|
||||
|
||||
[t.INVOICES_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.INVOICES_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.INVOICE_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.INVOICES_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, sales_invoices, 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_invoices.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.INVOICES_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,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('sales_invoices', reducer);
|
||||
|
||||
export const getInvoiceById = (state, id) => {
|
||||
return state.sales_invoices.items[id];
|
||||
};
|
||||
53
client/src/store/Invoice/invoices.selector.js
Normal file
53
client/src/store/Invoice/invoices.selector.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const invoiceTableQuery = (state) => state.sales_invoices.tableQuery;
|
||||
|
||||
const invoicesByIdSelector = (state, props) =>
|
||||
state.sales_invoices.items[props.invoiceId];
|
||||
|
||||
const invoicesPaginationSelector = (state, props) => {
|
||||
const viewId = state.sales_invoices.currentViewId;
|
||||
return state.sales_invoices.views?.[viewId];
|
||||
};
|
||||
|
||||
const invoicesPageSelector = (state, props, query) => {
|
||||
const viewId = state.sales_invoices.currentViewId;
|
||||
return state.sales_invoices.views?.[viewId]?.pages?.[query.page];
|
||||
};
|
||||
|
||||
const invoicesItemsSelector = (state) => state.sales_invoices.items;
|
||||
|
||||
|
||||
export const getInvoiceTableQueryFactory = () =>
|
||||
createSelector(
|
||||
paginationLocationQuery,
|
||||
invoiceTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
export const getInvoiceCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
invoicesPageSelector,
|
||||
invoicesItemsSelector,
|
||||
(invoicePage, invoicesItems) => {
|
||||
return typeof invoicePage === 'object'
|
||||
? pickItemsFromIds(invoicesItems, invoicePage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
export const getInvoiecsByIdFactory = () =>
|
||||
createSelector(invoicesByIdSelector, (invoice) => {
|
||||
return invoice;
|
||||
});
|
||||
|
||||
export const getInvoicePaginationMetaFactory = () =>
|
||||
createSelector(invoicesPaginationSelector, (invoicePage) => {
|
||||
return invoicePage?.paginationMeta || {};
|
||||
});
|
||||
12
client/src/store/Invoice/invoices.types.js
Normal file
12
client/src/store/Invoice/invoices.types.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export default {
|
||||
INVOICE_DELETE: 'INVOICE_DELETE',
|
||||
INVOICES_BULK_DELETE: 'INVOICES_BULK_DELETE',
|
||||
INVOICES_LIST_SET: 'INVOICES_LIST_SET',
|
||||
INVOICE_SET: 'INVOICE_SET',
|
||||
INVOICES_SET_CURRENT_VIEW: 'INVOICES_SET_CURRENT_VIEW',
|
||||
INVOICES_TABLE_QUERIES_ADD: 'INVOICES_TABLE_QUERIES_ADD',
|
||||
INVOICES_TABLE_LOADING: 'INVOICES_TABLE_LOADING',
|
||||
INVOICES_PAGINATION_SET: 'INVOICES_PAGINATION_SET',
|
||||
INVOICES_PAGE_SET: 'INVOICES_PAGE_SET',
|
||||
INVOICES_ITEMS_SET: 'INVOICES_ITEMS_SET',
|
||||
};
|
||||
136
client/src/store/PaymentReceive/paymentReceive.actions.js
Normal file
136
client/src/store/PaymentReceive/paymentReceive.actions.js
Normal file
@@ -0,0 +1,136 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitPaymentReceive = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post('sales/payment_receives', form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editPaymentReceive = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, rejcet) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post(`sales/payment_receives/${id}`, form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
rejcet(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deletePaymentReceive = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`payment_receives/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.PAYMENT_RECEIVE_DELETE });
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchPaymentReceive = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`payment_receives/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.PAYMENT_RECEIVE_SET,
|
||||
payload: {
|
||||
id,
|
||||
payment_receive: response.data.payment_receive,
|
||||
},
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchPaymentReceivesTable = ({ query = {} }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, rejcet) => {
|
||||
const pageQuery = getState().payment_receive.tableQuery;
|
||||
|
||||
dispatch({
|
||||
type: t.PAYMENT_RECEIVES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('payment_receives', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.RECEIPTS_PAGE_SET,
|
||||
payload: {
|
||||
payment_receives: response.data.payment_receives.results,
|
||||
pagination: response.data.payment_receives.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.PAYMENT_RECEIVES_ITEMS_SET,
|
||||
payload: {
|
||||
payment_receives: response.data.payment_receives.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.PAYMENT_RECEIVES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.payment_receives.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.PAYMENT_RECEIVES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
rejcet(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
11
client/src/store/PaymentReceive/paymentReceive.type.js
Normal file
11
client/src/store/PaymentReceive/paymentReceive.type.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export default {
|
||||
PAYMENT_RECEIVE_LIST_SET: 'PAYMENT_RECEIVE_LIST_SET',
|
||||
PAYMENT_RECEIVE_SET: 'PAYMENT_RECEIVE_SET',
|
||||
PAYMENT_RECEIVE_DELETE: 'PAYMENT_RECEIVE_DELETE',
|
||||
PAYMENT_RECEIVE_SET_CURRENT_VIEW: 'PAYMENT_RECEIVE_SET_CURRENT_VIEW',
|
||||
PAYMENT_RECEIVE_TABLE_QUERIES_ADD: 'PAYMENT_RECEIVE_TABLE_QUERIES_ADD',
|
||||
PAYMENT_RECEIVES_TABLE_LOADING: 'PAYMENT_RECEIVES_TABLE_LOADING',
|
||||
PAYMENT_RECEIVES_PAGE_SET: 'PAYMENT_RECEIVES_PAGE_SET',
|
||||
PAYMENT_RECEIVES_ITEMS_SET: 'PAYMENT_RECEIVES_ITEMS_SET',
|
||||
PAYMENT_RECEIVES_PAGINATION_SET: 'PAYMENT_RECEIVES_PAGINATION_SET',
|
||||
};
|
||||
138
client/src/store/receipt/receipt.actions.js
Normal file
138
client/src/store/receipt/receipt.actions.js
Normal file
@@ -0,0 +1,138 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitReceipt = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post('sales/receipts', form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteReceipt = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`sales/receipts/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.RECEIPT_DELETE,
|
||||
payload: { id },
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editReceipt = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, rejcet) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.post(`sales/receipts/${id}`, form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
rejcet(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchReceipt = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`sales/receipts/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.RECEIPT_SET,
|
||||
payload: {
|
||||
id,
|
||||
receipt: response.data.receipt,
|
||||
},
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchReceiptsTable = ({ query = {} }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, rejcet) => {
|
||||
const pageQuery = getState().sales_receipts.tableQuery;
|
||||
dispatch({
|
||||
type: t.RECEIPTS_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('sales/receipts', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.RECEIPTS_PAGE_SET,
|
||||
payload: {
|
||||
sales_receipts: response.data.sales_receipts.results,
|
||||
pagination: response.data.sales_receipts.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.RECEIPTS_ITEMS_SET,
|
||||
payload: {
|
||||
sales_receipts: response.data.sales_receipts.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.RECEIPTS_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.sales_receipts.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.RECEIPTS_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
rejcet(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
103
client/src/store/receipt/receipt.reducer.js
Normal file
103
client/src/store/receipt/receipt.reducer.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
},
|
||||
currentViewId: -1,
|
||||
};
|
||||
|
||||
const defaultReceipt = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
[t.RECEIPT_SET]: (state, action) => {
|
||||
const { id, receipt } = action.payload;
|
||||
const _receipt = state.items[id] || {};
|
||||
state.items[id] = { ...defaultReceipt, ..._receipt, ...receipt };
|
||||
},
|
||||
|
||||
[t.RECEIPTS_ITEMS_SET]: (state, action) => {
|
||||
const { sales_receipts } = action.payload;
|
||||
const _receipts = {};
|
||||
sales_receipts.forEach((receipt) => {
|
||||
_receipts[receipt.id] = {
|
||||
...defaultReceipt,
|
||||
...receipt,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._receipts,
|
||||
};
|
||||
},
|
||||
|
||||
[t.RECEIPTS_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.RECEIPT_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.RECEIPTS_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.RECEIPTS_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, sales_receipts, 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_receipts.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.RECEIPTS_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,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('sales_receipts', reducer);
|
||||
|
||||
export const getReceiptById = (state, id) => {
|
||||
return state.receipts.items[id];
|
||||
};
|
||||
52
client/src/store/receipt/receipt.selector.js
Normal file
52
client/src/store/receipt/receipt.selector.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const receiptsPageSelector = (state, props, query) => {
|
||||
const viewId = state.sales_receipts.currentViewId;
|
||||
return state.sales_receipts.views?.[viewId]?.pages?.[query.page];
|
||||
};
|
||||
|
||||
const receiptsPaginationSelector = (state, props) => {
|
||||
const viewId = state.sales_receipts.currentViewId;
|
||||
return state.sales_receipts.views?.[viewId];
|
||||
};
|
||||
|
||||
const receiptItemsSelector = (state) => state.sales_receipts.items;
|
||||
|
||||
const receiptTableQuery = (state) => state.sales_receipts.tableQuery;
|
||||
|
||||
const receiptByIdSelector = (state, props) => state.sales_receipts.items[props.receiptId];
|
||||
|
||||
|
||||
export const getReceiptCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
receiptsPageSelector,
|
||||
receiptItemsSelector,
|
||||
(receiptPage, receiptItems) => {
|
||||
return typeof receiptPage === 'object'
|
||||
? pickItemsFromIds(receiptItems, receiptPage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
export const getReceiptsTableQueryFactory = () =>
|
||||
createSelector(
|
||||
paginationLocationQuery,
|
||||
receiptTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
export const getReceiptByIdFactory = () =>
|
||||
createSelector(receiptByIdSelector, (receipt) => {
|
||||
return receipt;
|
||||
});
|
||||
|
||||
export const getReceiptsPaginationMetaFactory = () =>
|
||||
createSelector(receiptsPaginationSelector, (receiptPage) => {
|
||||
return receiptPage?.paginationMeta || {};
|
||||
});
|
||||
12
client/src/store/receipt/receipt.type.js
Normal file
12
client/src/store/receipt/receipt.type.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export default {
|
||||
RECEIPT_DELETE: 'RECEIPT_DELETE',
|
||||
RECEIPTS_BULK_DELETE: 'RECEIPTS_BULK_DELETE',
|
||||
RECEIPTS_LIST_SET: 'RECEIPTS_LIST_SET',
|
||||
RECEIPT_SET: 'RECEIPT_SET',
|
||||
RECEIPTS_SET_CURRENT_VIEW: 'RECEIPTS_SET_CURRENT_VIEW',
|
||||
RECEIPTS_TABLE_QUERIES_ADD: 'RECEIPTS_TABLE_QUERIES_ADD',
|
||||
RECEIPTS_TABLE_LOADING: 'RECEIPTS_TABLE_LOADING',
|
||||
RECEIPTS_PAGINATION_SET: 'RECEIPTS_PAGINATION_SET',
|
||||
RECEIPTS_PAGE_SET: 'RECEIPTS_PAGE_SET',
|
||||
RECEIPTS_ITEMS_SET: 'RECEIPTS_ITEMS_SET',
|
||||
};
|
||||
@@ -18,6 +18,11 @@ import globalSearch from './search/search.reducer';
|
||||
import exchangeRates from './ExchangeRate/exchange.reducer';
|
||||
import globalErrors from './globalErrors/globalErrors.reducer';
|
||||
import customers from './customers/customers.reducer';
|
||||
import sales_estimates from './Estimate/estimates.reducer';
|
||||
import sales_invoices from './Invoice/invoices.reducer';
|
||||
import sales_receipts from './receipt/receipt.reducer';
|
||||
import bills from './Bills/bills.reducer';
|
||||
import vendors from './vendors/vendors.reducer';
|
||||
|
||||
export default combineReducers({
|
||||
authentication,
|
||||
@@ -38,4 +43,11 @@ export default combineReducers({
|
||||
exchangeRates,
|
||||
globalErrors,
|
||||
customers,
|
||||
|
||||
// @todo camelCase
|
||||
sales_estimates,
|
||||
sales_invoices,
|
||||
sales_receipts,
|
||||
bills,
|
||||
vendors,
|
||||
});
|
||||
|
||||
@@ -17,6 +17,12 @@ import search from './search/search.type';
|
||||
import register from './registers/register.type';
|
||||
import exchangeRate from './ExchangeRate/exchange.type';
|
||||
import customer from './customers/customers.type';
|
||||
import estimates from './Estimate/estimates.types';
|
||||
import invoices from './Invoice/invoices.types';
|
||||
import receipts from './receipt/receipt.type';
|
||||
import bills from './Bills/bills.type';
|
||||
import paymentReceives from './PaymentReceive/paymentReceive.type';
|
||||
import vendors from './vendors/vendors.types';
|
||||
|
||||
export default {
|
||||
...authentication,
|
||||
@@ -38,4 +44,10 @@ export default {
|
||||
...register,
|
||||
...exchangeRate,
|
||||
...customer,
|
||||
...estimates,
|
||||
...invoices,
|
||||
...receipts,
|
||||
...bills,
|
||||
...paymentReceives,
|
||||
...vendors,
|
||||
};
|
||||
|
||||
120
client/src/store/vendors/vendors.actions.js
vendored
Normal file
120
client/src/store/vendors/vendors.actions.js
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const fetchVendorsTable = ({ query }) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const pageQuery = getState().vendors.tableQuery;
|
||||
dispatch({
|
||||
type: t.VENDORS_TABLE_LOADING,
|
||||
payload: { loading: true },
|
||||
});
|
||||
// @todo remove dead-code.
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
ApiService.get(`vendors`, { params: { ...pageQuery, ...query } })
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.VENDORS_PAGE_SET,
|
||||
payload: {
|
||||
vendors: response.data.vendors.results,
|
||||
pagination: response.data.vendors.pagination,
|
||||
customViewId: response.data.customViewId,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.VENDORS_ITEMS_SET,
|
||||
payload: {
|
||||
vendors: response.data.vendors.results,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.VENDORS_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.vendors.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.VENDORS_TABLE_LOADING,
|
||||
payload: { loading: false },
|
||||
});
|
||||
// @todo remove dead-lock.
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editVendor = ({ form, id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
// @todo remove dread-code.
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
|
||||
ApiService.post(`vendors/${id}`, form)
|
||||
.then((response) => {
|
||||
// @todo remove dread-code.
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
// @todo remove dread-code.
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteVendor = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.delete(`vendors/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.VENDOR_DELETE, payload: { id } });
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const submitVendor = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_LOADING,
|
||||
});
|
||||
|
||||
ApiService.post('vendors', form)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
dispatch({
|
||||
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
|
||||
});
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
96
client/src/store/vendors/vendors.reducer.js
vendored
Normal file
96
client/src/store/vendors/vendors.reducer.js
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
},
|
||||
currentViewId: -1,
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
[t.VENDORS_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.VENDORS_ITEMS_SET]: (state, action) => {
|
||||
const { vendors } = action.payload;
|
||||
const _vendors = {};
|
||||
vendors.forEach((vendor) => {
|
||||
_vendors[vendor.id] = {
|
||||
...vendor,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._vendors,
|
||||
};
|
||||
},
|
||||
|
||||
[t.VENDORS_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, vendors, pagination } = action.payload;
|
||||
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...(state.views?.[viewId]?.pages || {}),
|
||||
[pagination.page]: {
|
||||
ids: vendors.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.VENDOR_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.VENDORS_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.VENDORS_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.VENDOR_SET]: (state, action) => {
|
||||
// const { id, vendor } = action.payload;
|
||||
// const _venders = state.items[id] || {};
|
||||
// state.items[id] = { ..._venders, ...vendor };
|
||||
// },
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('vendors', reducer);
|
||||
54
client/src/store/vendors/vendors.selectors.js
vendored
Normal file
54
client/src/store/vendors/vendors.selectors.js
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const vendorsTableQuery = (state) => {
|
||||
return state.vendors.tableQuery;
|
||||
};
|
||||
|
||||
export const getVendorsTableQuery = createSelector(
|
||||
paginationLocationQuery,
|
||||
vendorsTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const vendorsPageSelector = (state, props, query) => {
|
||||
const viewId = state.vendors.currentViewId;
|
||||
return state.vendors.views?.[viewId]?.pages?.[query.page];
|
||||
};
|
||||
|
||||
const vendorsItemsSelector = (state) => state.vendors.items;
|
||||
|
||||
export const getVendorCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
vendorsPageSelector,
|
||||
vendorsItemsSelector,
|
||||
(vendorPage, vendorItems) => {
|
||||
return typeof vendorPage === 'object'
|
||||
? pickItemsFromIds(vendorItems, vendorPage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
const vendorsPaginationSelector = (state, props) => {
|
||||
const viewId = state.vendors.currentViewId;
|
||||
return state.vendors.views?.[viewId];
|
||||
};
|
||||
|
||||
export const getVendorsPaginationMetaFactory = () =>
|
||||
createSelector(vendorsPaginationSelector, (vendorPage) => {
|
||||
return vendorPage?.paginationMeta || {};
|
||||
});
|
||||
|
||||
const vendorByIdSelector = (state, props) => {
|
||||
return state.vendors.items[props.vendorId];
|
||||
};
|
||||
|
||||
export const getEstimateByIdFactory = () =>
|
||||
createSelector(vendorByIdSelector, (vendor) => {
|
||||
return vendor;
|
||||
});
|
||||
11
client/src/store/vendors/vendors.types.js
vendored
Normal file
11
client/src/store/vendors/vendors.types.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
export default {
|
||||
VENDORS_ITEMS_SET: 'VENDORS_ITEMS_SET',
|
||||
VENDOR_SET: 'VENDOR_SET',
|
||||
VENDORS_PAGE_SET: 'VENDORS_PAGE_SET',
|
||||
VENDORS_TABLE_LOADING: 'VENDORS_TABLE_LOADING',
|
||||
VENDORS_TABLE_QUERIES_ADD: 'VENDORS_TABLE_QUERIES_ADD',
|
||||
VENDOR_DELETE: 'VENDOR_DELETE',
|
||||
VENDORS_BULK_DELETE: 'VENDORS_BULK_DELETE',
|
||||
VENDORS_PAGINATION_SET: 'VENDORS_PAGINATION_SET',
|
||||
VENDORS_SET_CURRENT_VIEW: 'VENDORS_SET_CURRENT_VIEW',
|
||||
};
|
||||
Reference in New Issue
Block a user