Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4115 | Rev 4407 | 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")
78
      .then((response) => {
79
        const resData = response.data;
80
        if (resData.success) {
81
          Object.entries(resData.data).map(([key, value]) => {
82
            if (key === 'is_adult') {
83
              return setIsAdult(value === 'y' ? true : false)
84
            }
85
            setValue(key, value);
86
          });
87
        }
88
      })
89
      .finally(() => setLoading(false))
1 www 90
  }, []);
91
 
92
  return (
4405 stevensc 93
    <div className="acc-setting">
2642 stevensc 94
      <h2>Básica</h2>
4405 stevensc 95
      {loading
96
        ? <Spinner />
97
        :
2641 stevensc 98
        <form onSubmit={handleSubmit(handleOnSubmit)}>
4405 stevensc 99
          <div className="inputs__container">
2641 stevensc 100
            <div className="cp-field">
101
              <label htmlFor="first_name">Nombre</label>
102
              <input
103
                type="text"
104
                name="first_name"
105
                id="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
                id="last_name"
116
                ref={register({
117
                  required: "Por favor ingrese su apellido",
118
                })}
119
              />
120
              {<FormErrorFeedback>{errors?.last_name?.message}</FormErrorFeedback>}
121
            </div>
122
          </div>
4405 stevensc 123
          <div className="inputs__container">
2641 stevensc 124
            <div className="cp-field">
125
              <label htmlFor="last_name">Email</label>
126
              <input
127
                type="text"
128
                name="email"
129
                id="email"
130
                ref={register({
131
                  required: "Por favor ingrese su apellido",
132
                  pattern: {
4115 stevensc 133
                    value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
2641 stevensc 134
                    message: "Por favor ingrese un correo válido",
135
                  },
136
                })}
137
              />
138
              {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
139
            </div>
2642 stevensc 140
            <div className="cp-field">
4405 stevensc 141
              <label htmlFor="phone">Teléfono</label>
2642 stevensc 142
              <PhoneInput
143
                name="phone"
2644 stevensc 144
                inputClass="custom-input"
2642 stevensc 145
                value={watch("phone")}
146
                onChange={(phone) => {
147
                  setValue("phone", phone);
148
                }}
149
              />
150
              {<FormErrorFeedback>{errors?.phone?.message}</FormErrorFeedback>}
151
            </div>
2641 stevensc 152
          </div>
4405 stevensc 153
          <div className="inputs__container">
2641 stevensc 154
            <div className="cp-field">
155
              <label htmlFor="gender">Género</label>
156
              <select
157
                name="gender"
158
                id="gender"
159
                defaultValue=""
160
                ref={register}
161
              >
162
                <option value="" hidden>
163
                  Seleccione un género
164
                </option>
165
                <option value="m">Masculino</option>
166
                <option value="f">Femenino</option>
167
              </select>
168
              {<FormErrorFeedback>{errors?.gender?.message}</FormErrorFeedback>}
169
            </div>
4113 efrain 170
            <div className="cp-field">
4115 stevensc 171
              <label htmlFor="timezone">Zona horaria</label>
172
              <select
173
                name="timezone"
174
                id="timezone"
175
                defaultValue=""
176
                ref={register({
177
                  required: "Por favor elige una Zona horaria",
178
                })}
179
              >
180
                <option value="" hidden>
181
                  Zona horaria
182
                </option>
183
                {Object.entries(timezones).map(([key, value]) => (
184
                  <option value={key} key={key}>
185
                    {value}
186
                  </option>
187
                ))}
188
              </select>
189
              {<FormErrorFeedback>{errors?.timezone?.message}</FormErrorFeedback>}
4113 efrain 190
            </div>
2641 stevensc 191
          </div>
4405 stevensc 192
          <SwitchInput
193
            isChecked={isAdult}
194
            setValue={(value) => setIsAdult(value)}
195
          />
196
          <button type="submit" className="btn btn-secondary">
197
            Guardar
198
          </button>
2641 stevensc 199
        </form>
200
      }
4405 stevensc 201
 
202
 
203
    </div >
1 www 204
  );
205
};
206
 
207
const mapDispatchToProps = {
208
  addNotification: (notification) => addNotification(notification),
209
};
210
 
211
export default connect(null, mapDispatchToProps)(BasicSettings);