Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3269 | Rev 3303 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3230 stevensc 1
import React, { useState } from 'react'
3239 stevensc 2
import { useForm } from 'react-hook-form'
3
import { Button, styled, Tab, Tabs } from '@mui/material'
3230 stevensc 4
 
3239 stevensc 5
import { INTELLIGENCES, WEEK_DAYS } from '@constants/habits'
3230 stevensc 6
 
3239 stevensc 7
import Row from '@components/common/Row'
3230 stevensc 8
import Form from '@components/common/form'
3239 stevensc 9
import Input from '@components/UI/inputs/Input'
3230 stevensc 10
import Widget from '@components/UI/Widget'
11
import TabPanel from '@components/common/tab-panel'
3269 stevensc 12
import Ckeditor from '@components/common/ckeditor/Ckeditor'
3230 stevensc 13
import Select from '@components/UI/inputs/Select'
14
import CheckboxInput from '@components/UI/inputs/Checkbox'
3239 stevensc 15
import LoadingWrapper from '@components/common/loading-wrapper'
3230 stevensc 16
 
3239 stevensc 17
const FormTabPanel = styled(TabPanel)(({ theme }) => ({
18
  display: 'flex',
19
  flexDirection: 'column',
20
  width: '100%',
21
  gap: theme.spacing(0.5)
22
}))
23
 
3230 stevensc 24
export default function HabitForm({
25
  onSubmit = () => {},
26
  defaultValues = {
27
    name: '',
28
    description: '',
3239 stevensc 29
    monday_active: false,
30
    tuesday_active: false,
31
    wednesday_active: false,
32
    thursday_active: false,
33
    friday_active: false,
34
    saturday_active: false,
35
    sunday_active: false,
3292 stevensc 36
    monday_time: undefined,
37
    tuesday_time: undefined,
38
    wednesday_time: undefined,
39
    thursday_time: undefined,
40
    friday_time: undefined,
41
    saturday_time: undefined,
42
    sunday_time: undefined,
3239 stevensc 43
    quantitative_value: false,
44
    qualitative_description: '',
45
    notification_10min_before: false,
46
    notification_30min_before: false,
47
    intelligence: ''
3230 stevensc 48
  },
49
  values = {}
50
}) {
51
  const [currentTab, setCurrentTab] = useState(0)
52
 
53
  const {
3239 stevensc 54
    handleSubmit,
3230 stevensc 55
    control,
56
    formState: { errors, isSubmitting },
3239 stevensc 57
    trigger
3230 stevensc 58
  } = useForm({
59
    defaultValues,
60
    values
61
  })
3239 stevensc 62
  const handleChange = async (event, newValue) => {
63
    const valid = await trigger()
64
    if (valid) setCurrentTab(newValue)
65
  }
3230 stevensc 66
 
3239 stevensc 67
  const nextStep = async () => {
68
    const valid = await trigger()
69
    if (valid) setCurrentTab(currentTab + 1)
70
  }
71
 
72
  const dataAdapter = ({
73
    monday_active,
74
    tuesday_active,
75
    wednesday_active,
76
    thursday_active,
77
    friday_active,
78
    saturday_active,
79
    sunday_active,
80
    notification_10min_before,
81
    notification_30min_before,
82
    ...habit
83
  }) => {
84
    onSubmit({
3242 stevensc 85
      monday_active: monday_active ? 1 : 0,
86
      tuesday_active: tuesday_active ? 1 : 0,
87
      wednesday_active: wednesday_active ? 1 : 0,
88
      thursday_active: thursday_active ? 1 : 0,
89
      friday_active: friday_active ? 1 : 0,
90
      saturday_active: saturday_active ? 1 : 0,
91
      sunday_active: sunday_active ? 1 : 0,
92
      notification_10min_before: notification_10min_before ? 1 : 0,
93
      notification_30min_before: notification_30min_before ? 1 : 0,
3239 stevensc 94
      ...habit
95
    })
96
  }
97
 
3230 stevensc 98
  return (
3239 stevensc 99
    <Widget>
3230 stevensc 100
      <Tabs value={currentTab} onChange={handleChange}>
3239 stevensc 101
        <Tab label='Detalles' />
102
        <Tab label='Frecuencia' />
103
        <Tab label='Valor' />
104
        <Tab label='Notificaciones' />
3230 stevensc 105
      </Tabs>
106
      <Widget.Body>
3239 stevensc 107
        <Form onSubmit={handleSubmit(dataAdapter)}>
3230 stevensc 108
          <LoadingWrapper loading={isSubmitting} displayChildren>
3239 stevensc 109
            {/* Detalles */}
110
            <FormTabPanel value={0} index={currentTab}>
111
              <Input
3262 stevensc 112
                label='Nombre del hábito o competencia:'
3239 stevensc 113
                name='name'
3262 stevensc 114
                placeholder='Escribe el nombre del hábito o competencia'
3239 stevensc 115
                control={control}
116
                rules={{ required: 'El nombre es requerido' }}
117
                error={errors.name?.message}
118
              />
119
 
3230 stevensc 120
              <Select
3262 stevensc 121
                label='Inteligencias:'
3230 stevensc 122
                name='intelligence'
123
                options={INTELLIGENCES}
124
                control={control}
125
                rules={{ required: 'Este campo es requerido' }}
126
                error={errors.intelligence?.message}
127
              />
128
 
3239 stevensc 129
              <Ckeditor
3230 stevensc 130
                control={control}
131
                name='description'
132
                error={errors.description?.message}
3239 stevensc 133
                rules={{ required: 'La descripción es requerida' }}
134
                label='Descripción:'
3230 stevensc 135
              />
136
 
3239 stevensc 137
              <Button color='primary' onClick={nextStep}>
138
                Continuar
139
              </Button>
140
            </FormTabPanel>
141
 
142
            {/* Frecuencia */}
143
            <FormTabPanel value={1} index={currentTab}>
144
              {WEEK_DAYS.map(({ label, value, time }) => {
145
                return (
146
                  <Row
147
                    key={value}
148
                    styles={{
149
                      flexFlow: 'nowrap',
150
                      justifyContent: 'space-between'
151
                    }}
152
                  >
153
                    {/* Campos de días activos */}
154
                    <CheckboxInput
155
                      label={label}
156
                      control={control}
157
                      name={value}
158
                    />
159
                    {/* Puedes repetir los campos de días activos y sus horas para cada día de la semana */}
160
                    <Input
161
                      type='time'
162
                      control={control}
163
                      name={time}
164
                      rules={{
165
                        validate: (time, formValues) => {
166
                          if (!formValues[value]) return true
167
                          if (formValues[value] && time) return true
168
                          return 'La hora es requerida si el día está activo'
169
                        }
170
                      }}
171
                      style={{ width: 'auto' }}
172
                      error={errors[time]?.message}
173
                    />
174
                  </Row>
175
                )
176
              })}
177
 
178
              <Button color='primary' onClick={nextStep}>
179
                Continuar
180
              </Button>
181
            </FormTabPanel>
182
 
183
            {/* Valor */}
184
            <FormTabPanel value={2} index={currentTab}>
3230 stevensc 185
              <Input
3239 stevensc 186
                control={control}
187
                label='Valor cuantitativo:'
188
                name='quantitative_value'
3230 stevensc 189
                type='number'
3239 stevensc 190
                rules={{ required: 'El valor es requerido' }}
191
                error={errors.quantitative_value?.message}
3230 stevensc 192
              />
193
 
3239 stevensc 194
              <Ckeditor
3230 stevensc 195
                control={control}
3239 stevensc 196
                name='qualitative_description'
197
                error={errors.qualitative_description?.message}
198
                rules={{ required: 'La descripción es requerida' }}
199
                label='Descripción cualitativa:'
3230 stevensc 200
              />
201
 
3239 stevensc 202
              <Button color='primary' onClick={nextStep}>
203
                Continuar
204
              </Button>
205
            </FormTabPanel>
3230 stevensc 206
 
3239 stevensc 207
            {/* Notificaciones */}
208
            <FormTabPanel value={3} index={currentTab}>
209
              <CheckboxInput
210
                label='Notificación 10 min antes'
211
                name='notification_10min_before'
3230 stevensc 212
                control={control}
3239 stevensc 213
                rules={{
214
                  validate: (value, formValues) => {
215
                    if (!value && !formValues.notification_30min_before) {
216
                      return 'Debe seleccionar al menos una opción'
217
                    }
218
                    if (value && formValues.notification_30min_before) {
219
                      return 'No se puede seleccionar ambas opciones simultáneamente'
220
                    }
221
                    return true
222
                  }
223
                }}
3230 stevensc 224
              />
3239 stevensc 225
              <CheckboxInput
226
                label='Notificación 30 min antes'
227
                name='notification_30min_before'
228
                control={control}
229
                rules={{
230
                  validate: (value, formValues) => {
231
                    if (!value && !formValues.notification_10min_before) {
232
                      return 'Debe seleccionar al menos una opción'
233
                    }
234
                    if (value && formValues.notification_10min_before) {
235
                      return 'No se puede seleccionar ambas opciones simultáneamente'
236
                    }
237
                    return true
238
                  }
239
                }}
240
              />
241
              <Button color='primary' type='submit'>
242
                Enviar
243
              </Button>
244
            </FormTabPanel>
3230 stevensc 245
          </LoadingWrapper>
246
        </Form>
247
      </Widget.Body>
248
    </Widget>
249
  )
250
}