mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: add due bills reducer and selector.
This commit is contained in:
@@ -4,12 +4,14 @@ import {
|
||||
getBillCurrentPageFactory,
|
||||
getBillPaginationMetaFactory,
|
||||
getBillTableQueryFactory,
|
||||
getVendorDueBillsFactory
|
||||
} from 'store/Bills/bills.selectors';
|
||||
|
||||
export default (mapState) => {
|
||||
const getBillsItems = getBillCurrentPageFactory();
|
||||
const getBillsPaginationMeta = getBillPaginationMetaFactory();
|
||||
const getBillTableQuery = getBillTableQueryFactory();
|
||||
const getVendorDueBills = getVendorDueBillsFactory();
|
||||
|
||||
const mapStateToProps = (state, props) => {
|
||||
const tableQuery = getBillTableQuery(state, props);
|
||||
@@ -23,6 +25,8 @@ export default (mapState) => {
|
||||
billsPageination: getBillsPaginationMeta(state, props, tableQuery),
|
||||
billsLoading: state.bills.loading,
|
||||
nextBillNumberChanged: state.bills.nextBillNumberChanged,
|
||||
|
||||
vendorDueBills: getVendorDueBills(state, props),
|
||||
};
|
||||
return mapState ? mapState(mapped, state, props) : mapped;
|
||||
};
|
||||
|
||||
@@ -117,3 +117,17 @@ export const editBill = (id, form) => {
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchDueBills = ({ vendorId }) => (dispatch) => new Promise((resolve, reject) => {
|
||||
const params = { vendor_id: vendorId };
|
||||
|
||||
ApiService.get(`purchases/bills/due`, { params }).then((response) => {
|
||||
dispatch({
|
||||
type: t.DUE_BILLS_SET,
|
||||
payload: {
|
||||
bills: response.data.bills,
|
||||
}
|
||||
});
|
||||
resolve(response);
|
||||
}).catch(error => { reject(error) });
|
||||
});
|
||||
@@ -13,6 +13,7 @@ const initialState = {
|
||||
page: 1,
|
||||
},
|
||||
nextBillNumberChanged: false,
|
||||
dueBills: {},
|
||||
};
|
||||
|
||||
const defaultBill = {
|
||||
@@ -103,6 +104,25 @@ const reducer = createReducer(initialState, {
|
||||
const { isChanged } = action.payload;
|
||||
state.nextBillNumberChanged = isChanged;
|
||||
},
|
||||
|
||||
[t.DUE_BILLS_SET]: (state, action) => {
|
||||
const { bills } = action.payload;
|
||||
|
||||
const _dueBills = { ...state.dueBills };
|
||||
const _bills = { ...state.items };
|
||||
|
||||
bills.forEach((bill) => {
|
||||
_bills[bill.id] = { ...bill };
|
||||
|
||||
if (!_dueBills[bill.vendor_id]) {
|
||||
_dueBills[bill.vendor_id] = []
|
||||
}
|
||||
_dueBills[bill.vendor_id].push(bill.id);
|
||||
});
|
||||
|
||||
state.items = { ..._bills };
|
||||
state.dueBills = { ..._dueBills };
|
||||
}
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('bills', reducer);
|
||||
|
||||
@@ -9,8 +9,18 @@ const billPageSelector = (state, props, query) => {
|
||||
};
|
||||
const billItemsSelector = (state) => state.bills.items;
|
||||
|
||||
/**
|
||||
* Retrieve bill details.
|
||||
* @return {IBill}
|
||||
*/
|
||||
const billByIdSelector = (state, props) => state.bills.items[props.billId];
|
||||
|
||||
/**
|
||||
* Retrieve vendor due bills ids.
|
||||
* @return {number[]}
|
||||
*/
|
||||
const billsDueVendorSelector = (state, props) => state.bills.dueBills[props.vendorId];
|
||||
|
||||
const billPaginationSelector = (state, props) => {
|
||||
const viewId = state.bills.currentViewId;
|
||||
return state.bills.views?.[viewId];
|
||||
@@ -51,3 +61,17 @@ export const getBillPaginationMetaFactory = () =>
|
||||
createSelector(billPaginationSelector, (billPage) => {
|
||||
return billPage?.paginationMeta || {};
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieve due bills of specific vendor.
|
||||
*/
|
||||
export const getVendorDueBillsFactory = () =>
|
||||
createSelector(
|
||||
billItemsSelector,
|
||||
billsDueVendorSelector,
|
||||
(billsItems, dueBillsIds) => {
|
||||
return Array.isArray(dueBillsIds)
|
||||
? pickItemsFromIds(billsItems, dueBillsIds) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
@@ -10,4 +10,5 @@ export default {
|
||||
BILLS_PAGE_SET: 'BILLS_PAGE_SET',
|
||||
BILLS_ITEMS_SET: 'BILLS_ITEMS_SET',
|
||||
BILL_NUMBER_CHANGED: 'BILL_NUMBER_CHANGED',
|
||||
DUE_BILLS_SET: 'DUE_BILLS_SET'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user