1- import { useState } from 'react'
2- import type { ContractorPaymentForGroup } from '../types'
1+ import { useWatch } from 'react-hook-form'
32import { useComponentContext } from '@/contexts/ComponentAdapter/useComponentContext'
4- import { Flex , Grid , ActionsLayout } from '@/components/Common'
3+ import { Flex , Grid , ActionsLayout , NumberInputField , RadioGroupField } from '@/components/Common'
54import { formatNumberAsCurrency } from '@/helpers/formattedStrings'
65import { useLocale } from '@/contexts/LocaleProvider/useLocale'
76
87interface PaymentEditProps {
9- contractor : ContractorPaymentForGroup
10- onSave : ( contractor : ContractorPaymentForGroup ) => void
8+ onSave : ( ) => void
119 onCancel : ( ) => void
1210}
1311
14- export const PaymentEdit = ( { contractor , onSave, onCancel } : PaymentEditProps ) => {
15- const { Button, Text, Heading, Card, NumberInput , RadioGroup } = useComponentContext ( )
12+ export const PaymentEdit = ( { onSave, onCancel } : PaymentEditProps ) => {
13+ const { Button, Text, Heading, Card } = useComponentContext ( )
1614 const { locale } = useLocale ( )
1715
18- const [ editedContractor , setEditedContractor ] = useState < ContractorPaymentForGroup > ( {
19- ...contractor ,
20- } )
21-
22- const handleFieldChange = ( field : keyof ContractorPaymentForGroup , value : number | string ) => {
23- setEditedContractor ( ( prev : ContractorPaymentForGroup ) => ( {
24- ...prev ,
25- [ field ] : value ,
26- } ) )
27- }
28-
29- const handleSave = ( ) => {
30- onSave ( editedContractor )
31- }
16+ const wageType = useWatch ( { name : 'wageType' } )
17+ const wageTotal = useWatch ( { name : 'wageTotal' } )
3218
3319 const paymentMethodOptions = [
3420 { value : 'Check' , label : 'Check' } ,
@@ -48,70 +34,31 @@ export const PaymentEdit = ({ contractor, onSave, onCancel }: PaymentEditProps)
4834 </ Text >
4935 </ Flex >
5036
51- { editedContractor . wageType === 'Hourly' && (
37+ { wageType === 'Hourly' && (
5238 < Flex flexDirection = "column" gap = { 16 } >
5339 < Heading as = "h3" > Hours</ Heading >
54- < NumberInput
55- name = "hours"
56- value = { editedContractor . hours ? parseFloat ( editedContractor . hours ) : 0 }
57- onChange = { ( value : number ) => {
58- handleFieldChange ( 'hours' , value ? value . toString ( ) : '0' )
59- } }
60- isRequired
61- label = "Hours"
62- adornmentEnd = "hrs"
63- />
40+ < NumberInputField name = "hours" isRequired label = "Hours" adornmentEnd = "hrs" />
6441 </ Flex >
6542 ) }
6643
67- { editedContractor . wageType === 'Fixed' && (
44+ { wageType === 'Fixed' && (
6845 < Flex flexDirection = "column" gap = { 16 } >
6946 < Heading as = "h3" > Fixed pay</ Heading >
70- < NumberInput
71- name = "wage"
72- value = { editedContractor . wage ? parseFloat ( editedContractor . wage ) : 0 }
73- onChange = { ( value : number ) => {
74- handleFieldChange ( 'wage' , value ? value . toString ( ) : '0' )
75- } }
76- isRequired
77- label = "Wage"
78- format = "currency"
79- />
47+ < NumberInputField name = "wage" isRequired label = "Wage" format = "currency" />
8048 </ Flex >
8149 ) }
8250
8351 < Flex flexDirection = "column" gap = { 16 } >
8452 < Heading as = "h3" > Additional earnings</ Heading >
8553 < Grid gridTemplateColumns = { { base : '1fr' , small : [ 200 , 200 ] } } gap = { 16 } >
86- < NumberInput
87- name = "bonus"
88- value = { editedContractor . bonus ? parseFloat ( editedContractor . bonus ) : 0 }
89- onChange = { ( value : number ) => {
90- handleFieldChange ( 'bonus' , value ? value . toString ( ) : '0' )
91- } }
92- label = "Bonus"
93- format = "currency"
94- />
95- < NumberInput
96- name = "reimbursement"
97- value = {
98- editedContractor . reimbursement ? parseFloat ( editedContractor . reimbursement ) : 0
99- }
100- onChange = { ( value : number ) => {
101- handleFieldChange ( 'reimbursement' , value ? value . toString ( ) : '0' )
102- } }
103- label = "Reimbursement"
104- format = "currency"
105- />
54+ < NumberInputField name = "bonus" label = "Bonus" format = "currency" />
55+ < NumberInputField name = "reimbursement" label = "Reimbursement" format = "currency" />
10656 </ Grid >
10757 </ Flex >
10858
10959 < Flex flexDirection = "column" gap = { 16 } >
110- < RadioGroup
111- value = { editedContractor . paymentMethod || 'Direct Deposit' }
112- onChange = { ( value : string ) => {
113- handleFieldChange ( 'paymentMethod' , value )
114- } }
60+ < RadioGroupField
61+ name = "paymentMethod"
11562 options = { paymentMethodOptions }
11663 label = "Payment Method"
11764 />
@@ -120,18 +67,14 @@ export const PaymentEdit = ({ contractor, onSave, onCancel }: PaymentEditProps)
12067 < Flex justifyContent = "space-between" alignItems = "center" >
12168 < Text >
12269 < strong >
123- Total pay:{ ' ' }
124- { formatNumberAsCurrency (
125- contractor . wageTotal ? parseFloat ( contractor . wageTotal ) : 0 ,
126- locale ,
127- ) }
70+ Total pay: { formatNumberAsCurrency ( parseFloat ( wageTotal || '0' ) , locale ) }
12871 </ strong >
12972 </ Text >
13073 < ActionsLayout >
13174 < Button onClick = { onCancel } variant = "secondary" >
13275 Cancel
13376 </ Button >
134- < Button onClick = { handleSave } variant = "primary" >
77+ < Button onClick = { onSave } variant = "primary" >
13578 OK
13679 </ Button >
13780 </ ActionsLayout >
0 commit comments