Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4405 | Rev 4408 | 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
 
4115 stevensc 23
  const { register, handleSubmit, setValue, watch, setError, errors } = useForm({
24
    defaultValues: {
25
      phone: "",
26
      timezone: "",
27
    },
28
  })
1 www 29
 
30
  useEffect(() => {
31
    register("phone", {
32
      required: "Por favor ingrese su número de teléfono",
33
    });
4113 efrain 34
    register("timezone", {
35
      required: "Por favor ingrese su zona horaria",
36
    });
1 www 37
  }, []);
38
 
39
  const handleOnSubmit = async (data) => {
40
    setLoading(true);
41
    const formData = new FormData();
4405 stevensc 42
    Object.entries(data)
43
      .map(([key, value]) => formData.append(key, value))
44
    formData.append("is_adult", isAdult ? 'y' : 'n')
45
 
1 www 46
    await axios.post("/account-settings/basic", formData).then((response) => {
47
      const resData = response.data;
48
      if (resData.success) {
49
        addNotification({
50
          style: "success",
51
          msg: resData.data,
52
        });
53
      } else {
54
        if (typeof resData.data === "object") {
55
          resData.data;
56
          Object.entries(resData.data).map(([key, value]) => {
57
            setError(key, { type: "manual", message: value[0] });
58
          });
59
        } else {
60
          const errorMessage =
61
            typeof resData.data === "string"
62
              ? resData.data
63
              : "Ha ocurrido un error, Por favor intente mas tarde";
64
          addNotification({
65
            style: "danger",
66
            msg: errorMessage,
67
          });
68
        }
69
      }
70
    });
71
    setLoading(false);
72
  };
73
 
74
  useEffect(async () => {
75
    setLoading(true);
4405 stevensc 76
    axios
77
      .get("/account-settings/basic")
4407 stevensc 78
      .then(({ data: response }) => {
79
        if (response.success) {
80
          Object
81
            .entries(response.data)
82
            .map(([key, value]) => {
83
              if (key === 'is_adult') {
84
                return setIsAdult(value === 'y' ? true : false)
85
              }
86
              return setValue(key, value)
87
            })
4405 stevensc 88
        }
89
      })
90
      .finally(() => setLoading(false))
1 www 91
  }, []);
92
 
93
  return (
4405 stevensc 94
    <div className="acc-setting">
2642 stevensc 95
      <h2>Básica</h2>
4405 stevensc 96
      {loading
97
        ? <Spinner />
98
        :
2641 stevensc 99
        <form onSubmit={handleSubmit(handleOnSubmit)}>
4405 stevensc 100
          <div className="inputs__container">
2641 stevensc 101
            <div className="cp-field">
102
              <label htmlFor="first_name">Nombre</label>
103
              <input
104
                type="text"
105
                name="first_name"
4405 stevensc 106
                ref={register({ required: "Por favor ingrese su nombre" })}
2641 stevensc 107
              />
108
              {<FormErrorFeedback>{errors?.first_name?.message}</FormErrorFeedback>}
109
            </div>
110
            <div className="cp-field">
111
              <label htmlFor="last_name">Apellido</label>
112
              <input
113
                type="text"
114
                name="last_name"
115
                ref={register({
116
                  required: "Por favor ingrese su apellido",
117
                })}
118
              />
119
              {<FormErrorFeedback>{errors?.last_name?.message}</FormErrorFeedback>}
120
            </div>
121
          </div>
4405 stevensc 122
          <div className="inputs__container">
2641 stevensc 123
            <div className="cp-field">
124
              <label htmlFor="last_name">Email</label>
125
              <input
126
                type="text"
127
                name="email"
128
                id="email"
129
                ref={register({
130
                  required: "Por favor ingrese su apellido",
131
                  pattern: {
4115 stevensc 132
                    value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
2641 stevensc 133
                    message: "Por favor ingrese un correo válido",
134
                  },
135
                })}
136
              />
137
              {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
138
            </div>
2642 stevensc 139
            <div className="cp-field">
4405 stevensc 140
              <label htmlFor="phone">Teléfono</label>
2642 stevensc 141
              <PhoneInput
142
                name="phone"
2644 stevensc 143
                inputClass="custom-input"
2642 stevensc 144
                value={watch("phone")}
145
                onChange={(phone) => {
146
                  setValue("phone", phone);
147
                }}
148
              />
149
              {<FormErrorFeedback>{errors?.phone?.message}</FormErrorFeedback>}
150
            </div>
2641 stevensc 151
          </div>
4405 stevensc 152
          <div className="inputs__container">
2641 stevensc 153
            <div className="cp-field">
154
              <label htmlFor="gender">Género</label>
155
              <select
156
                name="gender"
157
                id="gender"
158
                defaultValue=""
159
                ref={register}
160
              >
161
                <option value="" hidden>
162
                  Seleccione un género
163
                </option>
164
                <option value="m">Masculino</option>
165
                <option value="f">Femenino</option>
166
              </select>
167
              {<FormErrorFeedback>{errors?.gender?.message}</FormErrorFeedback>}
168
            </div>
4113 efrain 169
            <div className="cp-field">
4115 stevensc 170
              <label htmlFor="timezone">Zona horaria</label>
171
              <select
172
                name="timezone"
173
                id="timezone"
174
                defaultValue=""
175
                ref={register({
176
                  required: "Por favor elige una Zona horaria",
177
                })}
178
              >
179
                <option value="" hidden>
180
                  Zona horaria
181
                </option>
182
                {Object.entries(timezones).map(([key, value]) => (
183
                  <option value={key} key={key}>
184
                    {value}
185
                  </option>
186
                ))}
187
              </select>
188
              {<FormErrorFeedback>{errors?.timezone?.message}</FormErrorFeedback>}
4113 efrain 189
            </div>
2641 stevensc 190
          </div>
4405 stevensc 191
          <SwitchInput
192
            isChecked={isAdult}
193
            setValue={(value) => setIsAdult(value)}
194
          />
195
          <button type="submit" className="btn btn-secondary">
196
            Guardar
197
          </button>
2641 stevensc 198
        </form>
199
      }
4405 stevensc 200
 
201
 
202
    </div >
1 www 203
  );
204
};
205
 
206
const mapDispatchToProps = {
207
  addNotification: (notification) => addNotification(notification),
208
};
209
 
210
export default connect(null, mapDispatchToProps)(BasicSettings);