Skip to content

Commit

Permalink
OM-148: fetch vouchers on eu change
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 committed Mar 28, 2024
1 parent 151915c commit 4297920
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
56 changes: 48 additions & 8 deletions src/components/VoucherSearcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import { useSelector, connect } from 'react-redux';
import { bindActionCreators } from 'redux';

Expand Down Expand Up @@ -35,7 +35,9 @@ function VoucherSearcher({ downloadWorkerVoucher, fetchWorkerVouchers, clearWork
workerVoucherExport,
errorWorkerVoucherExport,
} = useSelector((state) => state.workerVoucher);
const { economicUnit } = useSelector((state) => state.policyHolder);
const [failedExport, setFailedExport] = useState(false);
const [queryParams, setQueryParams] = useState([]);
const exportConfiguration = {
exportFields: ['code', 'policyholder', 'insuree', 'status'],
exportFieldsColumns: {
Expand All @@ -46,13 +48,22 @@ function VoucherSearcher({ downloadWorkerVoucher, fetchWorkerVouchers, clearWork
},
};

const fetchVouchers = (params) => {
try {
fetchWorkerVouchers(modulesManager, params);
} catch (error) {
throw new Error(`[VOUCHER_SEARCHER]: Fetching vouchers failed. ${error}`);
}
};
const fetchVouchers = useCallback(
(params) => {
try {
const actionParams = [...params];

if (economicUnit?.code) {
actionParams.push(`policyholder_Code:"${economicUnit.code}"`);
}

fetchWorkerVouchers(modulesManager, actionParams);
} catch (error) {
throw new Error(`[VOUCHER_SEARCHER]: Fetching vouchers failed. ${error}`);
}
},
[economicUnit],
);

const headers = () => [
'workerVoucher.code',
Expand Down Expand Up @@ -121,6 +132,34 @@ function VoucherSearcher({ downloadWorkerVoucher, fetchWorkerVouchers, clearWork
<VoucherFilter filters={filters} onChangeFilters={onChangeFilters} formatMessage={formatMessage} />
);

const filtersToQueryParams = ({
filters, pageSize, beforeCursor, afterCursor, orderBy,
}) => {
const queryParams = Object.keys(filters)
.filter((f) => !!filters[f].filter)
.map((f) => filters[f].filter);
if (!beforeCursor && !afterCursor) {
queryParams.push(`first: ${pageSize}`);
}
if (afterCursor) {
queryParams.push(`after: "${afterCursor}"`);
queryParams.push(`first: ${pageSize}`);
}
if (beforeCursor) {
queryParams.push(`before: "${beforeCursor}"`);
queryParams.push(`last: ${pageSize}`);
}
if (orderBy) {
queryParams.push(`orderBy: ["${orderBy}"]`);
}
setQueryParams(queryParams);
return queryParams;
};

useEffect(() => {
fetchVouchers(queryParams);
}, [economicUnit, queryParams]);

return (
<>
<Searcher
Expand All @@ -132,6 +171,7 @@ function VoucherSearcher({ downloadWorkerVoucher, fetchWorkerVouchers, clearWork
fetchedItems={fetchedWorkerVouchers}
fetchingItems={fetchingWorkerVouchers}
errorItems={errorWorkerVouchers}
filtersToQueryParams={filtersToQueryParams}
tableTitle={formatMessageWithValues('workerVoucher.searcherResultsTitle', { workerVouchersTotalCount })}
headers={headers}
itemFormatters={itemFormatters}
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const MODULE_NAME = 'workerVoucher';
export const REF_ROUTE_WORKER_VOUCHER = 'workerVoucher.route.workerVoucher';
export const REF_ROUTE_WORKER_VOUCHERS = 'workerVoucher.route.workerVouchers';
export const REF_ROUTE_BILL = 'bill.route.bill';
export const ECONOMIC_UNIT_STORAGE_KEY = 'userEconomicUnit';

export const MPAY_BILL_URL = '/msystems/mpay_payment/';
export const BILL_PAID_STATUS = '2';
Expand Down

0 comments on commit 4297920

Please sign in to comment.