-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathForm.tsx
215 lines (204 loc) · 7.33 KB
/
Form.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { FunctionComponent, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/router";
import { ErrorMessage{{#if hasManyRelations}}, Field, FieldArray{{/if}}, Formik } from "formik";
import { useMutation } from "@tanstack/react-query";
import { fetchApi, FetchError, FetchResponse } from "../../utils/dataAccess";
import { {{{ucf}}} } from '../../types/{{{ucf}}}';
interface Props {
{{{lc}}}?: {{{ucf}}};
}
interface SaveParams {
values: {{{ucf}}};
}
interface DeleteParams {
id: string;
}
const save{{{ucf}}} = async ({ values }: SaveParams) =>
await fetchApi<{{ucf}}>(!values["@id"] ? "/{{{name}}}" : values["@id"], {
method: !values["@id"] ? "POST" : "PUT",
body: JSON.stringify(values),
});
const delete{{{ucf}}} = async (id: string) => await fetchApi<{{ucf}}>(id, { method: "DELETE" });
export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
const [, setError] = useState<string | null>(null);
const router = useRouter();
const saveMutation = useMutation<FetchResponse<{{ucf}}> | undefined, Error|FetchError, SaveParams>({mutationFn: (saveParams) => save{{{ucf}}}(saveParams)});
const deleteMutation = useMutation<FetchResponse<{{ucf}}> | undefined, Error|FetchError, DeleteParams>(
{
mutationFn: ({ id }) => delete{{{ucf}}}(id),
onSuccess: () => {
router.push("/{{{lc}}}s");
},
onError: (error)=> {
setError(`Error when deleting the resource: ${error}`);
console.error(error);
},
});
const handleDelete = () => {
if (!{{lc}} || !{{lc}}["@id"]) return;
if (!window.confirm("Are you sure you want to delete this item?")) return;
deleteMutation.mutate({ id: {{lc}}["@id"] });
};
return (
<div className="container mx-auto px-4 max-w-2xl mt-4">
<Link
href="/{{{lc}}}s"
className="text-sm text-cyan-500 font-bold hover:text-cyan-700"
>
{`< Back to list`}
</Link>
<h1 className="text-3xl my-2">
{ {{{lc}}} ? `Edit {{{ucf}}} ${ {{~lc}}['@id']}` : `Create {{{ucf}}}` }
</h1>
<Formik
initialValues={
{{lc}} ?
{
...{{lc}},
{{#each fields}}
{{#if isEmbeddeds}}
{{name}}: {{../lc}}["{{name}}"]?.map((emb: any) => emb['@id']) ?? [],
{{else if embedded}}
{{name}}: {{../lc}}["{{name}}"]?.['@id'] ?? "",
{{/if}}
{{/each}}
} :
new {{{ucf}}}()
}
validate={() => {
const errors = {};
// add your validation logic here
return errors;
}}
onSubmit={(values, { setSubmitting, setStatus, setErrors }) => {
const isCreation = !values["@id"];
saveMutation.mutate(
{ values },
{
onSuccess: () => {
setStatus({
isValid: true,
msg: `Element ${isCreation ? "created" : "updated"}.`,
});
router.push("/{{{lc}}}s");
},
onError: (error) => {
setStatus({
isValid: false,
msg: `${error.message}`,
});
if ("fields" in error) {
setErrors(error.fields);
}
},
onSettled: ()=> {
setSubmitting(false);
}
}
);
}}
>
{({
values,
status,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
}) => (
<form className="shadow-md p-4" onSubmit={handleSubmit}>
{{#each formFields}}
<div className="mb-2">
{{#if isRelations}}
<div className="text-gray-700 block text-sm font-bold">{{name}}</div>
<FieldArray
name="{{name}}"
render={(arrayHelpers) => (
<div className="mb-2" id="{{../lc}}_{{name}}">
{values.{{name}} && values.{{name}}.length > 0 ? (
values.{{name}}.map((item: any, index: number) => (
<div key={index}>
<Field name={`{{name}}.${index}`} />
<button
type="button"
onClick={() => arrayHelpers.remove(index)}
>
-
</button>
<button
type="button"
onClick={() => arrayHelpers.insert(index, '')}
>
+
</button>
</div>
))
) : (
<button type="button" onClick={() => arrayHelpers.push('')}>
Add
</button>
)}
</div>
)}
/>
{{else}}
<label className="text-gray-700 block text-sm font-bold" htmlFor="{{../lc}}_{{name}}">{{name}}</label>
<input
name="{{name}}"
id="{{../lc}}_{{name}}"
{{#compare type "==" "dateTime" }}
value={values.{{name}}?.toLocaleString() ?? ""}
{{/compare}}
{{#compare type "!=" "dateTime" }}
value={values.{{name}} ?? ""}
{{/compare}}
type="{{type}}"
{{#if step}}step="{{{step}}}"{{/if}}
placeholder="{{{description}}}"
{{#if required}}required={true}{{/if}}
className={`mt-1 block w-full ${errors.{{name}} && touched.{{name}} ? 'border-red-500' : ''}`}
aria-invalid={errors.{{name}} && touched.{{name~}} ? 'true' : undefined}
onChange={handleChange}
onBlur={handleBlur}
/>
<ErrorMessage
className="text-xs text-red-500 pt-1"
component="div"
name="{{name}}"
/>
{{/if}}
</div>
{{/each}}
{status && status.msg && (
<div
className={`border px-4 py-3 my-4 rounded ${
status.isValid ? "text-cyan-700 border-cyan-500 bg-cyan-200/50" : "text-red-700 border-red-400 bg-red-100"
}`}
role="alert"
>
{status.msg}
</div>
)}
<button
type="submit"
className="inline-block mt-2 bg-cyan-500 hover:bg-cyan-700 text-sm text-white font-bold py-2 px-4 rounded"
disabled={isSubmitting}
>
Submit
</button>
</form>
)}
</Formik>
<div className="flex space-x-2 mt-4 justify-end">
{ {{{lc}}} && (
<button className="inline-block mt-2 border-2 border-red-400 hover:border-red-700 hover:text-red-700 text-sm text-red-400 font-bold py-2 px-4 rounded" onClick={handleDelete}>
Delete
</button>
)}
</div>
</div>
);
};