@@ -5,26 +5,73 @@ import React from 'react'
55
66import { Button , Collapsible } from '~/libs/ui'
77
8+ import { WinningsAudit } from '../../models/WinningsAudit'
89import { Winning } from '../../models/WinningDetail'
10+ import { fetchAuditLogs , getMemberHandle } from '../../services/wallet'
911
1012import styles from './PaymentView.module.scss'
1113
1214interface PaymentViewProps {
1315 payment : Winning
14- auditLines ?: {
15- date : string
16- userName : string
17- action : string
18- } [ ]
1916}
2017
2118const PaymentView : React . FC < PaymentViewProps > = ( props : PaymentViewProps ) => {
2219 const [ view , setView ] = React . useState < 'details' | 'audit' > ( 'details' )
20+ const [ auditLines , setAuditLines ] = React . useState < WinningsAudit [ ] > ( [ ] )
2321
2422 const handleToggleView = ( ) : void => {
2523 setView ( view === 'details' ? 'audit' : 'details' )
2624 }
2725
26+ React . useEffect ( ( ) => {
27+ if ( view === 'audit' ) {
28+ fetchAuditLogs ( props . payment . id )
29+ . then ( auditLogs => {
30+ const userIds = auditLogs . map ( log => log . userId )
31+ getMemberHandle ( userIds )
32+ . then ( ( handles : Map < number , string > ) => {
33+ auditLogs . forEach ( ( log : WinningsAudit ) => {
34+ log . userId = handles . get ( parseInt ( log . userId , 10 ) ) ?? log . userId
35+ } )
36+ } )
37+ . catch ( ( ) => {
38+ console . error ( 'Error fetching member handles' )
39+ } )
40+ . finally ( ( ) => {
41+ setAuditLines ( auditLogs )
42+ } )
43+ } )
44+ . catch ( ( ) => {
45+ setAuditLines ( [ ] )
46+ } )
47+ }
48+ } , [ props . payment . id , view ] )
49+
50+ const formatAction = ( action : string ) : React . ReactNode => {
51+ const fromIndex = action . indexOf ( 'from' )
52+ const toIndex = action . indexOf ( 'to' )
53+
54+ if ( fromIndex !== - 1 && toIndex !== - 1 ) {
55+ const beforeFrom = action . substring ( 0 , fromIndex )
56+ const fromValue = action . substring ( fromIndex + 5 , toIndex )
57+ const toValue = action . substring ( toIndex + 3 )
58+ return (
59+ < >
60+ { beforeFrom }
61+ from
62+ { ' ' }
63+ < strong > { fromValue } </ strong >
64+ { ' ' }
65+ to
66+ { ' ' }
67+ < strong > { toValue } </ strong >
68+ </ >
69+ )
70+ }
71+
72+ return action
73+ }
74+
2875 return (
2976 < div className = { styles . formContainer } >
3077 < div className = { styles . inputGroup } >
@@ -72,12 +119,12 @@ const PaymentView: React.FC<PaymentViewProps> = (props: PaymentViewProps) => {
72119 { view === 'audit' && (
73120 < >
74121 < div className = { styles . auditSection } >
75- { props . auditLines && props . auditLines . map ( line => (
122+ { auditLines && auditLines . length > 0 && auditLines . map ( line => (
76123 < Collapsible
77124 header = { (
78125 < h3 >
79126 {
80- new Date ( line . date )
127+ new Date ( line . createdAt )
81128 . toLocaleString ( )
82129 }
83130 </ h3 >
@@ -90,18 +137,23 @@ const PaymentView: React.FC<PaymentViewProps> = (props: PaymentViewProps) => {
90137 < p >
91138 < strong > User:</ strong >
92139 { ' ' }
93- { line . userName }
140+ { line . userId }
94141 </ p >
95142 < p >
96143 < strong > Action:</ strong >
97144 { ' ' }
98- { line . action }
145+ { formatAction ( line . action ) }
146+ </ p >
147+ < p >
148+ < strong > Note:</ strong >
149+ { ' ' }
150+ { line . note }
99151 </ p >
100152 </ div >
101153 </ div >
102154 </ Collapsible >
103155 ) ) }
104- { ( props . auditLines === undefined || props . auditLines . length === 0 )
156+ { ( auditLines === undefined || auditLines . length === 0 )
105157 && (
106158 < div className = { styles . auditItem } >
107159 < p > No audit data available</ p >
0 commit comments