Skip to content

Commit fb9faf0

Browse files
committed
chore: pr fixes
1 parent fb1c2d0 commit fb9faf0

File tree

2 files changed

+50
-110
lines changed

2 files changed

+50
-110
lines changed

src/components/Contractor/Payments/PaymentEdit/PaymentEdit.stories.tsx

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
import type { StoryDefault, Story } from '@ladle/react'
22
import { action } from '@ladle/react'
3+
import { FormWrapper } from '../../../../../.ladle/helpers/FormWrapper'
34
import { PaymentEdit } from './PaymentEditPresentation'
45

5-
const mockContractorFixed = {
6-
uuid: '1',
7-
contractorUuid: 'armstrong-louis',
8-
wageType: 'Fixed' as const,
9-
paymentMethod: 'Direct Deposit' as const,
10-
hours: undefined,
11-
wage: '1000',
12-
bonus: '0',
13-
reimbursement: '0',
14-
wageTotal: '1000',
15-
}
16-
17-
const mockContractorHourly = {
18-
uuid: '2',
19-
contractorUuid: 'fitzgerald-ella',
20-
wageType: 'Hourly' as const,
21-
hourlyRate: '18',
22-
paymentMethod: 'Check' as const,
23-
hours: '16',
24-
wage: undefined,
25-
bonus: '350',
26-
reimbursement: '0',
27-
wageTotal: '638',
28-
}
29-
306
export default {
31-
title: 'Domain/Contractor/Payments/Individual Contractor Earnings',
7+
title: 'Domain/Contractor/Payments/Edit Payment',
328
} satisfies StoryDefault
339

3410
export const EditPaymentFixedWage: Story = () => {
11+
const defaultValues = {
12+
uuid: '1',
13+
contractorUuid: 'armstrong-louis',
14+
wageType: 'Fixed',
15+
paymentMethod: 'Direct Deposit',
16+
hours: undefined,
17+
wage: '1000',
18+
bonus: '0',
19+
reimbursement: '0',
20+
wageTotal: '1000',
21+
}
22+
3523
return (
36-
<PaymentEdit
37-
contractor={mockContractorFixed}
38-
onSave={action('onSave')}
39-
onCancel={action('onCancel')}
40-
/>
24+
<FormWrapper defaultValues={defaultValues}>
25+
<PaymentEdit onSave={action('onSave')} onCancel={action('onCancel')} />
26+
</FormWrapper>
4127
)
4228
}
4329

@@ -47,12 +33,23 @@ EditPaymentFixedWage.meta = {
4733
}
4834

4935
export const EditPaymentHourlyWage: Story = () => {
36+
const defaultValues = {
37+
uuid: '2',
38+
contractorUuid: 'fitzgerald-ella',
39+
wageType: 'Hourly',
40+
hourlyRate: '18',
41+
paymentMethod: 'Check',
42+
hours: '16',
43+
wage: undefined,
44+
bonus: '350',
45+
reimbursement: '0',
46+
wageTotal: '638',
47+
}
48+
5049
return (
51-
<PaymentEdit
52-
contractor={mockContractorHourly}
53-
onSave={action('onSave')}
54-
onCancel={action('onCancel')}
55-
/>
50+
<FormWrapper defaultValues={defaultValues}>
51+
<PaymentEdit onSave={action('onSave')} onCancel={action('onCancel')} />
52+
</FormWrapper>
5653
)
5754
}
5855

src/components/Contractor/Payments/PaymentEdit/PaymentEditPresentation.tsx

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
import { useState } from 'react'
2-
import type { ContractorPaymentForGroup } from '../types'
1+
import { useWatch } from 'react-hook-form'
32
import { useComponentContext } from '@/contexts/ComponentAdapter/useComponentContext'
4-
import { Flex, Grid, ActionsLayout } from '@/components/Common'
3+
import { Flex, Grid, ActionsLayout, NumberInputField, RadioGroupField } from '@/components/Common'
54
import { formatNumberAsCurrency } from '@/helpers/formattedStrings'
65
import { useLocale } from '@/contexts/LocaleProvider/useLocale'
76

87
interface 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

Comments
 (0)