Преглед на файлове

fix puesto superior search

amenpunk преди 1 година
родител
ревизия
d292aafe56
променени са 4 файла, в които са добавени 216 реда и са изтрити 46 реда
  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,8 +96,10 @@ function Manual(props) {
96 96
   });
97 97
 
98 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 103
     setDialogValueHook(value)
102 104
   }
103 105
 
@@ -132,10 +134,10 @@ function Manual(props) {
132 134
   };
133 135
 
134 136
   const handleCloseDialog = () => {
135
-    setDialogValue({
136
-      title: '',
137
-      id: '',
138
-    });
137
+    // setDialogValue({
138
+    //   title: '',
139
+    //   id: '',
140
+    // });
139 141
     toggleOpenDialog(false);
140 142
   };
141 143
 
@@ -144,17 +146,24 @@ function Manual(props) {
144 146
     event.preventDefault();
145 147
     console.log('to save: ', dialogValue)
146 148
     let { id, nombre } = await savePuestoSuperior(dialogValue, auth)
149
+    console.log('nuevo puesto superior: ', id, nombre)
147 150
     if (id) {
148
-      setDialogValue({
151
+      let to_set = {
149 152
         title: nombre,
150 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 167
     handleCloseDialog();
159 168
   };
160 169
 
@@ -163,6 +172,7 @@ function Manual(props) {
163 172
     setValues({ ...values, puestosuperior: newValue?.id })
164 173
 
165 174
     if (typeof newValue === 'string') {
175
+      console.log('if')
166 176
       setTimeout(() => {
167 177
         toggleOpenDialog(true);
168 178
         setDialogValue({
@@ -171,12 +181,14 @@ function Manual(props) {
171 181
         });
172 182
       });
173 183
     } else if (newValue && newValue.inputValue) {
184
+      console.log('else if')
174 185
       toggleOpenDialog(true);
175 186
       setDialogValue({
176 187
         title: newValue.inputValue,
177 188
         id: '',
178 189
       });
179 190
     } else {
191
+      console.log('only else')
180 192
       setDialogValue(newValue);
181 193
     }
182 194
   }
@@ -229,7 +241,6 @@ function Manual(props) {
229 241
   const changeTab = (_event, newValue) => setTab(newValue);
230 242
 
231 243
   const { errors, touched, handleSubmit, getFieldProps, values, setValues } = formik;
232
-  // console.log({ values })
233 244
 
234 245
   const addPrueba = (check, id) => {
235 246
     let { tests } = values

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

@@ -21,6 +21,21 @@ import toast, { Toaster } from 'react-hot-toast';
21 21
 import { Service } from '../../Utils/HTTP';
22 22
 import { useSelector } from 'react-redux'
23 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 40
 const plazeSchema = Yup.object({
26 41
   id: Yup.number(),
@@ -42,7 +57,7 @@ function Edit(props) {
42 57
     resolver: yupResolver(plazeSchema),
43 58
     defaultValues: {
44 59
       nombrepuesto: 'mingtest',
45
-      puestosuperior: 0,
60
+      puestosuperior: null,
46 61
       fecha: '01/01/2019',
47 62
       notas: 'esto es un ejemplod e una nota',
48 63
       aredepto: 1,
@@ -81,10 +96,44 @@ function Edit(props) {
81 96
   const [open, setOpen] = React.useState(false);
82 97
   const [tab, setTab] = React.useState(0);
83 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 138
   const addPrueba = (check, id) => {
90 139
     let tests = getValues("tests")
@@ -115,28 +164,53 @@ function Edit(props) {
115 164
   }
116 165
 
117 166
   const puestoMutation = useMutation(updatePuesto)
118
-
119 167
   const close = () => toggle("EDIT", { id: null });
120 168
 
121 169
   const { data: categories } = useQuery('categories', getCategories);
122 170
   const { data: testsCatalog } = useQuery('tests', getTest);
123 171
 
124 172
   useEffect(() => {
173
+
125 174
     if (visible == null) return;
126 175
     let rest = new Service(`/plaza/getthis/${visible}`)
127 176
     rest
128 177
       .getQuery(auth.token)
129 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 180
         let temp_test = tests.map(t => ({ id: t.id }))
133 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 215
   const changeTab = (_event, newValue) => setTab(newValue);
142 216
 
@@ -193,30 +267,118 @@ function Edit(props) {
193 267
                   error={Boolean(errors?.nombrepuesto)}
194 268
                   {...register("nombrepuesto")} />
195 269
 
270
+
196 271
                 <FormControl fullWidth>
197
-                  <InputLabel id="demo-simple-select-label">Puesto Superior</InputLabel>
198 272
                   <Controller
199 273
                     helperText={errors.puestosuperior?.message}
200
-                    error={Boolean(errors?.puestosuperior)}
274
+                    error={errors?.puestosuperior}
201 275
                     name="puestosuperior"
202 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 350
                   </Controller>
219 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 382
               </Stack>
221 383
 
222 384
               <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>

+ 1 - 1
src/Pages/Login.jsx

@@ -116,7 +116,7 @@ export function Login() {
116 116
           sm={4}
117 117
           md={7}
118 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 120
             backgroundRepeat: 'no-repeat',
121 121
             backgroundColor: (t) => t.palette.mode === 'light' ? t.palette.grey[50] : t.palette.grey[900],
122 122
             backgroundSize: 'cover',

+ 2 - 5
src/Pages/Puestos.jsx

@@ -47,9 +47,6 @@ export function Puestos() {
47 47
     cacheTime: 0,
48 48
   });
49 49
 
50
-
51
-console.log(isLoading,result, error)
52
-
53 50
   const changePage = useCallback((_, value) => setPage(value), []);
54 51
   const [alignment, setAlignment] = React.useState('grid');
55 52
   const handleChange = useCallback((_event, newAlignment) => setAlignment(newAlignment), [])
@@ -139,7 +136,7 @@ console.log(isLoading,result, error)
139 136
                       index={page - 1}
140 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 140
                 </Row>
144 141
               </div>
145 142
               <div className={`main_list_products ${alignment === 'list' ? 'activar_vista' : 'desactivar_vista'}`} id="list_view_products">
@@ -153,7 +150,7 @@ console.log(isLoading,result, error)
153 150
                       index={page - 1}
154 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 154
                 </Row>
158 155
               </div>
159 156