1
1
import { FunctionComponent , useState } from "react" ;
2
2
import Link from "next/link" ;
3
3
import { useRouter } from "next/router" ;
4
+ import Head from "next/head" ;
4
5
import { Formik } from "formik" ;
5
6
import { fetch } from "../../utils/dataAccess" ;
6
7
import { { { { ucf } } } } from '../../types/{{{ucf}}}' ;
@@ -27,15 +28,21 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
27
28
28
29
return (
29
30
< div >
30
- { { { { lc} } } ? < h1 > Edit { { { ucf} } } { { { { lc} } } [ '@id' ] } </h1 > : < h1 > Create { { { ucf} } } </ h1 > }
31
- < Formik
32
- initialValues = { { { ~ lc} } ? { ...{ { lc ~ } } } : new { { { ucf} } } ( ) }
33
- validate= { ( values ) => {
34
- const errors = { } ;
35
- // add your validation logic here
31
+ < div >
32
+ < Head >
33
+ < title > { { { { lc} } } ? `Edit {{{ucf}}} ${ { { { ~ lc } }}['@id']}` : `Create {{{ucf}}}` } </ title >
34
+ < meta property = "og:title" content = "{{{lc}}} ? `Edit {{{ucf}}} ${ {{{~lc}}}['@id']}` : `Create {{{ucf}}}`" />
35
+ </ Head >
36
+ </ div >
37
+ < h1 > { { { lc} } } ? `Edit {{{ucf}}} ${ { { { ~ lc } }}['@id']}` : `Create {{{ucf}}}` < / h 1 >
38
+ < Formik
39
+ initialValues = { { { ~ lc} } ? { ...{ { lc~ } } } : new { { { ucf} } } ( ) }
40
+ validate= { ( values ) => {
41
+ const errors = { } ;
42
+ // add your validation logic here
36
43
return errors ;
37
- } }
38
- onSubmit = { async ( values , { setSubmitting, setStatus } ) => {
44
+ } }
45
+ onSubmit = { async ( values , { setSubmitting, setStatus } ) => {
39
46
const isCreation = ! values [ "@id" ] ;
40
47
try {
41
48
await fetch ( isCreation ? "/{{{name}}}" : values [ "@id" ] , {
@@ -53,24 +60,24 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
53
60
msg : `Error when ${ isCreation ? 'creating' : 'updating' } the resource.` ,
54
61
} ) ;
55
62
}
56
- setSubmitting ( false ) ;
57
- } }
58
- >
59
- { ( {
60
- values,
63
+ setSubmitting ( false ) ;
64
+ } }
65
+ >
66
+ { ( {
67
+ values,
61
68
status,
62
69
errors,
63
70
touched,
64
- handleChange,
65
- handleBlur,
66
- handleSubmit,
67
- isSubmitting,
68
- } ) => (
69
- < form onSubmit = { handleSubmit } >
71
+ handleChange,
72
+ handleBlur,
73
+ handleSubmit,
74
+ isSubmitting,
75
+ } ) => (
76
+ < form onSubmit = { handleSubmit } >
70
77
{ { #each formFields} }
71
- < div className = "form-group" >
72
- < label className = "form-control-label" htmlFor = "{{lc}}_{{name}}" > { { name} } </ label >
73
- < input
78
+ < div className = "form-group" >
79
+ < label className = "form-control-label" htmlFor = "{{lc}}_{{name}}" > { { name} } </ label >
80
+ < input
74
81
name = "{{name}}"
75
82
id = "{{lc}}_{{name}}"
76
83
value = { values . { { name } } ?? "" }
@@ -80,48 +87,48 @@ export const Form: FunctionComponent<Props> = ({ {{{lc}}} }) => {
80
87
{ { #if required} } required = { true } { { / i f } }
81
88
className = { `form-control${ errors . { { name } } && touched.{{name}} ? ' is-invalid' : ''}` }
82
89
aria-invalid = { errors . { { name } } && touched . { { name~ } } }
83
- onChange = { handleChange }
84
- onBlur = { handleBlur }
85
- />
86
- </ div >
87
- { errors . { { name } } && touched . { { name} } && < div className = "invalid-feedback" > { errors . { { name } } } </ div > }
90
+ onChange = { handleChange }
91
+ onBlur = { handleBlur }
92
+ />
93
+ </ div >
94
+ { errors . { { name } } && touched . { { name} } && < div className = "invalid-feedback" > { errors . { { name } } } </ div > }
88
95
{ { / each} }
89
96
90
- { status && status . msg && (
91
- < div
92
- className = { `alert ${
93
- status . isValid ? "alert-success" : "alert-danger"
94
- } `}
95
- role = "alert"
96
- >
97
- { status . msg }
98
- </ div >
99
- ) }
97
+ { status && status . msg && (
98
+ < div
99
+ className = { `alert ${
100
+ status . isValid ? "alert-success" : "alert-danger"
101
+ } `}
102
+ role = "alert"
103
+ >
104
+ { status . msg }
105
+ </ div >
106
+ ) }
100
107
101
- { error && (
102
- < div className = "alert alert-danger" role = "alert" >
103
- { error }
104
- </ div >
105
- ) }
108
+ { error && (
109
+ < div className = "alert alert-danger" role = "alert" >
110
+ { error }
111
+ </ div >
112
+ ) }
106
113
107
- < button
108
- type = "submit"
109
- className = "btn btn-success"
110
- disabled = { isSubmitting }
111
- >
112
- Submit
113
- </ button >
114
- </ form >
115
- ) }
116
- < / Formik >
117
- < Link href = "/{{{name}}}" >
118
- < a className = "btn btn-primary" > Back to list</ a >
119
- < / L i n k >
120
- { { { { lc} } } && (
121
- < button className = "btn btn-danger" onClick = { handleDelete } >
122
- < a > Delete</ a >
123
- </ button >
124
- ) }
125
- < / d i v >
126
- ) ;
114
+ < button
115
+ type = "submit"
116
+ className = "btn btn-success"
117
+ disabled = { isSubmitting }
118
+ >
119
+ Submit
120
+ </ button >
121
+ </ form >
122
+ ) }
123
+ < / Formik >
124
+ < Link href = "/{{{name}}}" >
125
+ < a className = "btn btn-primary" > Back to list</ a >
126
+ < / L i n k >
127
+ { { { { lc} } } && (
128
+ < button className = "btn btn-danger" onClick = { handleDelete } >
129
+ < a > Delete</ a >
130
+ </ button >
131
+ ) }
132
+ < / d i v >
133
+ ) ;
127
134
} ;
0 commit comments