Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
4115 stevensc 1
/* eslint-disable react/prop-types */
2
/* eslint-disable no-mixed-spaces-and-tabs */
1 www 3
import React, { useEffect, useState } from "react";
4
import { useForm } from "react-hook-form";
2641 stevensc 5
import { axios } from '../../../utils';
1 www 6
import { connect } from "react-redux";
7
import { addNotification } from "../../../redux/notification/notification.actions";
4405 stevensc 8
 
1 www 9
import Spinner from "../../../shared/loading-spinner/Spinner";
4405 stevensc 10
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
11
import PhoneInput from "react-phone-input-2";
12
import SwitchInput from "../shared/switch-input/SwitchInput";
13
import "react-phone-input-2/lib/style.css";
1 www 14
 
4115 stevensc 15
const BasicSettings = ({
16
  addNotification = function () { }, // Redux action
17
  timezones = {}
18
}) => {
1 www 19
 
20
  const [loading, setLoading] = useState(false);
4405 stevensc 21
  const [isAdult, setIsAdult] = useState(false);
1 www 22
 
4408 stevensc 23
  const { register, handleSubmit, setValue, watch, errors } = useForm()
1 www 24
 
25
  useEffect(() => {
26
    register("phone", {
27
      required: "Por favor ingrese su número de teléfono",
4408 stevensc 28
    })
4113 efrain 29
    register("timezone", {
30
      required: "Por favor ingrese su zona horaria",
4408 stevensc 31
    })
1 www 32
  }, []);
33
 
34
  const handleOnSubmit = async (data) => {
35
    setLoading(true);
36
    const formData = new FormData();
4408 stevensc 37
 
4405 stevensc 38
    formData.append("is_adult", isAdult ? 'y' : 'n')
4408 stevensc 39
    Object.entries(data).map(([key, value]) => formData.append(key, value))
4405 stevensc 40
 
4408 stevensc 41
    axios.post("/account-settings/basic", formData)
42
      .then(({ data: response }) => {
43
        if (!response.success) {
44
          const errorMessage = typeof response.data === "string" ? response.data : "Ha ocurrido un error, Por favor intente mas tarde"
45
          return addNotification({ style: "danger", msg: errorMessage })
1 www 46
        }
4408 stevensc 47
 
48
        return addNotification({ style: "success", msg: response.data })
49
      })
50
      .finally(() => setLoading(false))
1 www 51
  };
52
 
53
  useEffect(async () => {
54
    setLoading(true);
4405 stevensc 55
    axios
56
      .get("/account-settings/basic")
4407 stevensc 57
      .then(({ data: response }) => {
58
        if (response.success) {
59
          Object
60
            .entries(response.data)
61
            .map(([key, value]) => {
4408 stevensc 62
              console.log(key, value)
4407 stevensc 63
              if (key === 'is_adult') {
64
                return setIsAdult(value === 'y' ? true : false)
65
              }
66
              return setValue(key, value)
67
            })
4405 stevensc 68
        }
69
      })
70
      .finally(() => setLoading(false))
1 www 71
  }, []);
72
 
73
  return (
4405 stevensc 74
    <div className="acc-setting">
2642 stevensc 75
      <h2>Básica</h2>
4405 stevensc 76
      {loading
77
        ? <Spinner />
78
        :
2641 stevensc 79
        <form onSubmit={handleSubmit(handleOnSubmit)}>
4405 stevensc 80
          <div className="inputs__container">
2641 stevensc 81
            <div className="cp-field">
82
              <label htmlFor="first_name">Nombre</label>
83
              <input
84
                type="text"
85
                name="first_name"
4405 stevensc 86
                ref={register({ required: "Por favor ingrese su nombre" })}
2641 stevensc 87
              />
88
              {<FormErrorFeedback>{errors?.first_name?.message}</FormErrorFeedback>}
89
            </div>
90
            <div className="cp-field">
91
              <label htmlFor="last_name">Apellido</label>
92
              <input
93
                type="text"
94
                name="last_name"
95
                ref={register({
96
                  required: "Por favor ingrese su apellido",
97
                })}
98
              />
99
              {<FormErrorFeedback>{errors?.last_name?.message}</FormErrorFeedback>}
100
            </div>
101
          </div>
4405 stevensc 102
          <div className="inputs__container">
2641 stevensc 103
            <div className="cp-field">
104
              <label htmlFor="last_name">Email</label>
105
              <input
106
                type="text"
107
                name="email"
108
                ref={register({
4408 stevensc 109
                  required: "Por favor ingrese su correo electrónico",
2641 stevensc 110
                  pattern: {
4115 stevensc 111
                    value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
2641 stevensc 112
                    message: "Por favor ingrese un correo válido",
113
                  },
114
                })}
115
              />
116
              {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
117
            </div>
2642 stevensc 118
            <div className="cp-field">
4405 stevensc 119
              <label htmlFor="phone">Teléfono</label>
2642 stevensc 120
              <PhoneInput
121
                name="phone"
2644 stevensc 122
                inputClass="custom-input"
2642 stevensc 123
                value={watch("phone")}
4408 stevensc 124
                onChange={(phone) => setValue("phone", phone)}
2642 stevensc 125
              />
126
              {<FormErrorFeedback>{errors?.phone?.message}</FormErrorFeedback>}
127
            </div>
2641 stevensc 128
          </div>
4405 stevensc 129
          <div className="inputs__container">
2641 stevensc 130
            <div className="cp-field">
131
              <label htmlFor="gender">Género</label>
132
              <select
133
                name="gender"
134
                id="gender"
135
                defaultValue=""
136
                ref={register}
137
              >
138
                <option value="" hidden>
139
                  Seleccione un género
140
                </option>
141
                <option value="m">Masculino</option>
142
                <option value="f">Femenino</option>
143
              </select>
144
              {<FormErrorFeedback>{errors?.gender?.message}</FormErrorFeedback>}
145
            </div>
4113 efrain 146
            <div className="cp-field">
4115 stevensc 147
              <label htmlFor="timezone">Zona horaria</label>
148
              <select
149
                name="timezone"
150
                id="timezone"
151
                defaultValue=""
152
                ref={register({
153
                  required: "Por favor elige una Zona horaria",
154
                })}
155
              >
156
                <option value="" hidden>
157
                  Zona horaria
158
                </option>
159
                {Object.entries(timezones).map(([key, value]) => (
160
                  <option value={key} key={key}>
161
                    {value}
162
                  </option>
163
                ))}
164
              </select>
165
              {<FormErrorFeedback>{errors?.timezone?.message}</FormErrorFeedback>}
4113 efrain 166
            </div>
2641 stevensc 167
          </div>
4405 stevensc 168
          <SwitchInput
169
            isChecked={isAdult}
170
            setValue={(value) => setIsAdult(value)}
171
          />
172
          <button type="submit" className="btn btn-secondary">
173
            Guardar
174
          </button>
2641 stevensc 175
        </form>
176
      }
4405 stevensc 177
 
178
 
179
    </div >
1 www 180
  );
181
};
182
 
183
const mapDispatchToProps = {
184
  addNotification: (notification) => addNotification(notification),
185
};
186
 
187
export default connect(null, mapDispatchToProps)(BasicSettings);