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
55import { Button , IconOutline , PageDivider } from '~/libs/ui'
66
@@ -9,51 +9,61 @@ import { Winning } from '../../models/WinningDetail'
99import styles from './PaymentTable.module.scss'
1010
1111interface 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+
2023const 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