| 3719 |
stevensc |
1 |
import React, { useState } from 'react';
|
|
|
2 |
import { Controller, useForm } from 'react-hook-form';
|
|
|
3 |
import { Button, styled, Tab, Tabs } from '@mui/material';
|
|
|
4 |
|
|
|
5 |
import Widget from '@components/UI/Widget';
|
|
|
6 |
import Form from '@components/common/form';
|
|
|
7 |
import Input from '@components/UI/inputs/Input';
|
|
|
8 |
import TabPanel from '@components/common/tab-panel';
|
|
|
9 |
import Ckeditor from '@components/common/ckeditor/Ckeditor';
|
|
|
10 |
import DatetimePicker from '@components/common/inputs/datetime-picker';
|
|
|
11 |
import LoadingWrapper from '@components/common/loading-wrapper';
|
|
|
12 |
import TagsInput from '@components/UI/TagsInput';
|
|
|
13 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
14 |
|
|
|
15 |
const FormTabPanel = styled(TabPanel)(({ theme }) => ({
|
|
|
16 |
display: 'flex',
|
|
|
17 |
flexDirection: 'column',
|
|
|
18 |
width: '100%',
|
|
|
19 |
gap: theme.spacing(0.5)
|
|
|
20 |
}));
|
|
|
21 |
|
|
|
22 |
export default function GoalForm({
|
|
|
23 |
onSubmit = () => {},
|
|
|
24 |
habits = [],
|
|
|
25 |
defaultValues = {},
|
|
|
26 |
values = {}
|
|
|
27 |
}) {
|
|
|
28 |
const [currentTab, setCurrentTab] = useState(0);
|
|
|
29 |
|
|
|
30 |
const {
|
|
|
31 |
control,
|
|
|
32 |
handleSubmit,
|
|
|
33 |
formState: { errors, isSubmitting },
|
|
|
34 |
trigger
|
|
|
35 |
} = useForm({
|
|
|
36 |
defaultValues,
|
|
|
37 |
values
|
|
|
38 |
});
|
|
|
39 |
|
|
|
40 |
const handleChange = async (event, newValue) => {
|
|
|
41 |
const valid = await trigger();
|
|
|
42 |
if (valid) setCurrentTab(newValue);
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
const nextStep = async () => {
|
|
|
46 |
const valid = await trigger();
|
|
|
47 |
if (valid) setCurrentTab(currentTab + 1);
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
return (
|
|
|
51 |
<Widget styles={{ overflow: 'visible' }}>
|
|
|
52 |
<Tabs value={currentTab} onChange={handleChange}>
|
|
|
53 |
<Tab label='Detalles' />
|
|
|
54 |
<Tab label='Valor' />
|
|
|
55 |
<Tab label='Fecha' />
|
|
|
56 |
</Tabs>
|
|
|
57 |
|
|
|
58 |
<Widget.Body>
|
|
|
59 |
<Form onSubmit={handleSubmit(onSubmit)}>
|
|
|
60 |
<LoadingWrapper loading={isSubmitting} displayChildren>
|
|
|
61 |
<FormTabPanel value={0} index={currentTab}>
|
|
|
62 |
<Input
|
|
|
63 |
name='name'
|
|
|
64 |
label='Nombre'
|
|
|
65 |
placeholder='Escribe el nombre de la meta'
|
|
|
66 |
control={control}
|
|
|
67 |
rules={{ required: 'El nombre es requerido' }}
|
|
|
68 |
error={errors.name?.message}
|
|
|
69 |
/>
|
|
|
70 |
|
|
|
71 |
<Ckeditor
|
|
|
72 |
label='Descripción'
|
|
|
73 |
name='description'
|
|
|
74 |
control={control}
|
|
|
75 |
rules={{ required: 'La descripción es requerida' }}
|
|
|
76 |
error={errors.description?.message}
|
|
|
77 |
/>
|
|
|
78 |
|
|
|
79 |
<Button color='primary' onClick={nextStep}>
|
|
|
80 |
Continuar
|
|
|
81 |
</Button>
|
|
|
82 |
</FormTabPanel>
|
|
|
83 |
|
|
|
84 |
<FormTabPanel value={1} index={currentTab}>
|
|
|
85 |
<Input
|
|
|
86 |
type='number'
|
|
|
87 |
name='value'
|
|
|
88 |
label='Valor cuantitativo de la Meta a lograr'
|
|
|
89 |
placeholder='Escribe el valor'
|
|
|
90 |
control={control}
|
|
|
91 |
rules={{
|
|
|
92 |
required: { value: true, message: 'El valor es requerido' },
|
|
|
93 |
validate: {
|
|
|
94 |
positive: (value) => Number(value) > 0 || 'El valor mayor a cero'
|
|
|
95 |
}
|
|
|
96 |
}}
|
|
|
97 |
error={errors.value?.message}
|
|
|
98 |
/>
|
|
|
99 |
|
|
|
100 |
<Controller
|
|
|
101 |
name='skill_id'
|
|
|
102 |
control={control}
|
|
|
103 |
defaultValue={[]}
|
|
|
104 |
rules={{ required: 'Por favor selecciona un hábito' }}
|
|
|
105 |
render={({ field: { onChange, value, name }, fieldState: { error } }) => (
|
|
|
106 |
<>
|
|
|
107 |
<TagsInput
|
|
|
108 |
label='Hábitos y competencias para lograr la Meta'
|
|
|
109 |
suggestions={habits}
|
|
|
110 |
onChange={onChange}
|
|
|
111 |
defaultValue={value}
|
|
|
112 |
name={name}
|
|
|
113 |
/>
|
|
|
114 |
{error && <FormErrorFeedback>{error.message}</FormErrorFeedback>}
|
|
|
115 |
</>
|
|
|
116 |
)}
|
|
|
117 |
/>
|
|
|
118 |
|
|
|
119 |
<Button color='primary' onClick={nextStep}>
|
|
|
120 |
Continuar
|
|
|
121 |
</Button>
|
|
|
122 |
</FormTabPanel>
|
|
|
123 |
|
|
|
124 |
<FormTabPanel value={2} index={currentTab}>
|
|
|
125 |
<DatetimePicker
|
|
|
126 |
label='Fecha de inicio'
|
|
|
127 |
name='start_date'
|
|
|
128 |
control={control}
|
|
|
129 |
rules={{
|
|
|
130 |
required: { value: true, message: 'La fecha es requerida' },
|
|
|
131 |
validate: {
|
|
|
132 |
beEqualOrGreaterThanToday: (date) => {
|
|
|
133 |
const selectedDate = new Date(date + 'T00:00:00');
|
|
|
134 |
const selectedDateMs = selectedDate.getTime();
|
|
|
135 |
const todayMs = new Date().setHours(0, 0, 0, 0);
|
|
|
136 |
|
|
|
137 |
return selectedDateMs >= todayMs || 'La fecha debe ser mayor o igual a hoy';
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}}
|
|
|
141 |
/>
|
|
|
142 |
|
|
|
143 |
<DatetimePicker
|
|
|
144 |
label='Fecha de finalización'
|
|
|
145 |
name='end_date'
|
|
|
146 |
control={control}
|
|
|
147 |
rules={{
|
|
|
148 |
required: { value: true, message: 'La fecha es requerida' },
|
|
|
149 |
validate: (value, { start_date }) => {
|
|
|
150 |
const startDate = new Date(start_date + 'T00:00:00');
|
|
|
151 |
const endDate = new Date(value + 'T00:00:00');
|
|
|
152 |
const startDateMs = startDate.getTime();
|
|
|
153 |
const endDateMs = endDate.getTime();
|
|
|
154 |
|
|
|
155 |
return (
|
|
|
156 |
endDateMs > startDateMs || 'La fecha debe ser mayor a la fecha de inicio'
|
|
|
157 |
);
|
|
|
158 |
}
|
|
|
159 |
}}
|
|
|
160 |
/>
|
|
|
161 |
|
|
|
162 |
<Button type='submit' color='primary'>
|
|
|
163 |
Enviar
|
|
|
164 |
</Button>
|
|
|
165 |
</FormTabPanel>
|
|
|
166 |
</LoadingWrapper>
|
|
|
167 |
</Form>
|
|
|
168 |
</Widget.Body>
|
|
|
169 |
</Widget>
|
|
|
170 |
);
|
|
|
171 |
}
|