Browse Source

fix puesto superior search

amenpunk 1 year ago
parent
commit
d292aafe56
4 changed files with 216 additions and 46 deletions
  1. 24 13
      src/Components/Modal/AgregarManual.js
  2. 189 27
      src/Components/Modal/EditPlaza.js
  3. 1 1
      src/Pages/Login.jsx
  4. 2 5
      src/Pages/Puestos.jsx

+ 24 - 13
src/Components/Modal/AgregarManual.js

96
   });
96
   });
97
 
97
 
98
   let setDialogValue = (value) => {
98
   let setDialogValue = (value) => {
99
-    // console.log('llamada', value)
100
-    // setValues({...values, puestosuperior: value?.title })
99
+  console.log("DIALGO RECE:", value)
100
+    if(value?.id !== undefined && isNaN(value?.id) === false ){
101
+      setValues({...values, puestosuperior: value?.id })
102
+    }
101
     setDialogValueHook(value)
103
     setDialogValueHook(value)
102
   }
104
   }
103
 
105
 
132
   };
134
   };
133
 
135
 
134
   const handleCloseDialog = () => {
136
   const handleCloseDialog = () => {
135
-    setDialogValue({
136
-      title: '',
137
-      id: '',
138
-    });
137
+    // setDialogValue({
138
+    //   title: '',
139
+    //   id: '',
140
+    // });
139
     toggleOpenDialog(false);
141
     toggleOpenDialog(false);
140
   };
142
   };
141
 
143
 
144
     event.preventDefault();
146
     event.preventDefault();
145
     console.log('to save: ', dialogValue)
147
     console.log('to save: ', dialogValue)
146
     let { id, nombre } = await savePuestoSuperior(dialogValue, auth)
148
     let { id, nombre } = await savePuestoSuperior(dialogValue, auth)
149
+    console.log('nuevo puesto superior: ', id, nombre)
147
     if (id) {
150
     if (id) {
148
-      setDialogValue({
151
+      let to_set = {
149
         title: nombre,
152
         title: nombre,
150
         id: id,
153
         id: id,
151
-      });
154
+      }
155
+      console.log('have id and set', to_set)
156
+      console.table(to_set)
157
+      if (to_set.id) {
158
+        setDialogValue(to_set);
159
+      }
152
     }
160
     }
153
 
161
 
154
-    setDialogValue({
155
-      title: dialogValue.title,
156
-      id: dialogValue.id
157
-    });
162
+    // setDialogValue({
163
+    //   title: dialogValue.title,
164
+    //   id: dialogValue.id
165
+    // });
166
+console.log(values)
158
     handleCloseDialog();
167
     handleCloseDialog();
159
   };
168
   };
160
 
169
 
163
     setValues({ ...values, puestosuperior: newValue?.id })
172
     setValues({ ...values, puestosuperior: newValue?.id })
164
 
173
 
165
     if (typeof newValue === 'string') {
174
     if (typeof newValue === 'string') {
175
+      console.log('if')
166
       setTimeout(() => {
176
       setTimeout(() => {
167
         toggleOpenDialog(true);
177
         toggleOpenDialog(true);
168
         setDialogValue({
178
         setDialogValue({
171
         });
181
         });
172
       });
182
       });
173
     } else if (newValue && newValue.inputValue) {
183
     } else if (newValue && newValue.inputValue) {
184
+      console.log('else if')
174
       toggleOpenDialog(true);
185
       toggleOpenDialog(true);
175
       setDialogValue({
186
       setDialogValue({
176
         title: newValue.inputValue,
187
         title: newValue.inputValue,
177
         id: '',
188
         id: '',
178
       });
189
       });
179
     } else {
190
     } else {
191
+      console.log('only else')
180
       setDialogValue(newValue);
192
       setDialogValue(newValue);
181
     }
193
     }
182
   }
194
   }
229
   const changeTab = (_event, newValue) => setTab(newValue);
241
   const changeTab = (_event, newValue) => setTab(newValue);
230
 
242
 
231
   const { errors, touched, handleSubmit, getFieldProps, values, setValues } = formik;
243
   const { errors, touched, handleSubmit, getFieldProps, values, setValues } = formik;
232
-  // console.log({ values })
233
 
244
 
234
   const addPrueba = (check, id) => {
245
   const addPrueba = (check, id) => {
235
     let { tests } = values
246
     let { tests } = values

+ 189 - 27
src/Components/Modal/EditPlaza.js

21
 import { Service } from '../../Utils/HTTP';
21
 import { Service } from '../../Utils/HTTP';
22
 import { useSelector } from 'react-redux'
22
 import { useSelector } from 'react-redux'
23
 import { useQuery, useMutation, useQueryClient } from 'react-query'
23
 import { useQuery, useMutation, useQueryClient } from 'react-query'
24
+import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
25
+const filter = createFilterOptions();
26
+
27
+async function getPuestoSuperior(puesto, auth) {
28
+  if (puesto.length < 2) return []
29
+  let rest = new Service(`/plaza/keypuestosup?keyword=${puesto}`)
30
+  let result = await rest.get(auth.token)
31
+  // console.log(result)
32
+  if (result?.data?.length > 0) {
33
+    result = result.data.map((item) => ({ 'title': item.nombre, id: item.id }))
34
+    return result;
35
+  }
36
+  return [];
37
+}
38
+
24
 
39
 
25
 const plazeSchema = Yup.object({
40
 const plazeSchema = Yup.object({
26
   id: Yup.number(),
41
   id: Yup.number(),
42
     resolver: yupResolver(plazeSchema),
57
     resolver: yupResolver(plazeSchema),
43
     defaultValues: {
58
     defaultValues: {
44
       nombrepuesto: 'mingtest',
59
       nombrepuesto: 'mingtest',
45
-      puestosuperior: 0,
60
+      puestosuperior: null,
46
       fecha: '01/01/2019',
61
       fecha: '01/01/2019',
47
       notas: 'esto es un ejemplod e una nota',
62
       notas: 'esto es un ejemplod e una nota',
48
       aredepto: 1,
63
       aredepto: 1,
81
   const [open, setOpen] = React.useState(false);
96
   const [open, setOpen] = React.useState(false);
82
   const [tab, setTab] = React.useState(0);
97
   const [tab, setTab] = React.useState(0);
83
   const [checklist, setChecklist] = React.useState([]);
98
   const [checklist, setChecklist] = React.useState([]);
99
+  const [openSugg, setOpenSugg] = React.useState(false);
100
+  const [options, setOptions] = React.useState([]);
101
+  const [openDialog, toggleOpenDialog] = React.useState(false);
102
+  const [dialogValue, setDialogValueHook] = React.useState({
103
+    title: '',
104
+    id: '',
105
+  });
84
 
106
 
85
-  // const changePuestoSup = (event) => {
86
-  //   setPuestoSup(event.target.value);
87
-  // };
107
+  let setDialogValue = (value) => {
108
+    // console.log('llamada', value)
109
+    // setValues({...values, puestosuperior: value?.title })
110
+    setDialogValueHook(value)
111
+  }
112
+
113
+  const loading = openSugg && options.length === 0;
114
+
115
+  const AutoCompleteChange = (event, newValue) => {
116
+    console.log('newValue', newValue)
117
+    setValue('puestosuperior', newValue?.id)
118
+
119
+    if (typeof newValue === 'string') {
120
+      setTimeout(() => {
121
+        toggleOpenDialog(true);
122
+        setDialogValue({
123
+          title: newValue,
124
+          id: '',
125
+        });
126
+      });
127
+    } else if (newValue && newValue.inputValue) {
128
+      toggleOpenDialog(true);
129
+      setDialogValue({
130
+        title: newValue.inputValue,
131
+        id: '',
132
+      });
133
+    } else {
134
+      setDialogValue(newValue);
135
+    }
136
+  }
88
 
137
 
89
   const addPrueba = (check, id) => {
138
   const addPrueba = (check, id) => {
90
     let tests = getValues("tests")
139
     let tests = getValues("tests")
115
   }
164
   }
116
 
165
 
117
   const puestoMutation = useMutation(updatePuesto)
166
   const puestoMutation = useMutation(updatePuesto)
118
-
119
   const close = () => toggle("EDIT", { id: null });
167
   const close = () => toggle("EDIT", { id: null });
120
 
168
 
121
   const { data: categories } = useQuery('categories', getCategories);
169
   const { data: categories } = useQuery('categories', getCategories);
122
   const { data: testsCatalog } = useQuery('tests', getTest);
170
   const { data: testsCatalog } = useQuery('tests', getTest);
123
 
171
 
124
   useEffect(() => {
172
   useEffect(() => {
173
+
125
     if (visible == null) return;
174
     if (visible == null) return;
126
     let rest = new Service(`/plaza/getthis/${visible}`)
175
     let rest = new Service(`/plaza/getthis/${visible}`)
127
     rest
176
     rest
128
       .getQuery(auth.token)
177
       .getQuery(auth.token)
129
       .then(response => {
178
       .then(response => {
130
-        console.log('puesto edit: ',response.data)
131
-        let { areadeptoplz_id, fecha, tests } = response.data;
179
+        let { areadeptoplz_id, fecha, tests, puestosuperior } = response.data;
132
         let temp_test = tests.map(t => ({ id: t.id }))
180
         let temp_test = tests.map(t => ({ id: t.id }))
133
         setChecklist(temp_test.map(t => t.id))
181
         setChecklist(temp_test.map(t => t.id))
182
+        console.log('puesto sup',puestosuperior)
183
+        reset({ 
184
+          ...response.data, 
185
+          aredepto: areadeptoplz_id, 
186
+          fecha: new Date(fecha), 
187
+          tests: temp_test, 
188
+          puestosuperior: puestosuperior.id
189
+        })
190
+        // setDialogValue({ title: puestosuperior.nombre, id: puestosuperior.id })
191
+      })
192
+      .catch(e => console.log(e))
193
+
194
+
195
+    let active = true;
196
+    if (!loading) {
197
+      return undefined;
198
+    }
199
+
200
+    (async () => {
201
+      let puestos = await getPuestoSuperior(dialogValue.title, auth)
202
+      if (active) {
203
+        setOptions(puestos);
204
+      }
205
+    })();
206
+
207
+    return () => {
208
+      active = false;
209
+    };
134
 
210
 
135
 
211
 
136
-        reset({ ...response.data, aredepto: areadeptoplz_id, fecha: new Date(fecha), tests: temp_test })
137
-      })
138
-      .catch(console.log)
139
-  }, [visible, auth, reset])
212
+
213
+  }, [visible, auth, reset, loading, dialogValue])
140
 
214
 
141
   const changeTab = (_event, newValue) => setTab(newValue);
215
   const changeTab = (_event, newValue) => setTab(newValue);
142
 
216
 
193
                   error={Boolean(errors?.nombrepuesto)}
267
                   error={Boolean(errors?.nombrepuesto)}
194
                   {...register("nombrepuesto")} />
268
                   {...register("nombrepuesto")} />
195
 
269
 
270
+
196
                 <FormControl fullWidth>
271
                 <FormControl fullWidth>
197
-                  <InputLabel id="demo-simple-select-label">Puesto Superior</InputLabel>
198
                   <Controller
272
                   <Controller
199
                     helperText={errors.puestosuperior?.message}
273
                     helperText={errors.puestosuperior?.message}
200
-                    error={Boolean(errors?.puestosuperior)}
274
+                    error={errors?.puestosuperior}
201
                     name="puestosuperior"
275
                     name="puestosuperior"
202
                     control={control}
276
                     control={control}
203
-                    render={({field}) => 
204
-                      <Select {...field}>
205
-                        {
206
-                          categories ?
207
-                            categories.data.map(cate => {
208
-                              return (
209
-                                <MenuItem key={cate.id} value={cate.id}>{cate.nombre}</MenuItem>
210
-                              )
211
-                            })
212
-                            : <MenuItem>Null</MenuItem>
213
-                        }
214
-                      </Select>
277
+                    render={({ field }) =>
278
+                      <Autocomplete
279
+                        fullWidth
280
+                        value={dialogValue}
281
+                        onChange={AutoCompleteChange}
282
+                        open={openSugg}
283
+                        onOpen={() => {
284
+                          setOpenSugg(true);
285
+                        }}
286
+                        onClose={() => {
287
+                          setOpenSugg(false);
288
+                        }}
289
+                        isOptionEqualToValue={(option, value) => option.title === value.title}
290
+                        filterOptions={(options, params) => {
291
+                          const filtered = filter(options, params);
292
+
293
+                          if (params.inputValue !== '') {
294
+                            filtered.push({
295
+                              inputValue: params.inputValue,
296
+                              title: `Add "${params.inputValue}"`,
297
+                            });
298
+                          }
299
+
300
+                          return filtered;
301
+                        }}
302
+                        id="puesto_superior_autocomplete"
303
+                        options={options}
304
+                        loading={loading}
305
+                        getOptionLabel={(option) => {
306
+                          if (typeof option === 'string') {
307
+                            return option;
308
+                          }
309
+                          if (option.inputValue) {
310
+                            return option.inputValue;
311
+                          }
312
+                          return option.title;
313
+                        }}
314
+                        selectOnFocus
315
+                        clearOnBlur
316
+                        handleHomeEndKeys
317
+                        renderOption={(props, option) => <li {...props}>{option.title}</li>}
318
+                        freeSolo
319
+                        renderInput={(params) => (
320
+                          <TextField
321
+                            {...params}
322
+                            {...register('puestosuperior')}
323
+                            error={Boolean(errors.puestosuperior)}
324
+                            label="Puesto Superior"
325
+                            InputProps={{
326
+                              ...params.InputProps,
327
+                              onChange: (event) => {
328
+                                // let title = event.target.value;
329
+                                // console.log('titulo',title)
330
+                                setOptions([]);
331
+                                setDialogValue({
332
+                                  title: event.target.value,
333
+                                  id: '',
334
+                                });
335
+                              },
336
+                              endAdornment: (
337
+                                <React.Fragment>
338
+                                  {loading ? <CircularProgress color="inherit" size={20} /> : null}
339
+                                  {params.InputProps.endAdornment}
340
+                                </React.Fragment>
341
+                              ),
342
+                            }}
343
+                          />
344
+                        )}
345
+
346
+                      />
347
+
215
                     }
348
                     }
216
-                    >
217
-                    
349
+                  >
218
                   </Controller>
350
                   </Controller>
219
                 </FormControl>
351
                 </FormControl>
352
+
353
+
354
+                {
355
+                  /* <FormControl fullWidth>
356
+                  <InputLabel id="demo-simple-select-label">Puesto Superior</InputLabel>
357
+                  <Controller
358
+                    helperText={errors.puestosuperior?.message}
359
+                    error={Boolean(errors?.puestosuperior)}
360
+                    name="puestosuperior"
361
+                    control={control}
362
+                  render={({field}) => 
363
+                    <Select {...field}>
364
+                      {
365
+                        categories ?
366
+                          categories.data.map(cate => {
367
+                            return (
368
+                              <MenuItem key={cate.id} value={cate.id}>{cate.nombre}</MenuItem>
369
+                            )
370
+                      })
371
+                      : <MenuItem>Null</MenuItem>
372
+                      }
373
+                        </Select>
374
+                    }
375
+                  >
376
+
377
+                    </Controller>
378
+                  </FormControl> */
379
+                }
380
+
381
+
220
               </Stack>
382
               </Stack>
221
 
383
 
222
               <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
384
               <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>

+ 1 - 1
src/Pages/Login.jsx

116
           sm={4}
116
           sm={4}
117
           md={7}
117
           md={7}
118
           sx={{
118
           sx={{
119
-            backgroundImage: 'url(https://source.unsplash.com/random)',
119
+            backgroundImage: 'url(https://images.squarespace-cdn.com/content/v1/5c0ec3e30dbda3392d9c9d2d/1548101170354-A2I9LZIEE5CLJ1V5ZGAG/Psychometrictesting.jpg?format=2500w)',
120
             backgroundRepeat: 'no-repeat',
120
             backgroundRepeat: 'no-repeat',
121
             backgroundColor: (t) => t.palette.mode === 'light' ? t.palette.grey[50] : t.palette.grey[900],
121
             backgroundColor: (t) => t.palette.mode === 'light' ? t.palette.grey[50] : t.palette.grey[900],
122
             backgroundSize: 'cover',
122
             backgroundSize: 'cover',

+ 2 - 5
src/Pages/Puestos.jsx

47
     cacheTime: 0,
47
     cacheTime: 0,
48
   });
48
   });
49
 
49
 
50
-
51
-console.log(isLoading,result, error)
52
-
53
   const changePage = useCallback((_, value) => setPage(value), []);
50
   const changePage = useCallback((_, value) => setPage(value), []);
54
   const [alignment, setAlignment] = React.useState('grid');
51
   const [alignment, setAlignment] = React.useState('grid');
55
   const handleChange = useCallback((_event, newAlignment) => setAlignment(newAlignment), [])
52
   const handleChange = useCallback((_event, newAlignment) => setAlignment(newAlignment), [])
139
                       index={page - 1}
136
                       index={page - 1}
140
                     /> : undefined
137
                     /> : undefined
141
                   }
138
                   }
142
-                  {!result && !isLoading && <center><h1>No tienes nigun puesto</h1></center>}
139
+                  {!result && !isLoading && <center><h1>No tienes ningún puesto</h1></center>}
143
                 </Row>
140
                 </Row>
144
               </div>
141
               </div>
145
               <div className={`main_list_products ${alignment === 'list' ? 'activar_vista' : 'desactivar_vista'}`} id="list_view_products">
142
               <div className={`main_list_products ${alignment === 'list' ? 'activar_vista' : 'desactivar_vista'}`} id="list_view_products">
153
                       index={page - 1}
150
                       index={page - 1}
154
                     /> : undefined
151
                     /> : undefined
155
                   }
152
                   }
156
-                  {!result && !isLoading && <center><h1>No tienes nigun puesto</h1></center>}
153
+                  {!result && !isLoading && <center><h1>No tienes nigún puesto</h1></center>}
157
                 </Row>
154
                 </Row>
158
               </div>
155
               </div>
159
 
156