1
1
/* eslint-disable max-len */
2
2
/* eslint-disable react/jsx-no-bind */
3
+ import { toast } from 'react-toastify'
3
4
import React , { FC , useCallback , useEffect } from 'react'
4
5
5
6
import { Collapsible , ConfirmModal , LoadingCircles } from '~/libs/ui'
6
7
import { UserProfile } from '~/libs/core'
7
8
8
- import { getMemberHandle , getPayments } from '../../../lib/services/wallet'
9
+ import { editPayment , getMemberHandle , getPayments } from '../../../lib/services/wallet'
9
10
import { Winning , WinningDetail } from '../../../lib/models/WinningDetail'
10
- import { FilterBar } from '../../../lib'
11
+ import { FilterBar , PaymentView } from '../../../lib'
11
12
import { ConfirmFlowData } from '../../../lib/models/ConfirmFlowData'
12
13
import { PaginationInfo } from '../../../lib/models/PaginationInfo'
13
14
import PaymentEditForm from '../../../lib/components/payment-edit/PaymentEdit'
@@ -84,6 +85,30 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
84
85
totalItems : 0 ,
85
86
totalPages : 0 ,
86
87
} )
88
+ const [ editState , setEditState ] = React . useState < {
89
+ netAmount ?: number ;
90
+ releaseDate ?: Date ;
91
+ paymentStatus ?: string ;
92
+ auditNote ?: string ;
93
+ } > ( { } )
94
+
95
+ const editStateRef = React . useRef ( editState )
96
+
97
+ useEffect ( ( ) => {
98
+ editStateRef . current = editState
99
+ } , [ editState ] )
100
+
101
+ const handleValueUpdated = useCallback ( ( updates : {
102
+ auditNote ?: string ,
103
+ netAmount ?: number ,
104
+ paymentStatus ?: string ,
105
+ releaseDate ?: Date ,
106
+ } ) => {
107
+ setEditState ( prev => ( {
108
+ ...prev ,
109
+ ...updates ,
110
+ } ) )
111
+ } , [ ] )
87
112
88
113
const convertToWinnings = useCallback (
89
114
( payments : WinningDetail [ ] , handleMap : Map < number , string > ) => payments . map ( payment => {
@@ -116,7 +141,9 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
116
141
handle : handleMap . get ( parseInt ( payment . winnerId , 10 ) ) ?? payment . winnerId ,
117
142
id : payment . id ,
118
143
netPayment : formatCurrency ( payment . details [ 0 ] . totalAmount , payment . details [ 0 ] . currency ) ,
144
+ netPaymentNumber : parseFloat ( payment . details [ 0 ] . totalAmount ) ,
119
145
releaseDate : formattedReleaseDate ,
146
+ releaseDateObj : releaseDate ,
120
147
status : formatStatus ( payment . details [ 0 ] . status ) ,
121
148
type : payment . category . replaceAll ( '_' , ' ' )
122
149
. toLowerCase ( ) ,
@@ -143,6 +170,7 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
143
170
} finally {
144
171
setIsLoading ( false )
145
172
}
173
+ // eslint-disable-next-line react-hooks/exhaustive-deps
146
174
} , [ convertToWinnings , filters , pagination . currentPage , pagination . pageSize ] )
147
175
148
176
const renderConfirmModalContent = React . useMemo ( ( ) => {
@@ -157,10 +185,70 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
157
185
return confirmFlow ?. content
158
186
} , [ confirmFlow ] )
159
187
188
+ const updatePayment = async ( paymentId : string ) : Promise < void > => {
189
+ const currentEditState = editStateRef . current
190
+ // Send to server only the fields that have changed
191
+ const updateObj = {
192
+ auditNote : currentEditState . auditNote !== undefined ? currentEditState . auditNote : undefined ,
193
+ netAmount : currentEditState . netAmount !== undefined ? currentEditState . netAmount : undefined ,
194
+ paymentStatus : currentEditState . paymentStatus !== undefined ? currentEditState . paymentStatus : undefined ,
195
+ releaseDate : currentEditState . releaseDate !== undefined ? currentEditState . releaseDate : undefined ,
196
+ }
197
+
198
+ let paymentStatus : 'ON_HOLD_ADMIN' | 'OWED' | undefined
199
+ if ( updateObj . paymentStatus !== undefined ) paymentStatus = updateObj . paymentStatus . indexOf ( 'Owed' ) > - 1 ? 'OWED' : 'ON_HOLD_ADMIN'
200
+
201
+ const updates : {
202
+ auditNote ?: string
203
+ paymentStatus ?: 'ON_HOLD_ADMIN' | 'OWED'
204
+ releaseDate ?: string
205
+ paymentAmount ?: number
206
+ winningsId : string
207
+ } = {
208
+ auditNote : updateObj . auditNote ,
209
+ winningsId : paymentId ,
210
+ }
211
+
212
+ if ( paymentStatus ) updates . paymentStatus = paymentStatus
213
+ if ( updateObj . releaseDate !== undefined ) updates . releaseDate = updateObj . releaseDate . toISOString ( )
214
+ if ( updateObj . netAmount !== undefined ) updates . paymentAmount = updateObj . netAmount
215
+
216
+ toast . success ( 'Updating payment' , { position : toast . POSITION . BOTTOM_RIGHT } )
217
+ try {
218
+ const udpateMessage = await editPayment ( updates )
219
+ toast . success ( udpateMessage , { position : toast . POSITION . BOTTOM_RIGHT } )
220
+ } catch ( err ) {
221
+ toast . error ( 'Failed to update payment' , { position : toast . POSITION . BOTTOM_RIGHT } )
222
+ return
223
+ }
224
+
225
+ setEditState ( { } )
226
+
227
+ await fetchWinnings ( )
228
+ }
229
+
160
230
useEffect ( ( ) => {
161
231
fetchWinnings ( )
162
232
} , [ fetchWinnings ] )
163
233
234
+ const onPaymentEditCallback = useCallback ( ( payment : Winning ) => {
235
+ setConfirmFlow ( {
236
+ action : 'Save' ,
237
+ callback : async ( ) => {
238
+ updatePayment ( payment . id )
239
+ } ,
240
+ content : (
241
+ < PaymentEditForm
242
+ payment = { payment }
243
+ canSave = { setIsConfirmFormValid }
244
+ onValueUpdated = { handleValueUpdated }
245
+ />
246
+ ) ,
247
+ title : 'Edit Payment' ,
248
+ } )
249
+ // eslint-disable-next-line react-hooks/exhaustive-deps
250
+ } , [ handleValueUpdated , editState ] )
251
+
164
252
return (
165
253
< >
166
254
< div className = { styles . container } >
@@ -320,19 +408,23 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
320
408
currentPage : pageNumber ,
321
409
} )
322
410
} }
323
- onPaymentEditClick = { function onPaymentEditClicked ( payment : Winning ) {
411
+ onPaymentEditClick = { ( payment : Winning ) => {
412
+ setEditState ( { } )
413
+ onPaymentEditCallback ( payment )
414
+ } }
415
+ onPaymentViewClick = { function onPaymentViewClicked ( payment : Winning ) {
324
416
setConfirmFlow ( {
325
417
action : 'Save' ,
326
- callback : ( ) => console . log ( 'Edit payment:' , payment ) ,
418
+ callback : async ( ) => {
419
+ updatePayment ( payment . id )
420
+ } ,
327
421
content : (
328
- < PaymentEditForm
422
+ < PaymentView
329
423
payment = { payment }
330
- onErrorStateChanged = { function onErrorStateChanged ( error : boolean ) {
331
- setIsConfirmFormValid ( ! error )
332
- } }
333
424
/>
334
425
) ,
335
- title : 'Edit Payment' ,
426
+ showButtons : false ,
427
+ title : 'Payment Details' ,
336
428
} )
337
429
} }
338
430
/>
@@ -351,9 +443,11 @@ const ListView: FC<ListViewProps> = (props: ListViewProps) => {
351
443
</ div >
352
444
{ confirmFlow && (
353
445
< ConfirmModal
446
+ showButtons = { confirmFlow . showButtons }
354
447
title = { confirmFlow . title }
355
448
action = { confirmFlow . action }
356
449
onClose = { function onClose ( ) {
450
+ setEditState ( { } )
357
451
setConfirmFlow ( undefined )
358
452
} }
359
453
onConfirm = { function onConfirm ( ) {
0 commit comments