Sfoglia il codice sorgente

fix bug on edit plz

amenpunk 2 anni fa
parent
commit
201d05d363
2 ha cambiato i file con 119 aggiunte e 103 eliminazioni
  1. 24 20
      src/Components/Modal/AgregarManual.js
  2. 95 83
      src/Components/Modal/EditPlaza.js

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

@@ -95,10 +95,13 @@ function Manual(props) {
   });
 
   let setDialogValue = (value) => {
-    if (value?.id !== undefined && isNaN(value?.id) === false) {
-      setValues({ ...values, puestosuperior: value?.id })
-    }
+    // console.log('setDialogValue',value)
+    // if (value?.id !== undefined && isNaN(value?.id) === false) {
+    //   console.log('first if')
+    setValues({ ...values, puestosuperior: value?.id })
+    // }
     setDialogValueHook(value)
+    // console.log(values)
   }
 
   const loading = openSugg && options.length === 0;
@@ -123,8 +126,6 @@ function Manual(props) {
 
   }, [loading, dialogValue, auth]);
 
-
-
   const handleClose = () => false
 
   const changeDepartamento = (event) => {
@@ -152,8 +153,8 @@ function Manual(props) {
   };
 
   const AutoCompleteChange = (event, newValue) => {
+    // FIX: este set es el que realmente agrega al estado
     setValues({ ...values, puestosuperior: newValue?.id })
-
     if (typeof newValue === 'string') {
       setTimeout(() => {
         toggleOpenDialog(true);
@@ -264,6 +265,7 @@ function Manual(props) {
               </DialogContentText>
 
               <TextField
+                fullWidth
                 autoFocus
                 margin="dense"
                 id="name"
@@ -279,20 +281,22 @@ function Manual(props) {
                 variant="standard"
               />
 
-              <TextField
-                margin="dense"
-                id="name"
-                value={dialogValue?.id}
-                onChange={(event) =>
-                  setDialogValue({
-                    ...dialogValue,
-                    id: event.target.value,
+              {/*
+                <TextField
+                  margin="dense"
+                  id="name"
+                  value={dialogValue?.id}
+                  onChange={(event) =>
+                      setDialogValue({
+                        ...dialogValue,
+                        id: event.target.value,
                   })
-                }
-                label="Descripción"
-                type="text"
-                variant="standard"
+                  }
+                  label="Descripción"
+                  type="text"
+                  variant="standard"
               />
+              */}
             </DialogContent>
             <DialogActions>
               <Button onClick={handleCloseDialog}>Cancelar</Button>
@@ -366,7 +370,7 @@ function Manual(props) {
                         if (params.inputValue !== '') {
                           filtered.push({
                             inputValue: params.inputValue,
-                            title: `Add "${params.inputValue}"`,
+                            title: `Agregar "${params.inputValue}"`,
                           });
                         }
 
@@ -387,7 +391,7 @@ function Manual(props) {
                       selectOnFocus
                       clearOnBlur
                       handleHomeEndKeys
-                      renderOption={(props, option) => <li {...props}>{option.title}</li>}
+                      renderOption={(props, option) => <li key={option.id} {...props}>{option.title}</li>}
                       freeSolo
                       renderInput={(params) => (
                         <TextField

+ 95 - 83
src/Components/Modal/EditPlaza.js

@@ -27,7 +27,7 @@ import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
 const filter = createFilterOptions();
 
 async function getPuestoSuperior(puesto, auth) {
-  console.log('P:', puesto)
+  // console.log('P:', puesto)
   if (puesto.length < 2) return []
   let rest = new Service(`/plaza/keypuestosup?keyword=${puesto}`)
   let result = await rest.get(auth.token)
@@ -72,7 +72,7 @@ const plazeSchema = Yup.object({
 
 function Edit(props) {
 
-  const { setValue, getValues, reset, control, register, handleSubmit, formState: { errors } } = useForm({
+  const {setValue, getValues, reset, control, register, handleSubmit, formState: { errors } } = useForm({
     resolver: yupResolver(plazeSchema),
     defaultValues: {
       id: 0,
@@ -150,25 +150,38 @@ function Edit(props) {
     console.log('value receivied', value)
     let id = value?.id || 0;
     if (id !== 0) {
-      setValue('puestosuperior', id)
-      let temp = getValues();
+      console.log('setting value', id)
+      // let temp = getValues();
       // temp['puestosuperior'] = id
       // temp['puestosuperior_id'] = id
-      console.log("TEMP", temp)
-      reset({ ...temp, puestosuperior: id })
+      // reset({ ...temp, puestosuperior: id })
+      // setDialogValueHook(value)
+      // reset({ ...temp, puestosuperior_id: id,puestosuperior:value.title })
+      // reset()
+      setValue('puestosuperior', parseInt( id ))
+      // setFocus("notas", { shouldSelect: true })
+      // // resetField('puestosuperior',{keepError:false})
+      //
+
       // setDialogValueHook(value)
     }
-    console.log('SETTING:', value)
+    // console.log('SETTING:', value)
     setDialogValueHook(value)
-  }, [setValue, reset, getValues])
+    console.log("final", getValues())
+
+  }, [setValue, getValues])
 
   const loading = openSugg && options.length === 0;
 
   const AutoCompleteChange = (event, newValue) => {
-    console.log('newValue', newValue)
+    let temp = getValues();
+    console.log('newValue', temp, newValue)
+    temp['puestosuperior'] = newValue?.id;
+    // reset(temp)
+    // setDialogValue(newValue);
+    // return;
     //this should be change
-    // setValue('puestosuperior', newValue?.id)
-
+    setValue('puestosuperior', newValue?.id)
     if (typeof newValue === 'string') {
       console.log('if1')
       setTimeout(() => {
@@ -251,6 +264,7 @@ function Edit(props) {
             tests: temp_test,
             puestosuperior: puestosuperior.id
           })
+          // console.log("INITIAL:", getValues())
         })
         .catch(e => console.log(e))
 
@@ -383,89 +397,87 @@ function Edit(props) {
                   {...register("nombrepuesto")} />
 
 
-                {/*
-                  <FormControl fullWidth>
+                <FormControl fullWidth>
                   <Controller
                     helperText={errors.puestosuperior?.message}
                     error={errors?.puestosuperior}
                     name="puestosuperior"
                     control={control}
-                  render={({ field }) =>
-                    <Autocomplete
-                      fullWidth
-                      value={dialogValue}
-                      onChange={AutoCompleteChange}
-                      open={openSugg}
-                    onOpen={() => {
-                      setOpenSugg(true);
-                      }}
-                    onClose={() => {
-                      setOpenSugg(false);
-                      }}
-                      isOptionEqualToValue={(option, value) => option.title === value.title}
-                    filterOptions={(options, params) => {
-                      const filtered = filter(options, params);
-
-                      if (params.inputValue !== '') {
-                        filtered.push({
-                          inputValue: params.inputValue,
-                          title: `Agregar "${params.inputValue}"`,
-                        });
-                      }
-
-                      return filtered;
-                      }}
-                      id="puesto_superior_autocomplete"
-                      options={options}
-                      loading={loading}
-                    getOptionLabel={(option) => {
-                      if (typeof option === 'string') {
-                        return option;
-                      }
-                      if (option.inputValue) {
-                        return option.inputValue;
-                      }
-                      return option.title;
-                      }}
-                      selectOnFocus
-                      clearOnBlur
-                      handleHomeEndKeys
-                      renderOption={(props, option) => <li key={option.id} {...props}>{option.title}</li>}
-                      freeSolo
-                    renderInput={(params) => (
-                      <TextField
-                        {...params}
-                        {...register('puestosuperior')}
-                        error={Boolean(errors.puestosuperior)}
-                        label="Puesto Superior"
-                      InputProps={{
-                        ...params.InputProps,
-                          onChange: (event) => {
-                            // let title = event.target.value;
-                            // console.log('titulo',title)
-                            setOptions([]);
-                            setDialogValue({
-                              title: event.target.value,
-                              id: '',
+                    render={({ field }) =>
+                      <Autocomplete
+                        fullWidth
+                        value={dialogValue}
+                        onChange={AutoCompleteChange}
+                        open={openSugg}
+                        onOpen={() => {
+                          setOpenSugg(true);
+                        }}
+                        onClose={() => {
+                          setOpenSugg(false);
+                        }}
+                        isOptionEqualToValue={(option, value) => option.title === value.title}
+                        filterOptions={(options, params) => {
+                          const filtered = filter(options, params);
+
+                          if (params.inputValue !== '') {
+                            filtered.push({
+                              inputValue: params.inputValue,
+                              title: `Agregar "${params.inputValue}"`,
                             });
-                          },
-                          endAdornment: (
-                        <React.Fragment>
-                        {loading ? <CircularProgress color="inherit" size={20} /> : null}
-                        {params.InputProps.endAdornment}
-                            </React.Fragment>
-                          ),
+                          }
+
+                          return filtered;
                         }}
-                      />
-                      )}
+                        id="puesto_superior_autocomplete"
+                        options={options}
+                        loading={loading}
+                        getOptionLabel={(option) => {
+                          if (typeof option === 'string') {
+                            return option;
+                          }
+                          if (option.inputValue) {
+                            return option.inputValue;
+                          }
+                          return option.title;
+                        }}
+                        selectOnFocus
+                        clearOnBlur
+                        handleHomeEndKeys
+                        renderOption={(props, option) => <li key={option.id} {...props}>{option.title}</li>}
+                        freeSolo
+                        renderInput={(params) => (
+                          <TextField
+                            {...params}
+                            {...register('puestosuperior')}
+                            error={Boolean(errors.puestosuperior)}
+                            label="Puesto Superior"
+                            InputProps={{
+                              ...params.InputProps,
+                              onChange: (event) => {
+                                let title = event.target.value;
+                                console.log('titulo',title)
+                                setOptions([]);
+                                setDialogValue({
+                                  title: event.target.value,
+                                  id: '',
+                                });
+                              },
+                              endAdornment: (
+                                <React.Fragment>
+                                  {loading ? <CircularProgress color="inherit" size={20} /> : null}
+                                  {params.InputProps.endAdornment}
+                                </React.Fragment>
+                              ),
+                            }}
+                          />
+                        )}
 
                       />
 
                     }
-                    >
-                        </Controller>
-                    </FormControl>
-                    */}
+                  >
+                  </Controller>
+                </FormControl>
 
               </Stack>