|
|
@@ -1,24 +1,26 @@
|
|
|
-import React, { memo, useCallback, useMemo, useEffect } from 'react';
|
|
|
-import * as Yup from 'yup';
|
|
|
-import { useFormik, Form, FormikProvider } from 'formik';
|
|
|
+import React, { memo, useMemo, useEffect } from 'react';
|
|
|
import { Modal } from 'react-bootstrap'
|
|
|
-import toast, { Toaster } from 'react-hot-toast';
|
|
|
+import { useForm, Controller } from "react-hook-form";
|
|
|
+import { yupResolver } from '@hookform/resolvers/yup';
|
|
|
+import * as Yup from 'yup';
|
|
|
|
|
|
import DateFnsUtils from '@date-io/date-fns';
|
|
|
import { DesktopDatePicker, LocalizationProvider } from '@mui/lab';
|
|
|
import { TabPanel } from './TabPanel'
|
|
|
|
|
|
import {
|
|
|
- Button, Stack, TextField, MenuItem, FormControl, InputLabel, Select,
|
|
|
- Backdrop, CircularProgress,
|
|
|
- Tabs,Tab,Box, Divider,FormGroup,FormControlLabel,Checkbox
|
|
|
+ Button, Stack, TextField, MenuItem, FormControl, InputLabel, Select,
|
|
|
+ Backdrop, CircularProgress,
|
|
|
+ Tabs,Tab,Box, Divider,FormGroup,FormControlLabel,Checkbox
|
|
|
} from '@mui/material';
|
|
|
|
|
|
+import toast, { Toaster } from 'react-hot-toast';
|
|
|
+
|
|
|
import { Service } from '../../Utils/HTTP';
|
|
|
import { useSelector } from 'react-redux'
|
|
|
import { useQuery, useMutation, useQueryClient } from 'react-query'
|
|
|
|
|
|
-const NewPlazaSchema = Yup.object().shape({
|
|
|
+const plazeSchema = Yup.object({
|
|
|
id: Yup.number(),
|
|
|
nombrepuesto:
|
|
|
Yup.string().required('El nombre es requerido')
|
|
|
@@ -29,30 +31,38 @@ const NewPlazaSchema = Yup.object().shape({
|
|
|
fecha: Yup.date("Ingresa una fecha válida"),
|
|
|
notas: Yup.string("Ingresa una nota válida").min(5).max(150),
|
|
|
tests: Yup.array()
|
|
|
-})
|
|
|
-
|
|
|
+}).required();
|
|
|
|
|
|
|
|
|
function Edit(props) {
|
|
|
|
|
|
+ const {
|
|
|
+ reset,control,register, handleSubmit, formState: { errors } } = useForm({
|
|
|
+ resolver : yupResolver(plazeSchema),
|
|
|
+ defaultValues: {
|
|
|
+ nombrepuesto: 'mingtest',
|
|
|
+ puestosuperior: 77,
|
|
|
+ fecha: '01/01/2019',
|
|
|
+ notas: 'esto es un ejemplod e una nota',
|
|
|
+ aredepto : 1,
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ const onSubmit = data => {
|
|
|
+ console.log("SUBMIT: ",data)
|
|
|
+ }
|
|
|
+
|
|
|
const now = useMemo(() => new Date(), [])
|
|
|
const auth = useSelector((state) => state.token)
|
|
|
- const queryClient = useQueryClient()
|
|
|
+ // const queryClient = useQueryClient()
|
|
|
let { visible, toggle } = props
|
|
|
|
|
|
- const [departamento, setDepartamento] = React.useState('');
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
-
|
|
|
- const changeDepartamento = useCallback((event) => {
|
|
|
- setDepartamento(event.target.value);
|
|
|
- }, []);
|
|
|
-
|
|
|
- const [date, setDate] = React.useState(now);
|
|
|
const [tab, setTab] = React.useState(0);
|
|
|
const [checklist, setChecklist] = React.useState([]);
|
|
|
|
|
|
const addPrueba = (check,id) => {
|
|
|
- let { tests } = values
|
|
|
+ let { tests } = { tests : []}
|
|
|
let temp ;
|
|
|
if(check){
|
|
|
temp = [...tests, {id}]
|
|
|
@@ -60,7 +70,7 @@ function Edit(props) {
|
|
|
temp = tests.filter( test => test.id !== id);
|
|
|
}
|
|
|
setChecklist(temp.map( test => test.id) )
|
|
|
- setValues({...values, tests: temp})
|
|
|
+ // setValues({...values, tests: temp})
|
|
|
};
|
|
|
|
|
|
const getCategories = async () => {
|
|
|
@@ -82,62 +92,22 @@ function Edit(props) {
|
|
|
|
|
|
const close = () => toggle("EDIT", {id : null});
|
|
|
|
|
|
- const { data } = useQuery('categories', getCategories);
|
|
|
+ const { data: categories } = useQuery('categories', getCategories);
|
|
|
const { data: testsCatalog } = useQuery('tests', getTest);
|
|
|
|
|
|
- const formik = useFormik({
|
|
|
- initialValues: {
|
|
|
- id: 1,
|
|
|
- nombrepuesto: "",
|
|
|
- puestosuperior: 1,
|
|
|
- aredepto: 1,
|
|
|
- fecha: now,
|
|
|
- notas: "",
|
|
|
- tests : []
|
|
|
- },
|
|
|
- onSubmit: (fields, { resetForm }) => {
|
|
|
- setOpen(true);
|
|
|
- fields['fecha'] = new Date(fields.fecha).toISOString();
|
|
|
-
|
|
|
- puestoMutation.mutate(fields, {
|
|
|
- onSuccess: () => {
|
|
|
- close();
|
|
|
- setOpen(false)
|
|
|
- toast.success("Puesto Actualizado!!")
|
|
|
- queryClient.invalidateQueries('puestos')
|
|
|
- },
|
|
|
- onError:() => {
|
|
|
- close();
|
|
|
- setOpen(false)
|
|
|
- toast.error("Lo sentimos ocurrió error inténtalo más tarde")
|
|
|
- }
|
|
|
+ useEffect(() => {
|
|
|
+ if(visible == null) return;
|
|
|
+ let rest = new Service(`/plaza/getthis/${visible}`)
|
|
|
+ rest
|
|
|
+ .getQuery(auth.token)
|
|
|
+ .then( response => {
|
|
|
+ let { areadeptoplz_id, fecha } = response.data;
|
|
|
+ reset({...response.data, areadepto : areadeptoplz_id, fecha : new Date(fecha) })
|
|
|
})
|
|
|
+ .catch(console.log)
|
|
|
+ },[visible])
|
|
|
|
|
|
- resetForm();
|
|
|
- },
|
|
|
- validationSchema: NewPlazaSchema,
|
|
|
- });
|
|
|
-
|
|
|
- const { errors, touched, handleSubmit, getFieldProps, setValues, values } = formik;
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- console.info('use effect edit', props)
|
|
|
- },[props])
|
|
|
-
|
|
|
- // useEffect(() => {
|
|
|
- // if (puesto) {
|
|
|
- // setValues({
|
|
|
- // id: puesto.id,
|
|
|
- // nombrepuesto: puesto.nombrepuesto,
|
|
|
- // puestosuperior: puesto.puestosuperior,
|
|
|
- // aredepto: puesto.areadeptoplz_id,
|
|
|
- // fecha: new Date(puesto.create_day),
|
|
|
- // notas: puesto.notas,
|
|
|
- // tests : puesto.tests
|
|
|
- // })
|
|
|
- // setChecklist(puesto.tests.map(( {id} ) => id))
|
|
|
- // }
|
|
|
- // }, [puesto, now, setValues])
|
|
|
+ console.log("RENDER", props)
|
|
|
|
|
|
const changeTab = (_event, newValue) => {
|
|
|
setTab(newValue);
|
|
|
@@ -157,135 +127,119 @@ function Edit(props) {
|
|
|
<Tab label="Pruebas" />
|
|
|
</Tabs>
|
|
|
|
|
|
- <FormikProvider style={{ padding: 25 }} value={formik}>
|
|
|
- <Form autoComplete="off" noValidate onSubmit={handleSubmit}>
|
|
|
-
|
|
|
-
|
|
|
- <TabPanel value={tab} index={1}>
|
|
|
- <Stack spacing={1}>
|
|
|
-<Box style={{ paddingTop :5, paddingLeft :15 }}>
|
|
|
- <Divider/>
|
|
|
- <h4 style={{paddingTop :10, paddingBottom:10}}>Seleciona los test a realizar</h4>
|
|
|
- <Divider/>
|
|
|
- <FormGroup>
|
|
|
- {
|
|
|
- testsCatalog ?
|
|
|
- testsCatalog.data.map( test => (
|
|
|
- <FormControlLabel
|
|
|
- key={test.id}
|
|
|
- control={
|
|
|
- <Checkbox
|
|
|
- checked={checklist.includes((test.id))}
|
|
|
- onChange={(event)=> addPrueba(event.target.checked,test.id)}
|
|
|
- />
|
|
|
- }
|
|
|
- label={test.nombre}
|
|
|
- />
|
|
|
- )): null
|
|
|
- }
|
|
|
- </FormGroup>
|
|
|
- </Box>
|
|
|
+ <form onSubmit={handleSubmit(onSubmit)}>
|
|
|
+ <TabPanel value={tab} index={1}>
|
|
|
+ <Stack spacing={1}>
|
|
|
+ <Box style={{ paddingTop :5, paddingLeft :15 }}>
|
|
|
+ <Divider/>
|
|
|
+ <h4 style={{paddingTop :10, paddingBottom:10}}>Seleciona los test a realizar</h4>
|
|
|
+ <Divider/>
|
|
|
+ <FormGroup>
|
|
|
+ {
|
|
|
+ testsCatalog ?
|
|
|
+ testsCatalog.data.map( test => (
|
|
|
+ <FormControlLabel
|
|
|
+ label={test.nombre}
|
|
|
+ key={test.id}
|
|
|
+ control={
|
|
|
+ <Checkbox
|
|
|
+ checked={checklist.includes((test.id))}
|
|
|
+ onChange={(event)=> addPrueba(event.target.checked,test.id)} /> }
|
|
|
+ />
|
|
|
+ )): null
|
|
|
+ }
|
|
|
+ </FormGroup>
|
|
|
+ </Box>
|
|
|
+ </Stack>
|
|
|
+ </TabPanel>
|
|
|
+
|
|
|
+ <TabPanel value={tab} index={0} >
|
|
|
+ <Stack spacing={3}>
|
|
|
+
|
|
|
+ <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
|
|
|
+
|
|
|
+ <TextField
|
|
|
+ name="nombrepuesto"
|
|
|
+ label="Nombre"
|
|
|
+ fullWidth
|
|
|
+ helperText={errors.nombrepuesto?.message}
|
|
|
+ error={Boolean(errors?.nombrepuesto)}
|
|
|
+ {...register("nombrepuesto")} />
|
|
|
+
|
|
|
+ <TextField
|
|
|
+ name="puestosuperior"
|
|
|
+ label="Puesto superior"
|
|
|
+ fullWidth
|
|
|
+ {...register("puestosuperior")} />
|
|
|
+
|
|
|
</Stack>
|
|
|
- </TabPanel>
|
|
|
-
|
|
|
-
|
|
|
- <TabPanel value={tab} index={0} >
|
|
|
- <Stack spacing={3}>
|
|
|
-
|
|
|
- <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
|
|
|
-
|
|
|
- <TextField
|
|
|
- label="Nombre"
|
|
|
- fullWidth
|
|
|
- {...getFieldProps('nombrepuesto')}
|
|
|
- error={Boolean(touched.nombrepuesto && errors.nombrepuesto)}
|
|
|
- helperText={touched.nombrepuesto && errors.nombrepuesto}
|
|
|
- />
|
|
|
-
|
|
|
- <TextField
|
|
|
- label="Puesto Superior"
|
|
|
- fullWidth
|
|
|
- {...getFieldProps('puestosuperior')}
|
|
|
- error={Boolean(touched.puestosuperior && errors.puestosuperior)}
|
|
|
- helperText={touched.puestosuperior && errors.puestosuperior}
|
|
|
- />
|
|
|
-
|
|
|
- </Stack>
|
|
|
- <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
|
|
|
- <FormControl fullWidth>
|
|
|
- <InputLabel id="demo-simple-select-label">Departamento</InputLabel>
|
|
|
- <Select
|
|
|
- labelId="demo-simple-select-label"
|
|
|
- value={departamento}
|
|
|
- label="Departamento"
|
|
|
- onChange={changeDepartamento}
|
|
|
- {...getFieldProps('aredepto')}
|
|
|
- error={Boolean(touched.aredepto && errors.aredepto)} >
|
|
|
- {
|
|
|
- data ?
|
|
|
- data.data.map(cate => {
|
|
|
- return (
|
|
|
- <MenuItem key={cate.id} value={cate.id}>{cate.nombre}</MenuItem>
|
|
|
- )
|
|
|
- })
|
|
|
- : <MenuItem>Null</MenuItem>
|
|
|
- }
|
|
|
- </Select>
|
|
|
- </FormControl>
|
|
|
-
|
|
|
-
|
|
|
- <LocalizationProvider
|
|
|
- dateAdapter={DateFnsUtils}>
|
|
|
- <DesktopDatePicker
|
|
|
- label="Fecha Creación"
|
|
|
- fullWidth
|
|
|
- inputFormat="dd/MM/yyyy"
|
|
|
- {...getFieldProps('fecha')}
|
|
|
- xd
|
|
|
- value={date}
|
|
|
- onChange={setDate}
|
|
|
- renderInput={(params) =>
|
|
|
- <TextField
|
|
|
- disabled={true}
|
|
|
- label="Fecha Creación"
|
|
|
- fullWidth
|
|
|
- {...params}
|
|
|
- />}
|
|
|
- />
|
|
|
- </LocalizationProvider>
|
|
|
-
|
|
|
- </Stack>
|
|
|
-
|
|
|
- <Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}>
|
|
|
- <TextField
|
|
|
- id="filled-multiline-static"
|
|
|
- multiline
|
|
|
- rows={4}
|
|
|
- variant="filled"
|
|
|
- label="Notas"
|
|
|
- fullWidth
|
|
|
- type="text"
|
|
|
- {...getFieldProps('notas')}
|
|
|
- error={Boolean(touched.notas && errors.notas)}
|
|
|
- helperText={touched.notas && errors.notas}
|
|
|
- />
|
|
|
- </Stack>
|
|
|
+
|
|
|
+ <Stack direction={{ xs: 'column', sm: 'row' }} spacing={2}>
|
|
|
+
|
|
|
+ <FormControl fullWidth>
|
|
|
+ <InputLabel>Departamento</InputLabel>
|
|
|
+ <Controller
|
|
|
+ name="aredepto"
|
|
|
+ control={control}
|
|
|
+ render={ ({field}) =>
|
|
|
+ <Select {...field}>
|
|
|
+ {
|
|
|
+ categories ?
|
|
|
+ categories.data.map(cate => {
|
|
|
+ return (
|
|
|
+ <MenuItem key={cate.id} value={cate.id}>{cate.nombre}</MenuItem>
|
|
|
+ )
|
|
|
+ })
|
|
|
+ : <MenuItem value={1}>hola</MenuItem>
|
|
|
+ }
|
|
|
+ </Select>
|
|
|
+ }>
|
|
|
+ </Controller>
|
|
|
+ </FormControl>
|
|
|
+
|
|
|
+ <LocalizationProvider dateAdapter={DateFnsUtils}>
|
|
|
+ <Controller
|
|
|
+ name="fecha"
|
|
|
+ control={control}
|
|
|
+ render={({field}) =>
|
|
|
+ <DesktopDatePicker
|
|
|
+ {...field}
|
|
|
+ label="Fecha Creación"
|
|
|
+ fullWidth
|
|
|
+ inputFormat="dd/MM/yyyy"
|
|
|
+ renderInput={(params) => <TextField {...params} />}
|
|
|
+ />
|
|
|
+ } >
|
|
|
+ </Controller>
|
|
|
+ </LocalizationProvider>
|
|
|
</Stack>
|
|
|
- </TabPanel>
|
|
|
|
|
|
+ <Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}>
|
|
|
+ <TextField
|
|
|
+ name="notas"
|
|
|
+ multiline
|
|
|
+ rows={4}
|
|
|
+ label="Notas"
|
|
|
+ fullWidth
|
|
|
+ type="text"
|
|
|
+ {...register("notas")}
|
|
|
+ />
|
|
|
+ </Stack>
|
|
|
+ </Stack>
|
|
|
+ </TabPanel>
|
|
|
|
|
|
- <Modal.Footer>
|
|
|
- <Button
|
|
|
- type="submit"
|
|
|
- className="registerBtn"
|
|
|
- variant="contained"
|
|
|
- sx={{ mt: 1, mr: 1 }} >
|
|
|
- {'Actualizar'}
|
|
|
- </Button>
|
|
|
- </Modal.Footer>
|
|
|
+ <Modal.Footer>
|
|
|
+ <Button
|
|
|
+ type="submit"
|
|
|
+ className="registerBtn"
|
|
|
+ variant="contained"
|
|
|
+ sx={{ mt: 1, mr: 1 }} >
|
|
|
+ {'Actualizar'}
|
|
|
+ </Button>
|
|
|
+ </Modal.Footer>
|
|
|
+
|
|
|
+ </form>
|
|
|
|
|
|
- </Form>
|
|
|
- </FormikProvider>
|
|
|
</Modal.Body>
|
|
|
<Backdrop
|
|
|
sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
|
|
@@ -299,5 +253,4 @@ function Edit(props) {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
export default memo(Edit);
|