Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3230 | Rev 3239 | 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'
2
import { Controller, useForm } from 'react-hook-form'
3
import { Box, Button, InputLabel, Tab, Tabs } from '@mui/material'
4
import Datetime from 'react-datetime'
5
 
6
import {
7
  CREATE_OPTIONS,
8
  FREQUENCYS,
9
  INTELLIGENCES,
10
  WEEK_DAYS
11
} from '@constants/habits'
12
 
13
import Form from '@components/common/form'
14
import Widget from '@components/UI/Widget'
15
import TabPanel from '@components/common/tab-panel'
16
import LoadingWrapper from '@components/common/loading-wrapper'
17
import Select from '@components/UI/inputs/Select'
18
import Input from '@components/UI/inputs/Input'
19
import Ckeditor from '@components/UI/Ckeditor'
20
import CheckboxInput from '@components/UI/inputs/Checkbox'
21
import SwitchInput from '@components/UI/SwitchInput'
22
 
23
export default function HabitForm({
24
  onSubmit = () => {},
25
  defaultValues = {
26
    name: '',
27
    description: '',
28
    monday_active: '0',
29
    tuesday_active: '0',
30
    wednesday_active: '0',
31
    thursday_active: '0',
32
    friday_active: '0',
33
    saturday_active: '0',
34
    sunday_active: '0',
35
    monday_time: '06:00',
36
    tuesday_time: '07:00',
37
    wednesday_time: '08:00',
38
    thursday_time: '09:00',
39
    friday_time: '10:00',
40
    saturday_time: '11:00',
41
    sunday_time: '12:00',
42
    quantitative_value: '0',
43
    qualitative_description: 'hora',
44
    notification_10min_before: '0',
45
    notification_30min_before: '0',
46
    intelligence: 'physical'
47
  },
48
  values = {}
49
}) {
50
  const [currentTab, setCurrentTab] = useState(0)
51
 
52
  const handleChange = (event, newValue) => setCurrentTab(newValue)
53
 
54
  const {
55
    control,
56
    formState: { errors, isSubmitting },
57
    watch,
58
    setValue,
59
    handleSubmit
60
  } = useForm({
61
    defaultValues,
62
    values
63
  })
64
  const watchedInderminate = watch('indeterminate')
65
 
66
  return (
67
    <Widget styles={{ overflow: 'visible' }}>
68
      <Tabs value={currentTab} onChange={handleChange}>
69
        {CREATE_OPTIONS.map(({ label, value }) => (
70
          <Tab
71
            key={value}
72
            label={label}
73
            id={`simple-tab-${value}`}
74
            aria-controls={`simple-tabpanel-${value}`}
75
            value={value}
76
          />
77
        ))}
78
      </Tabs>
79
      <Widget.Body>
80
        <Form onSubmit={handleSubmit(onSubmit)}>
81
          <LoadingWrapper loading={isSubmitting} displayChildren>
82
            <TabPanel value={0} index={currentTab}>
83
              <Select
84
                name='intelligence'
85
                label='Inteligencia'
86
                options={INTELLIGENCES}
87
                control={control}
88
                rules={{ required: 'Este campo es requerido' }}
89
                error={errors.intelligence?.message}
90
              />
91
 
92
              <Input
93
                name='title'
94
                label='Titulo'
95
                placeholder='Define el título de tu hábito'
96
                control={control}
97
                rules={{ required: 'Este campo es requerido' }}
98
                error={errors.title?.message}
99
              />
100
 
101
              <Ckeditor
102
                name='description'
103
                label='Descripción'
104
                control={control}
105
                rules={{ required: 'Este campo es requerido' }}
106
                error={errors.description?.message}
107
              />
108
 
109
              <Input
110
                type='number'
111
                name='value'
112
                label='Valor'
113
                placeholder='Define un valor a hábito'
114
                control={control}
115
                rules={{ required: 'Este campo es requerido' }}
116
                error={errors.value?.message}
117
              />
118
 
119
              <Input
120
                label='Metodo'
121
                control={control}
122
                name='method'
123
                type='file'
124
              />
125
 
126
              <Button color='primary'>Continuar</Button>
127
            </TabPanel>
128
 
129
            <TabPanel value={1} index={currentTab}>
130
              <Select
131
                name='frequency'
132
                label='Frecuencia'
133
                options={FREQUENCYS}
134
                control={control}
135
                rules={{ required: 'Este campo es requerido' }}
136
                error={errors.frequency?.message}
137
              />
138
 
139
              {WEEK_DAYS.map(({ label, value, time }) => (
140
                <Box
141
                  key={value}
142
                  sx={{
143
                    display: 'flex',
144
                    justifyContent: 'space-between',
145
                    gap: '1rem'
146
                  }}
147
                >
148
                  <CheckboxInput label={label} control={control} name={value} />
149
                  <Controller
150
                    name={time}
151
                    control={control}
3231 stevensc 152
                    rules={{
153
                      validate: (val, formValues) => {
154
                        return (
155
                          (formValues[value] && val) ||
156
                          'Debes seleccionar una hora'
157
                        )
158
                      }
159
                    }}
160
                    render={({ field: { onChange, disabled } }) => (
3230 stevensc 161
                      <Datetime
162
                        dateFormat={false}
163
                        timeFormat={true}
164
                        onChange={(e) => {
3231 stevensc 165
                          const hour = new Date(e.toDate()).getHours()
166
                          const minute = new Date(e.toDate()).getMinutes()
167
                          const time = `${hour}:${minute}`
168
                          onChange(time)
3230 stevensc 169
                        }}
170
                        inputProps={{
171
                          className: 'form-control custom-picker',
3231 stevensc 172
                          disabled
3230 stevensc 173
                        }}
174
                        closeOnSelect
175
                      />
176
                    )}
177
                  />
178
                </Box>
179
              ))}
180
 
181
              <InputLabel>Fecha limite</InputLabel>
182
              <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
183
                <SwitchInput
184
                  label='Indeterminado'
185
                  setValue={(val) => setValue('indeterminate', val)}
186
                  isChecked={watchedInderminate}
187
                />
188
                {!watchedInderminate && (
189
                  <>
190
                    <Datetime
191
                      dateFormat='DD-MM-YYYY'
192
                      timeFormat={false}
193
                      inputProps={{ className: 'form-control custom-picker' }}
194
                      initialValue={Date.parse(new Date())}
195
                      closeOnSelect
196
                    />
197
                  </>
198
                )}
199
              </Box>
200
 
201
              <Button color='primary'>Continuar</Button>
202
            </TabPanel>
203
          </LoadingWrapper>
204
        </Form>
205
      </Widget.Body>
206
    </Widget>
207
  )
208
}