Skip to content

Commit 6c5f85e

Browse files
authored
fix: [EBE-574] fixed pagination and filters behavior (#358)
1 parent 89143a0 commit 6c5f85e

File tree

3 files changed

+100
-118
lines changed

3 files changed

+100
-118
lines changed

src/pages/refundRequests/__tests__/invoiceDataTable.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ describe('InvoiceDataTable', () => {
163163
expect(mockedGetTransactions).toHaveBeenCalledWith(
164164
expect.objectContaining({
165165
initiativeId: 'initiative-123',
166-
page: 0,
167166
size: 10,
168167
})
169168
);
@@ -185,7 +184,6 @@ describe('InvoiceDataTable', () => {
185184
expect(mockedGetTransactions).toHaveBeenCalledWith(
186185
expect.objectContaining({
187186
initiativeId: 'initiative-123',
188-
page: 0,
189187
size: 10,
190188
rewardBatchId: 'batch-1',
191189
rewardBatchTrxStatus: 'ELIGIBLE',
@@ -225,6 +223,7 @@ describe('InvoiceDataTable', () => {
225223
const clearSortButton = screen.getByTestId('clear-sort');
226224
fireEvent.click(clearSortButton);
227225
await waitFor(() => expect(mockedGetTransactions).toHaveBeenCalledTimes(2));
226+
// await waitFor(() => expect(mockedGetTransactions).toHaveBeenCalledWith({initiativeId: "initiative-123", size: 10}));
228227
const secondCallArgs = mockedGetTransactions.mock.calls[1][0];
229228
expect(secondCallArgs.sort).toBeUndefined();
230229
});

src/pages/refundRequests/invoiceDataTable.tsx

Lines changed: 87 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import StatusChipInvoice from '../../components/Chip/StatusChipInvoice';
1616
import {
1717
downloadInvoiceFile,
1818
getMerchantTransactionsProcessed,
19+
GetMerchantTransactionsProcessedParams,
1920
} from '../../services/merchantService';
2021
import { MISSING_DATA_PLACEHOLDER, TYPE_TEXT } from '../../utils/constants';
2122
import { safeFormatDate } from '../../utils/formatUtils';
@@ -56,13 +57,7 @@ const InvoiceDataTable = ({
5657
trxCode,
5758
fiscalCode,
5859
}: InvoiceDataTableProps) => {
59-
const [transactions, setTransactions] = useState<MerchantTransactionsListDTO>({
60-
content: [],
61-
pageNo: 0,
62-
pageSize: 10,
63-
totalElements: 0,
64-
totalPages: 0,
65-
});
60+
const [transactions, setTransactions] = useState<MerchantTransactionsListDTO["content"]>([]);
6661
const [pagination, setPagination] = useState({
6762
pageNo: 0,
6863
pageSize: 10,
@@ -88,21 +83,6 @@ const InvoiceDataTable = ({
8883
setDrawerOpened(false);
8984
};
9085

91-
const handleSortModelChange = (model: GridSortModel) => {
92-
if (
93-
model.length === 0 ||
94-
(model.length === 1 &&
95-
model[0].field === 'trxChargeDate' &&
96-
(model[0].sort === 'asc' || model[0].sort === 'desc'))
97-
) {
98-
setSortModel(model);
99-
}
100-
};
101-
102-
const handlePaginationPageChange = (page: number) => {
103-
setPagination((prev) => ({ ...prev, pageNo: page }));
104-
};
105-
10686
const downloadFile = async (selectedTransaction: any) => {
10787
try {
10888
setIsDownloading(true);
@@ -157,53 +137,30 @@ const InvoiceDataTable = ({
157137
}
158138
};
159139

160-
const loadTransactions = () => {
140+
const loadTransactions = (params?: Omit<GetMerchantTransactionsProcessedParams, "initiativeId">) => {
161141
setLoading(true);
162-
163-
let sortParam: string | undefined;
164-
if (
165-
sortModel.length === 1 &&
166-
sortModel[0].field === 'trxChargeDate' &&
167-
(sortModel[0].sort === 'asc' || sortModel[0].sort === 'desc')
168-
) {
169-
sortParam = `trxChargeDate,${sortModel[0].sort}`;
170-
}
171-
172-
const params = {
173-
initiativeId: id,
174-
page: pagination.pageNo,
175-
size: pagination.pageSize,
176-
...(sortParam ? { sort: sortParam } : {}),
142+
const filters = {
177143
...(fiscalCode ? { fiscalCode } : {}),
178144
...(batchId ? { rewardBatchId: batchId } : {}),
179145
...(rewardBatchTrxStatus ? { rewardBatchTrxStatus } : {}),
180146
...(pointOfSaleId ? { pointOfSaleId } : {}),
181147
...(trxCode ? { trxCode } : {}),
182148
};
183-
184-
getMerchantTransactionsProcessed(params)
185-
.then((data) => {
186-
setTransactions(data);
187-
setPagination({
188-
pageNo: data.pageNo,
189-
pageSize: data.pageSize,
190-
totalElements: data.totalElements,
191-
});
192-
})
193-
.finally(() => setLoading(false));
149+
getMerchantTransactionsProcessed({ initiativeId: id, size: pagination.pageSize, ...filters, ...params })
150+
.then(response => {
151+
const { content, ...paginationData } = response;
152+
setPagination(paginationData);
153+
setTransactions([...content]);
154+
}).finally(() => setLoading(false));
194155
};
195156

196-
197157
useEffect(() => {
198158
loadTransactions();
199159
}, [
200-
pagination.pageNo,
201-
pagination.pageSize,
202160
batchId,
203161
rewardBatchTrxStatus,
204162
pointOfSaleId,
205163
trxCode,
206-
sortModel,
207164
fiscalCode,
208165
]);
209166

@@ -300,15 +257,33 @@ const InvoiceDataTable = ({
300257
},
301258
];
302259

260+
const handleSortModelChange = (model: GridSortModel) => {
261+
if (
262+
model.length === 0 ||
263+
(model.length === 1 &&
264+
model[0].field === 'trxChargeDate' &&
265+
(model[0].sort === 'asc' || model[0].sort === 'desc'))
266+
) {
267+
setSortModel(model);
268+
loadTransactions({ ...(model[0]?.sort ? { sort: `trxChargeDate,${model[0].sort}` } : {}) });
269+
}
270+
};
271+
272+
const handlePaginationPageChange = (page: number) => {
273+
setPagination((prev) => ({ ...prev, page }));
274+
loadTransactions({ page });
275+
};
276+
303277
const handleRowsPerPageChange = (newPageSize: number) => {
304278
setPagination((prev) => ({
305279
...prev,
306280
pageNo: 0,
307281
pageSize: newPageSize,
308282
}));
283+
loadTransactions({ page: 0, size: newPageSize });
309284
};
310285

311-
const tableRows = transactions.content.map((row: any) => ({
286+
const tableRows = transactions.map((row: any) => ({
312287
...row,
313288
id: row.trxId,
314289
invoiceFilename: row.invoiceData?.filename || '',
@@ -338,11 +313,7 @@ const InvoiceDataTable = ({
338313
rows={tableRows}
339314
columns={columns}
340315
rowsPerPage={pagination.pageSize}
341-
paginationModel={{
342-
pageNo: pagination.pageNo,
343-
pageSize: pagination.pageSize,
344-
totalElements: pagination.totalElements,
345-
}}
316+
paginationModel={pagination}
346317
onPaginationPageChange={handlePaginationPageChange}
347318
sortModel={sortModel}
348319
onSortModelChange={handleSortModelChange}
@@ -362,7 +333,7 @@ const InvoiceDataTable = ({
362333
}}
363334
/>
364335
)}
365-
{!loading && transactions.content.length === 0 && (
336+
{!loading && transactions.length === 0 && (
366337
<Paper
367338
sx={{
368339
my: 4,
@@ -376,62 +347,62 @@ const InvoiceDataTable = ({
376347
<Typography variant="body2">Nessuna richiesta di rimborso trovata.</Typography>
377348
</Paper>
378349
)}
379-
{rowDetail && (
380-
<InvoiceDetail
381-
isOpen={drawerOpened}
382-
setIsOpen={handleToggleDrawer}
383-
batchId={batchId ?? ''}
384-
onSuccess={loadTransactions}
385-
title="Dettaglio transazione"
386-
itemValues={rowDetail}
387-
storeId={rowDetail?.pointOfSaleId || ''}
388-
listItem={[
389-
{
390-
label: 'Data e ora',
391-
id: 'trxChargeDate',
392-
type: TYPE_TEXT.Text,
393-
format: (val: any) => safeFormatDate(val),
394-
},
395-
{
396-
label: 'Elettrodomestico',
397-
id: 'additionalProperties.productName',
398-
type: TYPE_TEXT.Text,
399-
},
400-
{
401-
label: 'Codice Fiscale Beneficiario',
402-
id: 'fiscalCode',
403-
type: TYPE_TEXT.Text,
404-
},
405-
{
406-
label: 'ID transazione',
407-
id: 'trxId',
408-
type: TYPE_TEXT.Text,
409-
bold: true,
410-
},
411-
{
412-
label: 'Codice sconto',
413-
id: 'trxCode',
414-
type: TYPE_TEXT.Text,
415-
},
416-
{
417-
label: 'Totale della spesa',
418-
id: 'effectiveAmountCents',
419-
type: TYPE_TEXT.Currency,
420-
bold: true,
421-
},
422-
{
423-
label: 'Sconto applicato',
424-
id: 'rewardAmountCents',
425-
type: TYPE_TEXT.Currency,
426-
},
427-
{
428-
label: 'Importo autorizzato',
429-
id: 'authorizedAmountCents',
430-
type: TYPE_TEXT.Currency,
431-
},
432-
]}
433-
/>
434-
)}
350+
{rowDetail && (
351+
<InvoiceDetail
352+
isOpen={drawerOpened}
353+
setIsOpen={handleToggleDrawer}
354+
batchId={batchId ?? ''}
355+
onSuccess={loadTransactions}
356+
title="Dettaglio transazione"
357+
itemValues={rowDetail}
358+
storeId={rowDetail?.pointOfSaleId || ''}
359+
listItem={[
360+
{
361+
label: 'Data e ora',
362+
id: 'trxChargeDate',
363+
type: TYPE_TEXT.Text,
364+
format: (val: any) => safeFormatDate(val),
365+
},
366+
{
367+
label: 'Elettrodomestico',
368+
id: 'additionalProperties.productName',
369+
type: TYPE_TEXT.Text,
370+
},
371+
{
372+
label: 'Codice Fiscale Beneficiario',
373+
id: 'fiscalCode',
374+
type: TYPE_TEXT.Text,
375+
},
376+
{
377+
label: 'ID transazione',
378+
id: 'trxId',
379+
type: TYPE_TEXT.Text,
380+
bold: true,
381+
},
382+
{
383+
label: 'Codice sconto',
384+
id: 'trxCode',
385+
type: TYPE_TEXT.Text,
386+
},
387+
{
388+
label: 'Totale della spesa',
389+
id: 'effectiveAmountCents',
390+
type: TYPE_TEXT.Currency,
391+
bold: true,
392+
},
393+
{
394+
label: 'Sconto applicato',
395+
id: 'rewardAmountCents',
396+
type: TYPE_TEXT.Currency,
397+
},
398+
{
399+
label: 'Importo autorizzato',
400+
id: 'authorizedAmountCents',
401+
type: TYPE_TEXT.Currency,
402+
},
403+
]}
404+
/>
405+
)}
435406
</Box>
436407
);
437408
};

src/utils/__tests__/formatUtils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('safeFormatDate', () => {
8686
expect(result).toBe('15/01/2024 10:30');
8787
});
8888

89+
// test('should throw error', () => {
90+
// const error = {
91+
// toString: () => {
92+
// throw new Error();
93+
// }
94+
// };
95+
96+
// const result = safeFormatDate(error);
97+
98+
// expect(result).toBe(MISSING_DATA_PLACEHOLDER);
99+
// });
100+
89101
test('should return MISSING_DATA_PLACEHOLDER for invalid date', () => {
90102
const result = safeFormatDate('invalid-date');
91103

0 commit comments

Comments
 (0)