Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4411 | 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
 
4412 stevensc 23
  const { register, handleSubmit, setValue, watch, errors, getValues } = 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)
4412 stevensc 61
            .map(([key, value]) => {
62
              const currentValue = getValues(key)
63
 
64
              if (key === 'is_adult') {
65
                return setIsAdult(value === 'y' ? true : false)
66
              }
67
 
68
              if (!currentValue) {
69
                register(key, { required: true })
70
              }
71
 
72
              setValue(key, value)
73
            })
4405 stevensc 74
        }
75
      })
76
      .finally(() => setLoading(false))
1 www 77
  }, []);
78
 
79
  return (
4405 stevensc 80
    <div className="acc-setting">
2642 stevensc 81
      <h2>Básica</h2>
4405 stevensc 82
      {loading
83
        ? <Spinner />
84
        :
2641 stevensc 85
        <form onSubmit={handleSubmit(handleOnSubmit)}>
4405 stevensc 86
          <div className="inputs__container">
2641 stevensc 87
            <div className="cp-field">
88
              <label htmlFor="first_name">Nombre</label>
4411 stevensc 89
              <input type="text" {...register("first_name")} />
4410 stevensc 90
              {errors.first_name && <FormErrorFeedback>Por favor ingrese su nombre</FormErrorFeedback>}
2641 stevensc 91
            </div>
92
            <div className="cp-field">
93
              <label htmlFor="last_name">Apellido</label>
4411 stevensc 94
              <input {...register("text")} name="last_name" />
4410 stevensc 95
              {errors.last_name && <FormErrorFeedback>Por favor ingrese su apellido</FormErrorFeedback>}
2641 stevensc 96
            </div>
97
          </div>
4405 stevensc 98
          <div className="inputs__container">
2641 stevensc 99
            <div className="cp-field">
100
              <label htmlFor="last_name">Email</label>
4411 stevensc 101
              <input type="text" {...register("email")} />
4410 stevensc 102
              {errors.email && <FormErrorFeedback>Por favor ingrese un correo electrónico valido</FormErrorFeedback>}
2641 stevensc 103
            </div>
2642 stevensc 104
            <div className="cp-field">
4405 stevensc 105
              <label htmlFor="phone">Teléfono</label>
2642 stevensc 106
              <PhoneInput
107
                name="phone"
2644 stevensc 108
                inputClass="custom-input"
2642 stevensc 109
                value={watch("phone")}
4408 stevensc 110
                onChange={(phone) => setValue("phone", phone)}
2642 stevensc 111
              />
4410 stevensc 112
              {errors.phone && <FormErrorFeedback>Por favor ingrese un número telefónico valido</FormErrorFeedback>}
2642 stevensc 113
            </div>
2641 stevensc 114
          </div>
4405 stevensc 115
          <div className="inputs__container">
2641 stevensc 116
            <div className="cp-field">
117
              <label htmlFor="gender">Género</label>
118
              <select
4411 stevensc 119
                {...register("gender")}
2641 stevensc 120
              >
121
                <option value="" hidden>
122
                  Seleccione un género
123
                </option>
124
                <option value="m">Masculino</option>
125
                <option value="f">Femenino</option>
126
              </select>
4410 stevensc 127
              {errors.gender && <FormErrorFeedback>Por favor selecciona un genero</FormErrorFeedback>}
2641 stevensc 128
            </div>
4113 efrain 129
            <div className="cp-field">
4115 stevensc 130
              <label htmlFor="timezone">Zona horaria</label>
4411 stevensc 131
              <select {...register("timezone")}>
4115 stevensc 132
                <option value="" hidden>
133
                  Zona horaria
134
                </option>
4410 stevensc 135
                {Object.entries(timezones).map(([key, value]) =>
4115 stevensc 136
                  <option value={key} key={key}>
137
                    {value}
138
                  </option>
4410 stevensc 139
                )}
4115 stevensc 140
              </select>
4410 stevensc 141
              {errors.timezone && <FormErrorFeedback>Por favor selecciona una zona horaria</FormErrorFeedback>}
4113 efrain 142
            </div>
2641 stevensc 143
          </div>
4411 stevensc 144
          <div className="cp-field">
145
            <label htmlFor="timezone">Eres mayor de 18</label>
146
            <SwitchInput
147
              isChecked={isAdult}
148
              setValue={(value) => setIsAdult(value)}
149
            />
150
          </div>
4405 stevensc 151
          <button type="submit" className="btn btn-secondary">
152
            Guardar
153
          </button>
2641 stevensc 154
        </form>
155
      }
4405 stevensc 156
 
157
 
158
    </div >
1 www 159
  );
160
};
161
 
162
const mapDispatchToProps = {
163
  addNotification: (notification) => addNotification(notification),
164
};
165
 
166
export default connect(null, mapDispatchToProps)(BasicSettings);