-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathFormulario.jsx
More file actions
170 lines (165 loc) · 7.74 KB
/
Formulario.jsx
File metadata and controls
170 lines (165 loc) · 7.74 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
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
import { Field, Form, Formik } from 'formik'
import * as Yup from 'yup'
import { useRouter } from 'next/router'
import Alerta from './Alerta'
const Formulario = ({ cliente }) => {
const router = useRouter()
const validateYupSchema = Yup.object().shape({
fullName: Yup.string()
.min(3, 'El nombre es muy corto')
.max(18, 'El nombre es muy largo')
.required('El nombre es obligatorio'),
nickname: Yup.string().required(' nickName es obligatorio'),
age: Yup.number()
.positive('la edad es invalida')
.required('edad Es obligatorio'),
gender: Yup.string()
.min(3, 'El genero es muy corto')
.max(10, 'El genero es muy largo')
.required('El genero es obligatorio'),
occupation: Yup.string()
.min(3, 'ocupacion es muy corta')
.max(19, 'ocupacion es muy larga')
.required('ocupacion es obligatoria'),
})
const onsubmit1 = async (values) => {
try {
if (cliente.id) {
const respuesta = await fetch(`http://localhost:3001/people/${cliente.id}`, {
method: 'PUT',
body: JSON.stringify(values),
headers: { 'Content-Type': 'application/json' }
})
await respuesta.json()
router.push(`/verCliente/${cliente.id}`)
}
} catch (error) {
console.log(error)
}
}
function cancelar() {
router.push(`/verCliente/${cliente.id}`)
}
return (
<div className="bg-white mt-10 px-5 py-10 rounded-md shadow-lg shadow-gray-600 md:w-3/4 mx-auto">
<h1 className="text-gray-600 font-black text-xl uppercase"> Editar : {cliente.fullName}</h1>
<Formik
initialValues={{
fullName: cliente?.fullName ?? '',
nickname: cliente?.nickname ?? '',
age: cliente?.age ?? '',
gender: cliente?.gender ?? '',
picture: cliente?.picture ?? '',
occupation: cliente?.occupation ?? ''
}}
enableReinitialize={true}
onSubmit={async (values, { resetForm }) => {
onsubmit1(values)
resetForm()
}}
validationSchema={validateYupSchema}
>
{({ errors, touched }) => {
return (
<Form
className="mt-10"
>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='fullName'
>Nombre:</label>
<Field
id="fullName"
type="text"
className="mt-2 block w-full p-3 bg-gray-50 "
placeholder="Nombre del Cliente"
name="fullName"
/>
{touched.fullName && errors.fullName ? <Alerta>{errors.fullName}</Alerta> : null}
</div>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='nickname'
>Nick:</label>
<Field
id="nickname"
type="text"
className="mt-2 block w-full p-3 bg-gray-50"
placeholder="Nck del Cliente"
name="nickname"
/>
{touched.nickname && errors.nickname ? <Alerta>{errors.nickname}</Alerta> : null}
</div>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='age'
>Edad:</label>
<Field
id="age"
type="number"
className="mt-2 block w-full p-3 bg-gray-50 "
placeholder="Edad del Cliente"
name="age"
/>
{touched.age && errors.age ? <Alerta>{errors.age}</Alerta> : null}
</div>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='occupation'
>Ocupacion:</label>
<Field
id="occupation"
type="text"
className="mt-2 block w-full p-3 bg-gray-50 "
placeholder="Edad del Cliente"
name="occupation"
/>
{touched.occupation && errors.occupation ? <Alerta>{errors.occupation}</Alerta> : null}
</div>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='gender'
>Genero:</label>
<Field
id="gender"
type="text"
className="mt-2 block w-full p-3 bg-gray-50 "
placeholder="genero del Cliente"
name="gender"
/>
{touched.gender && errors.gender ? <Alerta>{errors.gender}</Alerta> : null}
</div>
<div className="mb-4">
<label
className="text-gray-800"
htmlFor='picture'
>url:</label>
<Field
id="picture"
type="text"
className="mt-2 block w-full p-3 bg-gray-50"
placeholder="colocar url"
name="picture"
/>
</div>
<div className="flex">
<div className='flex-initial w-3/4'>
<input type="submit" value='Editar perfil' className=" mt-5 w-full bg-blue-800 p-3 text-white uppercase font-bold text-lg" />
</div>
<div>
<button onClick={() => cancelar()} className="mt-5 ml-10 flex-initial w-32 bg-red-500 p-3 text-white uppercase font-bold text-lg">cancelar</button>
</div>
</div>
</Form>
)
}}
</Formik>
</div>
)
}
export default Formulario