Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2782 stevensc 1
import React, { useEffect } from 'react'
2781 stevensc 2
import { connect } from 'react-redux'
3
import { useForm } from 'react-hook-form'
4
 
5
import { useFetch } from '@hooks'
6
import { axios } from '@app/utils'
7
import { addNotification } from '@app/redux/notification/notification.actions'
8
 
9
import Widget from '@app/components/UI/Widget'
10
import Spinner from '@app/components/UI/Spinner'
11
import FormErrorFeedback from '@app/components/UI/form/FormErrorFeedback'
12
import SwitchInput from '@app/components/UI/SwitchInput'
13
 
14
const BasicSettings = ({
15
  addNotification = function () {} // Redux action
16
}) => {
2782 stevensc 17
  const { data: timezones } = useFetch('/helpers/timezones')
18
  const { data, isLoading } = useFetch('/account-settings/basic')
2781 stevensc 19
 
20
  const { register, handleSubmit, setValue, watch, errors, getValues } =
21
    useForm()
22
 
23
  const onSubmit = async (data) => {
24
    const formData = new FormData()
25
    Object.entries(data).map(([key, value]) => formData.append(key, value))
26
 
27
    axios
28
      .post('/account-settings/basic', formData)
29
      .then(({ data: responseData }) => {
30
        const { data, success } = responseData
31
 
32
        if (!success) {
33
          const errorMessage =
34
            typeof data === 'string'
35
              ? data
36
              : Object.entries(data)
37
                  .map(([key, value]) => `${key}: ${value}`)
38
                  .join(', ')
39
 
40
          throw new Error(errorMessage)
41
        }
42
 
43
        addNotification({ style: 'success', msg: data })
44
      })
45
      .catch((err) => {
46
        addNotification({ style: 'danger', msg: err.message })
47
      })
48
  }
49
 
2782 stevensc 50
  useEffect(() => {
51
    register('timezone', { required: 'Por favor ingrese su zona horaria' })
52
  }, [])
2781 stevensc 53
 
2782 stevensc 54
  useEffect(() => {
55
    Object.entries(data).forEach(([key, value]) => {
56
      const currentValue = getValues(key)
2781 stevensc 57
 
2782 stevensc 58
      if (!currentValue) register(key, { required: true })
59
      setValue(key, key === 'is_adult' ? value === 'y' : value)
60
    })
61
  }, [data])
2781 stevensc 62
 
2782 stevensc 63
  if (isLoading) {
64
    return <Spinner />
2781 stevensc 65
  }
66
 
67
  return (
68
    <Widget>
69
      <Widget.Header title='Información básica' />
70
 
71
      <Widget.Body>
2782 stevensc 72
        <form onSubmit={handleSubmit(onSubmit)}>
73
          <div className='inputs__container'>
74
            <div className='cp-field'>
75
              <label htmlFor='first_name'>Nombre</label>
76
              <input
77
                type='text'
78
                name='first_name'
79
                ref={register}
80
                defaultValue={watch('first_name')}
81
              />
82
              {errors.first_name && (
83
                <FormErrorFeedback>
84
                  Por favor ingrese su nombre
85
                </FormErrorFeedback>
86
              )}
2781 stevensc 87
            </div>
2782 stevensc 88
            <div className='cp-field'>
89
              <label htmlFor='last_name'>Apellido</label>
90
              <input
91
                type='text'
92
                name='last_name'
93
                ref={register}
94
                defaultValue={watch('last_name')}
95
              />
96
              {errors.last_name && (
97
                <FormErrorFeedback>
98
                  Por favor ingrese su apellido
99
                </FormErrorFeedback>
100
              )}
101
            </div>
102
          </div>
2781 stevensc 103
 
2782 stevensc 104
          <div className='inputs__container'>
105
            <div className='cp-field'>
106
              <label htmlFor='email'>Email</label>
107
              <input
108
                type='text'
109
                name='email'
110
                ref={register}
111
                defaultValue={watch('email')}
112
              />
113
              {errors.email && (
114
                <FormErrorFeedback>
115
                  Por favor ingrese un correo electrónico valido
116
                </FormErrorFeedback>
117
              )}
2781 stevensc 118
            </div>
2782 stevensc 119
            <div className='cp-field'>
120
              <label htmlFor='phone'>Teléfono</label>
121
              <input
122
                name='phone'
123
                ref={register({
124
                  required: 'Por favor ingrese su número de teléfono',
125
                  pattern: {
126
                    message: 'Por favor ingrese un número de teléfono válido',
127
                    value:
128
                      /^\+[1-9]{1}[0-9]{0,2}[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/
129
                  }
130
                })}
131
                defaultValue={watch('phone')}
132
              />
133
              {errors.phone && (
134
                <FormErrorFeedback>{errors.phone.message}</FormErrorFeedback>
135
              )}
136
            </div>
137
          </div>
2781 stevensc 138
 
2782 stevensc 139
          <div className='inputs__container'>
140
            <div className='cp-field'>
141
              <label htmlFor='gender'>Género</label>
142
              <select
143
                defaultValue={watch('gender')}
144
                name='gender'
145
                ref={register}
146
              >
147
                <option value='' hidden>
148
                  Seleccione un género
149
                </option>
150
                <option value='m'>Masculino</option>
151
                <option value='f'>Femenino</option>
152
              </select>
153
              {errors.gender && (
154
                <FormErrorFeedback>
155
                  Por favor selecciona un genero
156
                </FormErrorFeedback>
157
              )}
2781 stevensc 158
            </div>
159
 
160
            <div className='cp-field'>
2782 stevensc 161
              <label htmlFor='timezone'>Zona horaria</label>
162
              <select
163
                name='timezone'
164
                ref={register}
165
                defaultValue={watch('timezone')}
166
              >
167
                <option value='' hidden>
168
                  Zona horaria
169
                </option>
170
                {Object.entries(timezones).map(([key, value]) => (
171
                  <option value={key} key={key}>
172
                    {value}
173
                  </option>
174
                ))}
175
              </select>
176
              {errors.timezone && (
177
                <FormErrorFeedback>
178
                  Por favor selecciona una zona horaria
179
                </FormErrorFeedback>
180
              )}
2781 stevensc 181
            </div>
2782 stevensc 182
          </div>
2781 stevensc 183
 
2782 stevensc 184
          <div className='cp-field'>
185
            <label htmlFor='timezone'>Eres mayor de 18</label>
186
            <SwitchInput
187
              isChecked={watch('is_adult')}
188
              setValue={(value) => setValue('is_adult', value)}
189
            />
190
          </div>
191
 
192
          <button type='submit' className='btn btn-primary mt-3'>
193
            Guardar
194
          </button>
195
        </form>
2781 stevensc 196
      </Widget.Body>
197
    </Widget>
198
  )
199
}
200
 
201
const mapDispatchToProps = {
202
  addNotification: (notification) => addNotification(notification)
203
}
204
 
205
export default connect(null, mapDispatchToProps)(BasicSettings)