Skip to content

Commit 705b52b

Browse files
committed
fix: keep selection state across page navigation
Signed-off-by: Rakib Ansary <rakibansary@topcoder.com>
1 parent d7bd18b commit 705b52b

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

src/apps/wallet/src/home/tabs/winnings/WinningsTab.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import PaymentsTable from '../../../lib/components/payments-table/PaymentTable'
1616

1717
import styles from './Winnings.module.scss'
1818

19-
type PaymentId = { [paymentId: string]: boolean }
20-
2119
interface ListViewProps {
2220
profile: UserProfile
2321
}
@@ -73,6 +71,7 @@ const formatCurrency = (amountStr: string, currency: string): string => {
7371
const ListView: FC<ListViewProps> = (props: ListViewProps) => {
7472
const [confirmFlow, setConfirmFlow] = React.useState<ConfirmFlowData | undefined>(undefined)
7573
const [winnings, setWinnings] = React.useState<ReadonlyArray<Winning>>([])
74+
const [selectedPayments, setSelectedPayments] = React.useState<{ [paymentId: string]: Winning }>({})
7675
const [isLoading, setIsLoading] = React.useState<boolean>(false)
7776
const [filters, setFilters] = React.useState<Record<string, string[]>>({})
7877
const [pagination, setPagination] = React.useState<PaginationInfo>({
@@ -130,14 +129,8 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
130129
fetchWinnings()
131130
}, [fetchWinnings])
132131

133-
const processPayouts = async (paymentIds: PaymentId): Promise<void> => {
134-
const winningIds: string[] = []
135-
Object.keys(paymentIds)
136-
.forEach((key: string) => {
137-
if (paymentIds[key]) {
138-
winningIds.push(key)
139-
}
140-
})
132+
const processPayouts = async (winningIds: string[]): Promise<void> => {
133+
setSelectedPayments({})
141134

142135
toast.info('Processing payments...', {
143136
position: toast.POSITION.BOTTOM_RIGHT,
@@ -246,13 +239,15 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
246239
...filters,
247240
[key]: value,
248241
})
242+
setSelectedPayments({})
249243
}}
250244
onResetFilters={() => {
251245
setPagination({
252246
...pagination,
253247
currentPage: 1,
254248
})
255249
setFilters({})
250+
setSelectedPayments({})
256251
}}
257252
/>
258253
{/* <PageDivider /> */}
@@ -262,6 +257,10 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
262257
currentPage={pagination.currentPage}
263258
numPages={pagination.totalPages}
264259
payments={winnings}
260+
selectedPayments={selectedPayments}
261+
onSelectedPaymentsChange={function onSelectedPaymentsChanged(selectedWinnings: { [paymentId: string]: Winning }) {
262+
setSelectedPayments(selectedWinnings)
263+
}}
265264
onNextPageClick={async function onNextPageClicked() {
266265
if (pagination.currentPage === pagination.totalPages) {
267266
return
@@ -289,12 +288,12 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
289288
})
290289
}}
291290
onPayMeClick={async function onPayMeClicked(
292-
paymentIds: PaymentId,
291+
paymentIds: { [paymentId: string]: Winning },
293292
totalAmount: string,
294293
) {
295294
setConfirmFlow({
296295
action: 'Yes',
297-
callback: () => processPayouts(paymentIds),
296+
callback: () => processPayouts(Object.keys(paymentIds)),
298297
content: `You are about to process payments for a total of USD ${totalAmount}`,
299298
title: 'Are you sure?',
300299
})

src/apps/wallet/src/lib/components/payments-table/PaymentTable.tsx

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react/jsx-no-bind */
22
/* eslint-disable @typescript-eslint/explicit-function-return-type */
3-
import React, { useState } from 'react'
3+
import React, { useEffect, useState } from 'react'
44

55
import { Button, IconOutline, PageDivider } from '~/libs/ui'
66

@@ -9,51 +9,61 @@ import { Winning } from '../../models/WinningDetail'
99
import styles from './PaymentTable.module.scss'
1010

1111
interface PaymentTableProps {
12-
payments: ReadonlyArray<Winning>
13-
onPayMeClick: (paymentIds: { [paymentId: string]: boolean }, totalAmount: string) => void
14-
currentPage: number
15-
numPages: number
16-
onNextPageClick: () => void
17-
onPreviousPageClick: () => void
18-
onPageClick: (pageNumber: number) => void
12+
payments: ReadonlyArray<Winning>;
13+
selectedPayments?: { [paymentId: string]: Winning };
14+
currentPage: number;
15+
numPages: number;
16+
onPayMeClick: (paymentIds: { [paymentId: string]: Winning }, totalAmount: string) => void;
17+
onSelectedPaymentsChange?: (paymentIds: { [paymentId: string]: Winning }) => void;
18+
onNextPageClick: () => void;
19+
onPreviousPageClick: () => void;
20+
onPageClick: (pageNumber: number) => void;
1921
}
22+
2023
const PaymentsTable: React.FC<PaymentTableProps> = (props: PaymentTableProps) => {
21-
const [selectedPayments, setSelectedPayments] = useState<{ [paymentId: string]: boolean }>({})
24+
const [selectedPayments, setSelectedPayments] = useState<{ [paymentId: string]: Winning }>({})
2225
const [toggleClicked, setToggleClicked] = useState(false)
2326

27+
useEffect(() => {
28+
if (props.selectedPayments) {
29+
setSelectedPayments(props.selectedPayments)
30+
}
31+
}, [props.selectedPayments])
32+
33+
const isSomeSelected = Object.keys(selectedPayments).length > 0
34+
2435
const togglePaymentSelection = (paymentId: string) => {
25-
setSelectedPayments(prevSelected => ({
26-
...prevSelected,
27-
[paymentId]: !prevSelected[paymentId],
28-
}))
29-
}
36+
const newSelections = { ...selectedPayments }
37+
if (newSelections[paymentId]) {
38+
delete newSelections[paymentId]
39+
} else {
40+
const payment = props.payments.find(p => p.id === paymentId)
41+
if (payment) {
42+
newSelections[paymentId] = payment
43+
}
44+
}
3045

31-
const isSomeSelected = Object.values(selectedPayments)
32-
.some(selected => selected)
46+
setSelectedPayments(newSelections)
47+
props.onSelectedPaymentsChange?.(newSelections)
48+
}
3349

3450
const toggleAllPayments = () => {
35-
setToggleClicked(!toggleClicked)
36-
37-
if (isSomeSelected) {
38-
setSelectedPayments({})
39-
} else {
40-
const newSelections: { [paymentId: string]: boolean } = {}
51+
const newSelections: { [paymentId: string]: Winning } = {}
52+
if (!toggleClicked && !isSomeSelected) {
4153
props.payments.forEach(payment => {
4254
if (payment.canBeReleased) {
43-
newSelections[payment.id] = true
55+
newSelections[payment.id] = payment
4456
}
4557
})
46-
setSelectedPayments(newSelections)
4758
}
48-
}
4959

50-
const calculateTotal = () => props.payments.reduce((acc: number, payment: Winning) => {
51-
if (selectedPayments[payment.id]) {
52-
return acc + parseFloat(payment.netPayment.replace(/[^0-9.-]+/g, ''))
53-
}
60+
setToggleClicked(!toggleClicked)
61+
setSelectedPayments(newSelections)
62+
props.onSelectedPaymentsChange?.(newSelections)
63+
}
5464

55-
return acc
56-
}, 0)
65+
const calculateTotal = () => Object.values(selectedPayments)
66+
.reduce((acc, payment) => acc + parseFloat(payment.netPayment.replace(/[^0-9.-]+/g, '')), 0)
5767

5868
const total = calculateTotal()
5969

0 commit comments

Comments
 (0)