Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2786 stevensc 1
import React from 'react'
2
import { useDispatch } from 'react-redux'
2781 stevensc 3
import { useForm } from 'react-hook-form'
2784 stevensc 4
import { Button, styled } from '@mui/material'
2781 stevensc 5
 
6
import { useFetch } from '@hooks'
7
import { axios } from '@app/utils'
8
import { addNotification } from '@app/redux/notification/notification.actions'
9
 
2786 stevensc 10
import Widget from '@components/UI/Widget'
2784 stevensc 11
import Input from '@components/UI/inputs/Input'
12
import Select from '@components/UI/inputs/Select'
2786 stevensc 13
import Spinner from '@components/UI/Spinner'
14
import SwitchInput from '@components/UI/SwitchInput'
2781 stevensc 15
 
2784 stevensc 16
const Row = styled('div')(({ theme }) => ({
17
  display: 'flex',
18
  gap: theme.spacing(0.5)
19
}))
20
 
2786 stevensc 21
const BasicSettings = () => {
2782 stevensc 22
  const { data: timezones } = useFetch('/helpers/timezones')
2787 stevensc 23
  const { data: formValues, isLoading } = useFetch(
24
    '/account-settings/basic',
25
    undefined
26
  )
2786 stevensc 27
  const dispatch = useDispatch()
2781 stevensc 28
 
2786 stevensc 29
  const { control, handleSubmit, setValue, watch, errors } = useForm({
2784 stevensc 30
    defaultValues: {
31
      first_name: '',
32
      last_name: '',
33
      email: '',
34
      phone: '',
35
      gender: 'm',
36
      timezones: '',
37
      is_adult: 'n'
2788 stevensc 38
    }
2784 stevensc 39
  })
40
  const watchedIsAdult = watch('is_adult') === 'y'
2781 stevensc 41
 
2785 stevensc 42
  const onSubmit = handleSubmit(async (data) => {
2781 stevensc 43
    const formData = new FormData()
44
    Object.entries(data).map(([key, value]) => formData.append(key, value))
45
 
46
    axios
47
      .post('/account-settings/basic', formData)
2786 stevensc 48
      .then((response) => {
49
        dispatch(addNotification({ style: 'success', msg: response.data }))
2781 stevensc 50
      })
51
      .catch((err) => {
2786 stevensc 52
        dispatch(addNotification({ style: 'danger', msg: err.message }))
2781 stevensc 53
      })
2785 stevensc 54
  })
2781 stevensc 55
 
2782 stevensc 56
  if (isLoading) {
57
    return <Spinner />
2781 stevensc 58
  }
59
 
60
  return (
61
    <Widget>
62
      <Widget.Header title='Información básica' />
63
 
64
      <Widget.Body>
2785 stevensc 65
        <form onSubmit={onSubmit}>
2784 stevensc 66
          <Row>
67
            <Input
68
              label='Nombre'
69
              name='first_name'
70
              control={control}
71
              error={errors.first_name?.message}
72
              rules={{ required: 'Este campo es requerido' }}
73
            />
74
            <Input
75
              label='Apellido'
76
              name='last_name'
77
              control={control}
78
              error={errors.last_name?.message}
79
              rules={{ required: 'Este campo es requerido' }}
80
            />
81
          </Row>
2781 stevensc 82
 
2784 stevensc 83
          <Row>
84
            <Input
85
              label='Correo'
86
              name='email'
87
              control={control}
88
              error={errors.email?.message}
89
              rules={{ required: 'Este campo es requerido' }}
90
            />
91
            <Input
92
              label='Teléfono'
93
              name='phone'
94
              control={control}
95
              error={errors.phone?.message}
96
              rules={{
97
                required: 'Por favor ingrese su número de teléfono',
98
                pattern: {
99
                  message: 'Por favor ingrese un número de teléfono válido',
100
                  value:
101
                    /^\+[1-9]{1}[0-9]{0,2}[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/
102
                }
103
              }}
104
            />
105
          </Row>
2781 stevensc 106
 
2784 stevensc 107
          <Row>
108
            <Select
109
              label='Género'
110
              name='gender'
111
              control={control}
2786 stevensc 112
              rules={{ required: 'Este campo es requerido' }}
2784 stevensc 113
              error={errors.gender?.message}
114
              options={[
115
                { name: 'Masculino', value: 'm' },
116
                { name: 'Femenino', value: 'f' }
117
              ]}
118
            />
119
            <Select
120
              label='Zona horaria'
121
              name='timezones'
122
              control={control}
2786 stevensc 123
              rules={{ required: 'Este campo es requerido' }}
2784 stevensc 124
              error={errors.timezones?.message}
125
              options={Object.entries(timezones).map(([key, value]) => ({
126
                name: key,
127
                value
128
              }))}
129
            />
130
          </Row>
2781 stevensc 131
 
2784 stevensc 132
          <SwitchInput
133
            label='Eres mayor de 18'
134
            isChecked={watchedIsAdult}
135
            setValue={(value) => setValue('is_adult', value ? 'y' : 'n')}
136
          />
2781 stevensc 137
 
2786 stevensc 138
          <Button variant='primary' type='submit' sx={{ marginTop: 1 }}>
2782 stevensc 139
            Guardar
2784 stevensc 140
          </Button>
2782 stevensc 141
        </form>
2781 stevensc 142
      </Widget.Body>
143
    </Widget>
144
  )
145
}
146
 
2786 stevensc 147
export default BasicSettings