-
Notifications
You must be signed in to change notification settings - Fork 5
/
AgencyForm.tsx
130 lines (115 loc) · 3.47 KB
/
AgencyForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Button } from 'react-bootstrap'
import { useForm, UseFormReturn } from 'react-hook-form'
import type { EditAgencyById, UpdateAgencyInput } from 'types/graphql'
import { useAuth } from 'web/src/auth'
import {
Form,
FormError,
FieldError,
Label,
TextField,
Submit,
} from '@redwoodjs/forms'
import type { RWGqlError } from '@redwoodjs/forms'
type FormAgency = NonNullable<EditAgencyById['agency']>
interface AgencyFormProps {
agency?: EditAgencyById['agency']
onSave: (data: UpdateAgencyInput, id?: FormAgency['id']) => void
error: RWGqlError
loading: boolean
}
const AgencyForm = (props: AgencyFormProps) => {
const { agency, onSave, error, loading } = props
const formMethods: UseFormReturn<FormAgency> = useForm<FormAgency>()
const hasErrors = Object.keys(formMethods.formState.errors).length > 0
const { currentUser } = useAuth()
// Resets the form to the previous values when editing the existing agency
// Clears out the form when creating a new agency
const onReset = () => {
formMethods.reset()
}
const onSubmit = (data: FormAgency) => {
const modifiedData = {
...data,
organizationId: currentUser.agency.organizationId,
}
onSave(modifiedData, props?.agency?.id)
}
return (
<Form<FormAgency>
onSubmit={onSubmit}
formMethods={formMethods}
error={error}
className={hasErrors ? 'was-validated' : ''}
>
{agency && (
<div className="row">
<Label name="id" className="form-label col-sm-2 col-form-label">
Agency Code
</Label>
<div className="col-sm-2">
<TextField
name="id"
defaultValue={agency?.id}
className="form-control mb-3 col-auto"
disabled
/>
</div>
</div>
)}
<FormError
error={error}
wrapperClassName="rw-form-error-wrapper"
titleClassName="rw-form-error-title"
listClassName="rw-form-error-list"
/>
<div className="row mb-3">
<Label name="code" className="form-label col-sm-2 col-form-label">
Agency Code
</Label>
<div className="col-sm-6">
<TextField
name="code"
defaultValue={agency?.code}
className="form-control"
errorClassName="form-control is-invalid"
validation={{ required: 'This field is required' }}
/>
</div>
<FieldError
name="code"
className="error-message offset-2 invalid-feedback"
/>
</div>
<div className="row mb-3">
<Label name="name" className="form-label col-sm-2 col-form-label">
Agency Name
</Label>
<div className="col-sm-6">
<TextField
name="name"
defaultValue={agency?.name}
className="form-control"
errorClassName="form-control is-invalid"
validation={{ required: 'This field is required' }}
/>
</div>
<FieldError
name="name"
className="error-message offset-2 invalid-feedback"
/>
</div>
<div className="row">
<div className="offset-2 col-sm-6">
<Submit disabled={loading} className="btn btn-primary me-2">
Save
</Submit>
<Button onClick={onReset} className="btn btn-secondary">
Reset
</Button>
</div>
</div>
</Form>
)
}
export default AgencyForm