Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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