feat: datatables pagination.

This commit is contained in:
Ahmed Bouhuolia
2020-11-16 13:36:14 +02:00
parent 6d4b3164a8
commit 13dd14b9d4
76 changed files with 1160 additions and 1315 deletions

View File

@@ -1,5 +1,8 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import {
createTableQueryReducers,
viewPaginationSetReducer,
} from 'store/journalNumber.reducer';
import t from 'store/types';
@@ -9,7 +12,7 @@ const initialState = {
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
nextBillNumberChanged: false,
@@ -24,7 +27,8 @@ const defaultBill = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.BILL_SET]: (state, action) => {
const { id, bill } = action.payload;
const _bill = state.items[id] || {};
@@ -83,27 +87,6 @@ const reducer = createReducer(initialState, {
};
},
[t.BILLS_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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.BILL_NUMBER_CHANGED]: (state, action) => {
const { isChanged } = action.payload;
state.nextBillNumberChanged = isChanged;
@@ -141,7 +124,8 @@ const reducer = createReducer(initialState, {
const billsIds = bills.map((bill) => bill.id);
state.byBillPayamentId[billPaymentId] = billsIds;
}
});
},
export default createTableQueryReducers('bills', reducer);
...viewPaginationSetReducer(t.BILLS_PAGINATION_SET),
...createTableQueryReducers('BILLS'),
});

View File

@@ -1,12 +1,20 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
getCurrentPageResults,
} from 'store/selectors';
// Retreive bills table query.
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 currentView = state.bills.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
// Retreive bills items.
const billItemsSelector = (state) => state.bills.items;
@@ -15,7 +23,8 @@ const billItemsSelector = (state) => state.bills.items;
const billByIdSelector = (state, props) => state.bills.items[props.billId];
// Retrieve vendor due bills ids.
const billsPayableVendorSelector = (state, props) => state.bills.payable.byVendorId[props.vendorId];
const billsPayableVendorSelector = (state, props) =>
state.bills.payable.byVendorId[props.vendorId];
const billPaginationSelector = (state, props) => {
const viewId = state.bills.currentViewId;
@@ -58,13 +67,16 @@ export const getBillByIdFactory = () =>
*/
export const getBillPaginationMetaFactory = () =>
createSelector(billPaginationSelector, (billPage) => {
return billPage?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(billPage?.paginationMeta || {}),
};
});
/**
* Retrieve vendor payable bills.
*/
export const getVendorPayableBillsFactory = () =>
export const getVendorPayableBillsFactory = () =>
createSelector(
billItemsSelector,
billsPayableVendorSelector,
@@ -78,7 +90,7 @@ export const getVendorPayableBillsFactory = () =>
/**
* Retrieve vendor payable bills entries.
*/
export const getVendorPayableBillsEntriesFactory = () =>
export const getVendorPayableBillsEntriesFactory = () =>
createSelector(
billItemsSelector,
billsPayableVendorSelector,
@@ -94,5 +106,5 @@ export const getVendorPayableBillsEntriesFactory = () =>
id: null,
payment_amount: null,
}));
}
);
},
);

View File

@@ -4,7 +4,7 @@ export default {
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_QUERIES_ADD: 'BILLS/TABLE_QUERIES_ADD',
BILLS_TABLE_LOADING: 'BILLS_TABLE_LOADING',
BILLS_PAGINATION_SET: 'BILLS_PAGINATION_SET',
BILLS_PAGE_SET: 'BILLS_PAGE_SET',

View File

@@ -1,6 +1,8 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import { journalNumberChangedReducer } from 'store/journalNumber.reducer';
import {
journalNumberChangedReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
@@ -9,18 +11,17 @@ const initialState = {
views: {},
loading: false,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
currentViewId: -1,
};
const defaultEstimate = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.ESTIMATE_SET]: (state, action) => {
const { id, estimate } = action.payload;
const _estimate = state.items[id] || {};
@@ -102,10 +103,9 @@ const reducer = createReducer(initialState, {
},
...journalNumberChangedReducer(t.ESTIMATE_NUMBER_CHANGED),
...createTableQueryReducers('ESTIMATES'),
});
export default createTableQueryReducers('sales_estimates', reducer);
export const getEstimateById = (state, id) => {
return state.sales_estimates.items[id];
};

View File

@@ -1,5 +1,5 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import { pickItemsFromIds, paginationLocationQuery, defaultPaginationMeta } from 'store/selectors';
const estimateTableQuery = (state) => state.salesEstimates.tableQuery;
@@ -14,9 +14,12 @@ const estimateItemsSelector = (state) => state.salesEstimates.items;
const estimatesPageSelector = (state, props, query) => {
const viewId = state.salesEstimates.currentViewId;
return state.salesEstimates.views?.[viewId]?.pages?.[query.page];
const currentPageId = state.salesEstimates.views?.[viewId]?.paginationMeta?.page;
return state.salesEstimates.views?.[viewId]?.pages?.[currentPageId];
};
// Retrieve estimates table query.
export const getEstimatesTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
@@ -29,6 +32,7 @@ export const getEstimatesTableQueryFactory = () =>
},
);
// Retreive estimate results of the current page.
export const getEstimateCurrentPageFactory = () =>
createSelector(
estimatesPageSelector,
@@ -40,12 +44,17 @@ export const getEstimateCurrentPageFactory = () =>
},
);
// 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 estimateView?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(estimateView?.paginationMeta || {}),
};
});

View File

@@ -3,8 +3,8 @@ export default {
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_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',

View File

@@ -1,7 +1,9 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import { journalNumberChangedReducer } from 'store/journalNumber.reducer';
import {
journalNumberChangedReducer,
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
const initialState = {
@@ -10,7 +12,7 @@ const initialState = {
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
dueInvoices: {},
@@ -25,7 +27,7 @@ const defaultInvoice = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.INVOICE_SET]: (state, action) => {
const { id, sale_invoice } = action.payload;
const _invoice = state.items[id] || {};
@@ -82,28 +84,6 @@ const reducer = createReducer(initialState, {
};
},
[t.INVOICES_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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.INVOICES_RECEIVABLE_BY_PAYMENT_ID]: (state, action) => {
const { paymentReceiveId, saleInvoices } = action.payload;
const saleInvoicesIds = saleInvoices.map((saleInvoice) => saleInvoice.id);
@@ -126,10 +106,10 @@ const reducer = createReducer(initialState, {
},
...journalNumberChangedReducer(t.INVOICE_NUMBER_CHANGED),
...viewPaginationSetReducer(t.INVOICES_PAGINATION_SET),
...createTableQueryReducers('INVOICES'),
});
export default createTableQueryReducers('sales_invoices', reducer);
export const getInvoiceById = (state, id) => {
return state.sales_invoices.items[id];
};

View File

@@ -2,7 +2,7 @@ import { createSelector } from '@reduxjs/toolkit';
import {
pickItemsFromIds,
paginationLocationQuery,
getItemById,
defaultPaginationMeta,
} from 'store/selectors';
const invoiceTableQuery = (state) => state.salesInvoices.tableQuery;
@@ -17,10 +17,15 @@ const invoicesPaginationSelector = (state, props) => {
const invoicesPageSelector = (state, props, query) => {
const viewId = state.salesInvoices.currentViewId;
return state.salesInvoices.views?.[viewId]?.pages?.[query.page];
const currentView = state.salesInvoices.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const invoicesItemsSelector = (state) => state.salesInvoices.items;
const invoicesReceiableCustomerSelector = (state, props) => state.salesInvoices.receivable.byCustomerId[props.customerId];
const invoicesReceiableCustomerSelector = (state, props) =>
state.salesInvoices.receivable.byCustomerId[props.customerId];
export const getInvoiceTableQueryFactory = () =>
createSelector(
@@ -34,6 +39,7 @@ export const getInvoiceTableQueryFactory = () =>
},
);
// Retrieve invoices of the current view and view.
export const getInvoiceCurrentPageFactory = () =>
createSelector(
invoicesPageSelector,
@@ -45,24 +51,27 @@ export const getInvoiceCurrentPageFactory = () =>
},
);
// Retrieve specific invoice by the passed invoice id.
export const getInvoiecsByIdFactory = () =>
createSelector(invoicesByIdSelector, (invoice) => {
return invoice;
});
// Retrieve invoices pagination meta.
export const getInvoicePaginationMetaFactory = () =>
createSelector(invoicesPaginationSelector, (invoicePage) => {
return invoicePage?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(invoicePage?.paginationMeta || {}),
};
});
export const getCustomerReceivableInvoicesEntriesFactory = () =>
export const getCustomerReceivableInvoicesEntriesFactory = () =>
createSelector(
invoicesItemsSelector,
invoicesReceiableCustomerSelector,
(invoicesItems, customerInvoicesIds) => {
const invoicesIds = [
...(customerInvoicesIds || []),
];
const invoicesIds = [...(customerInvoicesIds || [])];
const invoices = pickItemsFromIds(invoicesItems, invoicesIds);
return invoices.map((invoice) => ({
@@ -73,4 +82,4 @@ export const getCustomerReceivableInvoicesEntriesFactory = () =>
payment_amount: 0,
}));
},
)
);

View File

@@ -4,7 +4,7 @@ export default {
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_QUERIES_ADD: 'INVOICES/TABLE_QUERIES_ADD',
INVOICES_TABLE_LOADING: 'INVOICES_TABLE_LOADING',
INVOICES_PAGINATION_SET: 'INVOICES_PAGINATION_SET',
INVOICES_PAGE_SET: 'INVOICES_PAGE_SET',

View File

@@ -1,6 +1,8 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import { omit } from 'lodash';
import {
createTableQueryReducers,
viewPaginationSetReducer,
} from 'store/journalNumber.reducer';
import t from 'store/types';
const initialState = {
@@ -9,7 +11,7 @@ const initialState = {
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
nextPaymentNumberChanged: false,
@@ -19,11 +21,12 @@ const defaultPaymentMade = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.PAYMENT_MADES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.PAYMENT_MADES_ITEMS_SET]: (state, action) => {
const { bill_payments } = action.payload;
const _bill_payments = {};
@@ -41,7 +44,7 @@ const reducer = createReducer(initialState, {
[t.PAYMENT_MADE_SET]: (state, action) => {
const { id, paymentMade } = action.payload;
const _oldPaymentMade = (state.items[id] || {});
const _oldPaymentMade = state.items[id] || {};
state.items[id] = {
...defaultPaymentMade,
@@ -58,28 +61,6 @@ const reducer = createReducer(initialState, {
}
},
[t.PAYMENT_MADES_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.PAYMENT_MADES_PAGE_SET]: (state, action) => {
const { customViewId, bill_payments, pagination } = action.payload;
@@ -97,10 +78,11 @@ const reducer = createReducer(initialState, {
};
},
[t.PAYMENT_MADES_NUMBER_CHANGED]:(state,action)=>{
[t.PAYMENT_MADES_NUMBER_CHANGED]: (state, action) => {
const { isChanged } = action.payload;
state.nextPaymentNumberChanged = isChanged
state.nextPaymentNumberChanged = isChanged;
},
}
...viewPaginationSetReducer(t.PAYMENT_MADES_PAGINATION_SET),
...createTableQueryReducers('PAYMENT_MADES'),
});
export default createTableQueryReducers('bill_payments', reducer);

View File

@@ -1,12 +1,19 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
import { transformToObject } from 'utils';
const paymentMadeTableQuery = (state) => state.paymentMades.tableQuery;
const paymentMadesPageSelector = (state, props, query) => {
const paymentMadesPageSelector = (state) => {
const viewId = state.paymentMades.currentViewId;
return state.paymentMades.views?.[viewId]?.pages?.[query.page];
const currentView = state.paymentMades.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const paymentMadesItemsSelector = (state) => state.paymentMades.items;
@@ -16,12 +23,15 @@ const PaymentMadePaginationSelector = (state, props) => {
return state.paymentMades.views?.[viewId];
};
const paymentMadeById = (state, props) => state.paymentMades.items[props.paymentMadeId];
const paymentMadeById = (state, props) =>
state.paymentMades.items[props.paymentMadeId];
const paymentMadeEntries = (state, props) => props.paymentMadeEntries;
const billsItemsSelector = (state, props) => state.bills.items;
const billsPayableByPaymentMadeSelector = (state, props) => state.bills.payable.byBillPayamentId[props.paymentMadeId];
const paymentMadeBillsSelector = (state, props) => state.bills.byBillPayamentId[props.paymentMadeId];
const billsPayableByPaymentMadeSelector = (state, props) =>
state.bills.payable.byBillPayamentId[props.paymentMadeId];
const paymentMadeBillsSelector = (state, props) =>
state.bills.byBillPayamentId[props.paymentMadeId];
export const getPaymentMadeCurrentPageFactory = () =>
createSelector(
@@ -46,8 +56,11 @@ export const getPaymentMadeTableQuery = createSelector(
);
export const getPaymentMadePaginationMetaFactory = () =>
createSelector(PaymentMadePaginationSelector, (Page) => {
return Page?.paginationMeta || {};
createSelector(PaymentMadePaginationSelector, (page) => {
return {
...defaultPaginationMeta(),
...(page?.paginationMeta || {}),
};
});
export const getPaymentMadeByIdFactory = () =>
@@ -55,39 +68,40 @@ export const getPaymentMadeByIdFactory = () =>
return payment_Made;
});
export const getPaymentMadeEntriesDataFactory = () =>
export const getPaymentMadeEntriesDataFactory = () =>
createSelector(
billsItemsSelector,
paymentMadeEntries,
(billsItems, paymentEntries) => {
return Array.isArray(paymentEntries) ?
paymentEntries.map((entry) => ({
...entry, ...(billsItems[entry.bill_id] || {}),
})) : [];
}
return Array.isArray(paymentEntries)
? paymentEntries.map((entry) => ({
...entry,
...(billsItems[entry.bill_id] || {}),
}))
: [];
},
);
export const getPaymentMadeEntriesFactory = () =>
// Retrieve payment made entries.
export const getPaymentMadeEntriesFactory = () =>
createSelector(
billsItemsSelector,
billsPayableByPaymentMadeSelector,
paymentMadeBillsSelector,
paymentMadeById,
(
billsItems,
paymentPayableBillsIds,
paymentMadeBillsIds,
paymentMade,
) => {
(billsItems, paymentPayableBillsIds, paymentMadeBillsIds, paymentMade) => {
const billsIds = [
...(paymentPayableBillsIds || []),
...(paymentMadeBillsIds || []),
];
const bills = pickItemsFromIds(billsItems, billsIds);
const billEntries = transformToObject((paymentMade?.entries || []), 'bill_id');
const billEntries = transformToObject(
paymentMade?.entries || [],
'bill_id',
);
return bills.map((bill) => {
const paymentMadeEntry = (billEntries?.[bill.id] || {});
const paymentMadeEntry = billEntries?.[bill.id] || {};
return {
...bill,
@@ -98,4 +112,4 @@ export const getPaymentMadeEntriesFactory = () =>
};
});
},
);
);

View File

@@ -3,7 +3,7 @@ export default {
PAYMENT_MADE_DELETE: 'PAYMENT_MADE_DELETE',
PAYMENT_MADE_SET: 'PAYMENT_MADE_SET',
PAYMENT_MADE_SET_CURRENT_VIEW: 'PAYMENT_MADE_SET_CURRENT_VIEW',
PAYMENT_MADE_TABLE_QUERIES_ADD: 'PAYMENT_MADE_TABLE_QUERIES_ADD',
PAYMENT_MADES_TABLE_QUERIES_ADD: 'PAYMENT_MADES/TABLE_QUERIES_ADD',
PAYMENT_MADES_TABLE_LOADING: 'PAYMENT_MADES_TABLE_LOADING',
PAYMENT_MADES_PAGE_SET: 'PAYMENT_MADES_PAGE_SET',
PAYMENT_MADES_ITEMS_SET: 'PAYMENT_MADES_ITEMS_SET',

View File

@@ -1,8 +1,10 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import { omit } from 'lodash';
import { journalNumberChangedReducer } from 'store/journalNumber.reducer';
import {
journalNumberChangedReducer,
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
const initialState = {
@@ -11,7 +13,7 @@ const initialState = {
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
};
@@ -20,7 +22,7 @@ const defaultPaymentReceive = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.PAYMENT_RECEIVE_SET]: (state, action) => {
const { id, paymentReceive } = action.payload;
@@ -71,27 +73,6 @@ const reducer = createReducer(initialState, {
}
},
[t.PAYMENT_RECEIVES_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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.PAYMENT_RECEIVES_PAGE_SET]: (state, action) => {
const { customViewId, payment_receives, pagination } = action.payload;
@@ -109,5 +90,6 @@ const reducer = createReducer(initialState, {
},
...journalNumberChangedReducer(t.PAYMENT_RECEIVE_NUMBER_CHANGED),
...viewPaginationSetReducer(t.PAYMENT_RECEIVES_PAGINATION_SET),
...createTableQueryReducers('PAYMENT_RECEIVES'),
});
export default createTableQueryReducers('payment_receives', reducer);

View File

@@ -1,10 +1,17 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
import { transformToObject } from 'utils';
const paymentReceivesPageSelector = (state, props, query) => {
const paymentReceivesPageSelector = (state) => {
const viewId = state.paymentReceives.currentViewId;
return state.paymentReceives.views?.[viewId]?.pages?.[query.page];
const viewMeta = state.paymentReceives.views?.[viewId];
const currentPageId = viewMeta?.paginationMeta?.page;
return viewMeta?.pages?.[currentPageId];
};
const paymentReceivesItemsSelector = (state) => state.paymentReceives.items;
@@ -28,17 +35,19 @@ const paymentReceiveInvoicesSelector = (state, props) =>
const paymentReceiveByIdSelector = (state, props) =>
state.paymentReceives.items[props.paymentReceiveId];
// Retrieve payment receive current page results.
export const getPaymentReceiveCurrentPageFactory = () =>
createSelector(
paymentReceivesPageSelector,
paymentReceivesItemsSelector,
(Page, Items) => {
return typeof Page === 'object'
? pickItemsFromIds(Items, Page.ids) || []
(expensesPage, expensesItems) => {
return typeof expensesPage === 'object'
? pickItemsFromIds(expensesItems, expensesPage.ids) || []
: [];
},
);
// Retrieve payment receives table fetch query.
export const getPaymentReceiveTableQuery = createSelector(
paginationLocationQuery,
paymentReceiveTableQuery,
@@ -50,16 +59,22 @@ export const getPaymentReceiveTableQuery = createSelector(
},
);
// Retrieve payment receive pagination meta.
export const getPaymentReceivePaginationMetaFactory = () =>
createSelector(PaymentReceivePaginationSelector, (Page) => {
return Page?.paginationMeta || {};
createSelector(PaymentReceivePaginationSelector, (page) => {
return {
...defaultPaginationMeta(),
...(page?.paginationMeta || {}),
};
});
// Retrieve payment receive based on the passed payment receive id.
export const getPaymentReceiveByIdFactory = () =>
createSelector(payemntReceiveById, (payment_receive) => {
return payment_receive;
});
// Retrieve the payment receive associated invoices.
export const getPaymentReceiveInvoices = createSelector(
payemntReceiveById,
invoicesItemsSelector,
@@ -73,9 +88,7 @@ export const getPaymentReceiveInvoices = createSelector(
},
);
/**
* Retrieve payment receive invoices entries.
*/
// Retrieve payment receive invoices entries.
export const getPaymentReceiveEntriesFactory = () =>
createSelector(
invoicesItemsSelector,

View File

@@ -2,12 +2,11 @@ export default {
PAYMENT_RECEIVE_LIST_SET: 'PAYMENT_RECEIVE_LIST_SET',
PAYMENT_RECEIVE_DELETE: 'PAYMENT_RECEIVE_DELETE',
PAYMENT_RECEIVE_SET: 'PAYMENT_RECEIVE_SET',
PAYMENT_RECEIVE_SET_CURRENT_VIEW: 'PAYMENT_RECEIVE_SET_CURRENT_VIEW',
PAYMENT_RECEIVE_TABLE_QUERIES_ADD: 'PAYMENT_RECEIVE_TABLE_QUERIES_ADD',
PAYMENT_RECEIVES_SET_CURRENT_VIEW: 'PAYMENT_RECEIVES_SET_CURRENT_VIEW',
PAYMENT_RECEIVES_TABLE_QUERIES_ADD: 'PAYMENT_RECEIVES/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',
PAYMENT_RECEIVE_NUMBER_CHANGED: 'PAYMENT_RECEIVE_NUMBER_CHANGED',
};

View File

@@ -1,21 +1,25 @@
import t from 'store/types';
import { snakeCase } from 'lodash';
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import {
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
// Responsible for data fetch query based on this query.
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
errors: [],
};
const customersReducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.CUSTOMER_SET]: (state, action) => {
const { id, customer } = action.payload;
const _customers = state.items[id] || {};
@@ -65,28 +69,6 @@ const customersReducer = createReducer(initialState, {
state.loading = loading;
},
[t.CUSTOMERS_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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.CUSTOMERS_BULK_DELETE]: (state, action) => {
const { ids } = action.payload;
const items = { ...state.items };
@@ -98,9 +80,10 @@ const customersReducer = createReducer(initialState, {
});
state.items = items;
},
});
export default createTableQueryReducers('customers', customersReducer);
...viewPaginationSetReducer(t.CUSTOMERS_PAGINATION_SET),
...createTableQueryReducers('CUSTOMERS'),
});
export const getCustomerById = (state, id) => {
return state.customers.items[id];

View File

@@ -1,5 +1,9 @@
import { createSelector } from 'reselect';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
const customerTableQuery = (state) => state.customers.tableQuery;
@@ -12,9 +16,12 @@ const customersPaginationSelector = (state, props) => {
return state.customers.views?.[viewId];
};
const customerPageSelector = (state, props, query) => {
const customerPageSelector = (state, props) => {
const viewId = state.customers.currentViewId;
return state.customers.views?.[viewId]?.pages?.[query.page];
const currentView = state.customers.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const customersItemsSelector = (state) => state.customers.items;
@@ -49,6 +56,8 @@ export const getCustomersByIdFactory = () =>
export const getCustomerPaginationMetaFactory = () =>
createSelector(customersPaginationSelector, (customerPage) => {
return customerPage?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(customerPage?.paginationMeta || {}),
};
});

View File

@@ -3,7 +3,7 @@ export default {
CUSTOMER_SET: 'CUSTOMER_SET',
CUSTOMERS_PAGE_SET: 'CUSTOMERS_PAGE_SET',
CUSTOMERS_TABLE_LOADING: 'CUSTOMERS_TABLE_LOADING',
CUSTOMERS_TABLE_QUERIES_ADD: 'CUSTOMERS_TABLE_QUERIES_ADD',
CUSTOMERS_TABLE_QUERIES_ADD: 'CUSTOMERS/TABLE_QUERIES_ADD',
CUSTOMER_DELETE: 'CUSTOMER_DELETE',
CUSTOMERS_BULK_DELETE: 'CUSTOMERS_BULK_DELETE',
CUSTOMERS_PAGINATION_SET: 'CUSTOMERS_PAGINATION_SET',

View File

@@ -1,5 +1,8 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import {
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
@@ -7,6 +10,7 @@ const initialState = {
items: {},
views: {},
loading: false,
//
tableQuery: {
page_size: 12,
page: 1,
@@ -18,7 +22,7 @@ const defaultExpense = {
categories: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.EXPENSE_SET]: (state, action) => {
const { id, expense } = action.payload;
const oldExpense = state.items[id] || {};
@@ -66,29 +70,6 @@ const reducer = createReducer(initialState, {
};
},
[t.EXPENSES_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.EXPENSES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
@@ -117,9 +98,10 @@ const reducer = createReducer(initialState, {
});
state.items = items;
},
});
export default createTableQueryReducers('expenses', reducer);
...viewPaginationSetReducer(t.EXPENSES_PAGINATION_SET),
...createTableQueryReducers('EXPENSES'),
});
export const getExpenseById = (state, id) => {
return state.expenses.items[id];

View File

@@ -1,8 +1,29 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const expensesTableQuery = state => state.expenses.tableQuery;
const expensesTableQuery = (state) => state.expenses.tableQuery;
const getPageExpensesQuery = (state, props) => {
const currentPageId = state.expenses.views?.[props.viewId]?.paginationMeta?.page;
return currentPageId || 0;
};
const expensesPageSelector = (state, props, query) => {
const viewId = state.expenses.currentViewId;
const currentPageId = getPageExpensesQuery(state, { viewId });
return state.expenses.views?.[viewId]?.pages?.[currentPageId];
};
const expensesItemsSelector = (state) => state.expenses.items;
const expenseByIdSelector = (state, props) => state.expenses.items[props.expenseId];
const manualJournalsPaginationSelector = (state, props) => {
const viewId = state.expenses.currentViewId;
return state.expenses.views?.[viewId];
};
// Retrive expenses table query.
export const getExpensesTableQuery = createSelector(
paginationLocationQuery,
expensesTableQuery,
@@ -14,13 +35,7 @@ export const getExpensesTableQuery = createSelector(
},
);
const expensesPageSelector = (state, props, query) => {
const viewId = state.expenses.currentViewId;
return state.expenses.views?.[viewId]?.pages?.[query.page];
};
const expensesItemsSelector = (state) => state.expenses.items;
// Retrieve expenses results of the current page.
export const getExpensesCurrentPageFactory = () => createSelector(
expensesPageSelector,
expensesItemsSelector,
@@ -31,8 +46,7 @@ export const getExpensesCurrentPageFactory = () => createSelector(
},
);
const expenseByIdSelector = (state, props) => state.expenses.items[props.expenseId];
// Retrieve specific expense by the passed expense id.
export const getExpenseByIdFactory = () => createSelector(
expenseByIdSelector,
(expense) => {
@@ -40,11 +54,7 @@ export const getExpenseByIdFactory = () => createSelector(
}
);
const manualJournalsPaginationSelector = (state, props) => {
const viewId = state.expenses.currentViewId;
return state.expenses.views?.[viewId];
};
// Retrieve expenses pagination meta.
export const getExpensesPaginationMetaFactory = () => createSelector(
manualJournalsPaginationSelector,
(expensesPage) => {

View File

@@ -4,7 +4,7 @@ export default {
EXPENSE_DELETE: 'EXPENSE_DELETE',
EXPENSES_BULK_DELETE: 'EXPENSES_BULK_DELETE',
EXPENSES_SET_CURRENT_VIEW: 'EXPENSES_SET_CURRENT_VIEW',
EXPENSES_TABLE_QUERIES_ADD:'EXPENSES_TABLE_QUERIES_ADD',
EXPENSES_TABLE_QUERIES_ADD:'EXPENSES/TABLE_QUERIES_ADD',
EXPENSE_PUBLISH: 'EXPENSE_PUBLISH',
EXPENSES_TABLE_LOADING: 'EXPENSES_TABLE_LOADING',
EXPENSES_PAGE_SET: 'EXPENSES_PAGE_SET',

View File

@@ -30,13 +30,17 @@ export const fetchItems = ({ query }) => {
customViewId: response.data?.filter_meta?.view?.custom_view_id,
paginationMeta: response.data.pagination,
});
dispatch({
type: t.ITEMS_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
}
})
dispatch({
type: t.ITEMS_TABLE_LOADING,
payload: { loading: false },
});
dispatch({
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
});
resolve(response);
})
.catch((error) => {

View File

@@ -1,7 +1,10 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
import { getItemsViewPages } from 'store/items/items.selectors';
import { createTableQueryReducers } from 'store/queryReducers';
import {
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
const initialState = {
items: {},
@@ -9,12 +12,15 @@ const initialState = {
itemsRelation: {},
currentPage: 1,
currentViewId: -1,
tableQuery: {},
bulkActions: {},
loading: false,
tableQuery: {
page_size: 12,
page: 1,
},
};
const itemsReducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.ITEMS_SET]: (state, action) => {
const _items = {};
@@ -111,9 +117,10 @@ const itemsReducer = createReducer(initialState, {
});
state.items = items;
},
});
export default createTableQueryReducers('items', itemsReducer);
...viewPaginationSetReducer(t.ITEMS_PAGINATION_SET),
...createTableQueryReducers('ITEMS'),
});
export const getItemById = (state, id) => {
return state.items.items[id];

View File

@@ -2,12 +2,13 @@ export default {
ITEMS_SET: 'ITEMS_SET',
ITEM_SET: 'ITEM_SET',
ITEMS_PAGE_SET: 'ITEMS_PAGE_SET',
ITEMS_PAGINATION_SET: 'ITEMS_PAGINATION_SET',
ITEM_DELETE: 'ITEM_DELETE',
ITEM_BULK_ACTION_ADD: 'ITEM_BULK_ACTION_ADD',
ITEM_BULK_ACTION_REMOVE: 'ITEM_BULK_ACTION_REMOVE',
ITEMS_TABLE_QUERY_SET: 'ITEMS_TABLE_QUERY_SET',
ITEMS_TABLE_QUERIES_ADD: 'ITEMS_TABLE_QUERIES_ADD',
ITEMS_TABLE_QUERY_SET: 'ITEMS/TABLE_QUERY_SET',
ITEMS_TABLE_QUERIES_ADD: 'ITEMS/TABLE_QUERIES_ADD',
ITEMS_TABLE_LOADING: 'ITEMS_TABLE_LOADING',
ITEMS_SET_CURRENT_VIEW: 'ITEMS_SET_CURRENT_VIEW',

View File

@@ -4,4 +4,44 @@ export const journalNumberChangedReducer = (type) => ({
const { isChanged } = action.payload;
state.journalNumberChanged = isChanged;
},
});
export const viewPaginationSetReducer = (type) => ({
[type]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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 const createTableQueryReducers = (RESOURCE_NAME) => ({
[`${RESOURCE_NAME}/TABLE_QUERY_SET`]: (state, action) => {
state.tableQuery = {
...state.tableQuery,
[state.key]: action.payload.value,
};
},
[`${RESOURCE_NAME}/TABLE_QUERIES_ADD`]: (state, action) => {
state.tableQuery = {
...state.tableQuery,
...action.payload.queries,
};
},
});

View File

@@ -138,7 +138,7 @@ export const fetchManualJournalsTable = ({ query } = {}) => {
manual_journals: response.data.manual_journals,
});
dispatch({
type: 'MANUAL_JOURNALS_PAGINATION_SET',
type: t.MANUAL_JOURNALS_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId:

View File

@@ -1,8 +1,11 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import { omit } from 'lodash';
import { journalNumberChangedReducer } from 'store/journalNumber.reducer';
import {
journalNumberChangedReducer,
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
const initialState = {
items: {},
@@ -10,7 +13,7 @@ const initialState = {
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 6,
page_size: 12,
page: 1,
},
paginationMeta: {
@@ -23,8 +26,7 @@ const defaultJournal = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.MANUAL_JOURNAL_SET]: (state, action) => {
const { id, manualJournal } = action.payload;
state.items[id] = { ...defaultJournal, ...manualJournal };
@@ -32,7 +34,7 @@ const reducer = createReducer(initialState, {
[t.MANUAL_JOURNAL_PUBLISH]: (state, action) => {
const { id } = action.payload;
const item = state.items[id] || {}
const item = state.items[id] || {};
state.items[id] = { ...item, status: 1 };
},
@@ -57,15 +59,15 @@ const reducer = createReducer(initialState, {
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: (manualJournals.map((i) => i.id)),
ids: manualJournals.map((i) => i.id),
},
}
},
};
},
@@ -95,34 +97,11 @@ const reducer = createReducer(initialState, {
state.items = items;
},
[t.MANUAL_JOURNALS_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,
},
};
},
...viewPaginationSetReducer(t.MANUAL_JOURNALS_PAGINATION_SET),
...journalNumberChangedReducer(t.MANUAL_JOURNAL_NUMBER_CHANGED),
...createTableQueryReducers('MANUAL_JOURNALS'),
});
export default createTableQueryReducers('manual_journals', reducer);
export const getManualJournal = (state, id) => {
return state.manualJournals.items[id];
}
};

View File

@@ -1,9 +1,12 @@
import { createSelector } from 'reselect';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
import { pickItemsFromIds, paginationLocationQuery, defaultPaginationMeta } from 'store/selectors';
const manualJournalsPageSelector = (state, props, query) => {
const manualJournalsPageSelector = (state) => {
const viewId = state.manualJournals.currentViewId;
return state.manualJournals.views?.[viewId]?.pages?.[query.page];
const currentView = state.manualJournals.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const manualJournalsPaginationSelector = (state, props) => {
@@ -15,6 +18,7 @@ const manualJournalsTableQuery = (state) => state.manualJournals.tableQuery;
const manualJournalsDataSelector = (state) => state.manualJournals.items;
// Retrieve manual jounral current page results.
export const getManualJournalsItems = createSelector(
manualJournalsPageSelector,
manualJournalsDataSelector,
@@ -25,13 +29,18 @@ export const getManualJournalsItems = createSelector(
},
);
// Retrieve manual journals pagination meta.
export const getManualJournalsPagination = createSelector(
manualJournalsPaginationSelector,
(manualJournalsPage) => {
return manualJournalsPage?.paginationMeta || {};
return {
...defaultPaginationMeta(),
...(manualJournalsPage?.paginationMeta || {}),
};
},
);
// Retrieve manual jouranls table query.
export const getManualJournalsTableQuery = createSelector(
paginationLocationQuery,
manualJournalsTableQuery,

View File

@@ -6,7 +6,7 @@ export default {
MANUAL_JOURNALS_PAGE_SET: 'MANUAL_JOURNALS_PAGE_SET',
MANUAL_JOURNALS_ITEMS_SET: 'MANUAL_JOURNALS_ITEMS_SET',
MANUAL_JOURNALS_SET_CURRENT_VIEW: 'MANUAL_JOURNALS_SET_CURRENT_VIEW',
MANUAL_JOURNALS_TABLE_QUERIES_ADD: 'MANUAL_JOURNALS_TABLE_QUERIES_ADD',
MANUAL_JOURNALS_TABLE_QUERIES_ADD: 'MANUAL_JOURNALS/TABLE_QUERIES_ADD',
MANUAL_JOURNAL_REMOVE: 'MANUAL_JOURNAL_REMOVE',
MANUAL_JOURNAL_PUBLISH: 'MANUAL_JOURNAL_PUBLISH',

View File

@@ -1,27 +0,0 @@
const pages = (pages, action) => {
const { type, items, meta } = action;
switch(type) {
case REQUEST_PAGE:
return {
...pages,
[meta.currentPage]: {
...pages[meta.currentPage],
ids: [],
fetching: true,
},
};
case RECEIVE_PAGE:
return {
...pages,
[meta.currentPage]: {
...pages[meta.currentPage],
ids: items.map(i => i.id),
fetching: false,
},
};
}
};

View File

@@ -6,7 +6,7 @@ export const createTableQueryReducers =
const RESOURCE_NAME = resourceName.toUpperCase();
switch (action.type) {
case `${RESOURCE_NAME}_TABLE_QUERY_SET`:
case `${RESOURCE_NAME}/TABLE_QUERY_SET`:
return {
...state,
tableQuery: {
@@ -14,15 +14,11 @@ export const createTableQueryReducers =
[state.key]: state.value,
}
};
case `${RESOURCE_NAME}_TABLE_QUERIES_ADD`:
case `${RESOURCE_NAME}/TABLE_QUERIES_ADD`:
return {
...state,
tableQuery: {
...state.tableQuery,
...action.queries
},
};
default:
return reducer(state, action);
}
}
}

View File

@@ -1,14 +1,13 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import t from 'store/types';
import { journalNumberChangedReducer } from 'store/journalNumber.reducer';
import { journalNumberChangedReducer, createTableQueryReducers } from 'store/journalNumber.reducer';
const initialState = {
items: {},
views: {},
loading: false,
tableQuery: {
page_size: 5,
page_size: 12,
page: 1,
},
currentViewId: -1,
@@ -18,7 +17,7 @@ const defaultReceipt = {
entries: [],
};
const reducer = createReducer(initialState, {
export default createReducer(initialState, {
[t.RECEIPT_SET]: (state, action) => {
const { id, sale_receipt } = action.payload;
const _receipt = state.items[id] || {};
@@ -94,10 +93,9 @@ const reducer = createReducer(initialState, {
};
},
...journalNumberChangedReducer(t.RECEIPT_NUMBER_CHANGED),
...createTableQueryReducers('RECEIPTS'),
});
export default createTableQueryReducers('sales_receipts', reducer);
export const getReceiptById = (state, id) => {
return state.receipts.items[id];
};

View File

@@ -1,9 +1,12 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const receiptsPageSelector = (state, props, query) => {
const receiptsPageSelector = (state) => {
const viewId = state.salesReceipts.currentViewId;
return state.salesReceipts.views?.[viewId]?.pages?.[query.page];
const currentView = state.salesReceipts.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const receiptsPaginationSelector = (state, props) => {
@@ -17,7 +20,7 @@ const receiptTableQuery = (state) => state.salesReceipts.tableQuery;
const receiptByIdSelector = (state, props) => state.salesReceipts.items[props.receiptId];
// Retrieve current page sale receipts results.
export const getReceiptCurrentPageFactory = () =>
createSelector(
receiptsPageSelector,
@@ -29,6 +32,7 @@ export const getReceiptCurrentPageFactory = () =>
},
);
// Retrieve receipts table query.
export const getReceiptsTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
@@ -41,11 +45,13 @@ export const getReceiptsTableQueryFactory = () =>
},
);
// Retrieve specific receipts by the passed receipt id.
export const getReceiptByIdFactory = () =>
createSelector(receiptByIdSelector, (receipt) => {
return receipt;
});
// Retrieve receipts pagination meta.
export const getReceiptsPaginationMetaFactory = () =>
createSelector(receiptsPaginationSelector, (receiptPage) => {
return receiptPage?.paginationMeta || {};

View File

@@ -4,7 +4,7 @@ export default {
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_QUERIES_ADD: 'RECEIPTS/TABLE_QUERIES_ADD',
RECEIPTS_TABLE_LOADING: 'RECEIPTS_TABLE_LOADING',
RECEIPTS_PAGINATION_SET: 'RECEIPTS_PAGINATION_SET',
RECEIPTS_PAGE_SET: 'RECEIPTS_PAGE_SET',

View File

@@ -47,4 +47,12 @@ export const paginationLocationQuery = (state, props) => {
parseInt(v, 10),
)
: null;
}
}
export const defaultPaginationMeta = () => ({
pageSize: 0,
page: 1,
total: 0,
pagesCount: 0,
pageIndex: 0,
})