Browse Source

Puesto superior logic

amenpunk 2 years ago
parent
commit
1477f93710
1 changed files with 41 additions and 14 deletions
  1. 41 14
      src/Components/Modal/AgregarManual.js

+ 41 - 14
src/Components/Modal/AgregarManual.js

@@ -25,17 +25,34 @@ import { useSelector } from 'react-redux';
25 25
 const filter = createFilterOptions();
26 26
 
27 27
 async function getPuestoSuperior(puesto, auth) {
28
-  if (puesto.length < 3) return []
28
+  if (puesto.length < 2) return []
29 29
   let rest = new Service(`/plaza/keypuestosup?keyword=${puesto}`)
30 30
   let result = await rest.get(auth.token)
31 31
   // console.log(result)
32 32
   if (result?.data?.length > 0) {
33
-    result = result.data.map((item) => ({ 'title': item.nombre, year: item.id }))
33
+    result = result.data.map((item) => ({ 'title': item.nombre, id: item.id }))
34 34
     return result;
35 35
   }
36 36
   return [];
37 37
 }
38 38
 
39
+async function savePuestoSuperior(input, auth) {
40
+  let rest = new Service("/plaza/puestosuperior")
41
+  let body = {
42
+    "active": 1,
43
+    "nombre": input.title,
44
+    "decription": input.id,
45
+    "modifyday": "2023-02-12T23:55:26.007",
46
+    "createday": "2023-02-12T23:55:26.007",
47
+    "id": null,
48
+    "descripction": input.id,
49
+    "modify_day": "2023-02-12T23:55:26.007",
50
+  }
51
+  let result = await rest.post(body, auth.token);
52
+  let { id, nombre } = result;
53
+  return { id, nombre }
54
+}
55
+
39 56
 
40 57
 function Manual(props) {
41 58
 
@@ -58,7 +75,7 @@ function Manual(props) {
58 75
 
59 76
   const NewPlazaSchema = Yup.object().shape({
60 77
     nombrepuesto: Yup.string().required('El nombre es requerido').min(5, "El nombre del  puesto debe ser mayor a 5 caracteres").max(100),
61
-    puestosuperior: Yup.string().required("El puesto es requerido").min(5, "El nombre del  puesto debe ser mayor a 5 caracteres").max(30),
78
+    puestosuperior: Yup.number().required("El puesto superior es requerido"),
62 79
     aredepto: Yup.number().required('Escoge alguna área'),
63 80
     fecha: Yup.date("Ingresa una fecha válida"),
64 81
     notas: Yup.string("Ingresa una nota válida").min(5, "Ingresa una nota válida").max(150),
@@ -75,11 +92,11 @@ function Manual(props) {
75 92
   const [options, setOptions] = React.useState([]);
76 93
   const [dialogValue, setDialogValueHook] = React.useState({
77 94
     title: '',
78
-    year: '',
95
+    id: '',
79 96
   });
80 97
 
81 98
   let setDialogValue = (value) => {
82
-    console.log('llamada', value)
99
+    // console.log('llamada', value)
83 100
     // setValues({...values, puestosuperior: value?.title })
84 101
     setDialogValueHook(value)
85 102
   }
@@ -117,17 +134,26 @@ function Manual(props) {
117 134
   const handleCloseDialog = () => {
118 135
     setDialogValue({
119 136
       title: '',
120
-      year: '',
137
+      id: '',
121 138
     });
122 139
     toggleOpenDialog(false);
123 140
   };
124 141
 
125 142
 
126
-  const handleSubmitDialog = (event) => {
143
+  const handleSubmitDialog = async (event) => {
127 144
     event.preventDefault();
145
+    console.log('to save: ', dialogValue)
146
+    let { id, nombre } = await savePuestoSuperior(dialogValue, auth)
147
+    if (id) {
148
+      setDialogValue({
149
+        title: nombre,
150
+        id: id,
151
+      });
152
+    }
153
+
128 154
     setDialogValue({
129 155
       title: dialogValue.title,
130
-      year: parseInt(dialogValue.year, 10),
156
+      id: dialogValue.id
131 157
     });
132 158
     handleCloseDialog();
133 159
   };
@@ -135,20 +161,21 @@ function Manual(props) {
135 161
   const AutoCompleteChange = (event, newValue) => {
136 162
     console.log('newValue', newValue)
137 163
     // TODO : formik logic to validate autocomplete
164
+    setValues({ ...values, puestosuperior: newValue?.id })
138 165
 
139 166
     if (typeof newValue === 'string') {
140 167
       setTimeout(() => {
141 168
         toggleOpenDialog(true);
142 169
         setDialogValue({
143 170
           title: newValue,
144
-          year: '',
171
+          id: '',
145 172
         });
146 173
       });
147 174
     } else if (newValue && newValue.inputValue) {
148 175
       toggleOpenDialog(true);
149 176
       setDialogValue({
150 177
         title: newValue.inputValue,
151
-        year: '',
178
+        id: '',
152 179
       });
153 180
     } else {
154 181
       setDialogValue(newValue);
@@ -166,7 +193,7 @@ function Manual(props) {
166 193
   const formik = useFormik({
167 194
     initialValues: {
168 195
       nombrepuesto: "",
169
-      puestosuperior: "",
196
+      puestosuperior: null,
170 197
       aredepto: 1,
171 198
       fecha: date,
172 199
       notas: "",
@@ -265,11 +292,11 @@ function Manual(props) {
265 292
               <TextField
266 293
                 margin="dense"
267 294
                 id="name"
268
-                value={dialogValue?.year}
295
+                value={dialogValue?.id}
269 296
                 onChange={(event) =>
270 297
                   setDialogValue({
271 298
                     ...dialogValue,
272
-                    year: event.target.value,
299
+                    id: event.target.value,
273 300
                   })
274 301
                 }
275 302
                 label="Descripción"
@@ -386,7 +413,7 @@ function Manual(props) {
386 413
                               setOptions([]);
387 414
                               setDialogValue({
388 415
                                 title: event.target.value,
389
-                                year: '',
416
+                                id: '',
390 417
                               });
391 418
                             },
392 419
                             endAdornment: (