-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathAddForageForm.tsx
More file actions
128 lines (119 loc) · 4.26 KB
/
AddForageForm.tsx
File metadata and controls
128 lines (119 loc) · 4.26 KB
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
import { FormProvider, useForm } from 'react-hook-form';
import { Stack } from '@mui/material';
import { useToolbarContext } from 'contexts/ToolbarContext';
import FormTextField from 'components/forms/FormTextField/FormTextField';
import FormMultipleChoiceField from 'components/forms/FormMultipleChoiceField/FormMultipleChoiceField';
import { zodResolver } from '@hookform/resolvers/zod';
import FormCheckboxListField from 'components/forms/FormCheckboxListField/FormCheckboxListField';
import FormResourceAddressField from 'components/forms/FormAddressField/FormResourceAddressField';
import useAddResourceMutation from 'hooks/mutations/useAddResourceMutation';
import ResourceEntryTypeField from 'components/forms/ResourceEntryTypeField/ResourceEntryTypeField';
import ResourceForm from 'components/AddResourceModal/ResourceForm';
import foragingResourceSchema, {
type ForagingFormValues
} from 'schemas/foragingResourceSchema';
import { tagOptions, forageTypeOptions } from './choiceFieldOptions';
type AddForageFormProps = {
onGoBack: VoidFunction;
onComplete: VoidFunction;
};
type FormValues = ForagingFormValues;
const TITLE = 'Add a Foraging Resource';
const COLOR = '#5DA694';
const SCHEMA = foragingResourceSchema;
const AddForageForm = ({ onGoBack, onComplete }: AddForageFormProps) => {
const { setToolbarModal } = useToolbarContext();
const { mutate: addResource, isPending } = useAddResourceMutation();
const methods = useForm({
defaultValues: SCHEMA.parse({}),
resolver: zodResolver(SCHEMA)
});
const onClose = () => {
onGoBack();
setToolbarModal(null);
};
const onSubmit = (resource: FormValues) => {
addResource(resource, { onSuccess: onComplete });
};
return (
<FormProvider {...methods}>
<ResourceForm<FormValues>
title={TITLE}
color={COLOR}
onSubmit={onSubmit}
isSubmitting={isPending}
onClose={onClose}
onGoBack={onGoBack}
renderPageOne={({ imageElement, shouldShowImageElement }) => (
<>
{shouldShowImageElement && imageElement}
<Stack
direction={{ sx: 'column', md: 'row' }}
gap={2}
justifyContent={{ sx: 'flex-start', md: 'center' }}
>
<FormTextField<FormValues>
name="name"
label="Name"
helperText="Enter a name for the resource. (Example: City Hall)"
required
fullWidth
/>
<FormResourceAddressField label="Street Address" fullWidth />
</Stack>
<Stack
direction={{ sx: 'column', md: 'row' }}
gap={2}
justifyContent={{ sx: 'flex-start', md: 'center' }}
>
<FormTextField<FormValues>
name="description"
label="Description"
fullWidth
/>
<ResourceEntryTypeField />
</Stack>
<Stack
direction={{ sx: 'column', md: 'row' }}
gap={2}
justifyContent={{ sx: 'flex-start', md: 'center' }}
>
<FormMultipleChoiceField<FormValues>
name="forage.forage_type"
label="Forage Type"
options={forageTypeOptions}
fullWidth
/>
</Stack>
</>
)}
renderPageTwo={({ imageElement, shouldShowImageElement }) => (
<>
<Stack
direction={{ sx: 'column', md: 'row' }}
gap={3}
justifyContent={{ sx: 'flex-start', md: 'space-evenly' }}
>
{shouldShowImageElement && imageElement}
<FormCheckboxListField<FormValues>
name="forage.tags"
label="Helpful info"
options={tagOptions}
labelPlacement="end"
/>
</Stack>
<FormTextField<FormValues>
name="guidelines"
label="Guidelines"
helperText="Share tips on respectful PHLASKing at this location."
fullWidth
multiline
minRows={2}
/>
</>
)}
/>
</FormProvider>
);
};
export default AddForageForm;