Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3231 | Ir a la última revisión | | 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}
152
                    render={({
153
                      field: { ref, name, onChange, value, disabled }
154
                    }) => (
155
                      <Datetime
156
                        dateFormat={false}
157
                        timeFormat={true}
158
                        onChange={(e) => {
159
                          console.log(e.toDate())
160
                          onChange(e.toDate())
161
                        }}
162
                        inputProps={{
163
                          className: 'form-control custom-picker',
164
                          disabled,
165
                          ref,
166
                          name,
167
                          value
168
                        }}
169
                        initialValue={Date.now()}
170
                        closeOnSelect
171
                      />
172
                    )}
173
                  />
174
                </Box>
175
              ))}
176
 
177
              <InputLabel>Fecha limite</InputLabel>
178
              <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
179
                <SwitchInput
180
                  label='Indeterminado'
181
                  setValue={(val) => setValue('indeterminate', val)}
182
                  isChecked={watchedInderminate}
183
                />
184
                {!watchedInderminate && (
185
                  <>
186
                    <Datetime
187
                      dateFormat='DD-MM-YYYY'
188
                      timeFormat={false}
189
                      inputProps={{ className: 'form-control custom-picker' }}
190
                      initialValue={Date.parse(new Date())}
191
                      closeOnSelect
192
                    />
193
                  </>
194
                )}
195
              </Box>
196
 
197
              <Button color='primary'>Continuar</Button>
198
            </TabPanel>
199
          </LoadingWrapper>
200
        </Form>
201
      </Widget.Body>
202
    </Widget>
203
  )
204
}