Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4113 | Rev 4405 | 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 PhoneInput from "react-phone-input-2";
5
import "react-phone-input-2/lib/style.css";
6
import { useForm } from "react-hook-form";
2641 stevensc 7
import { axios } from '../../../utils';
1 www 8
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
9
import { connect } from "react-redux";
10
import { addNotification } from "../../../redux/notification/notification.actions";
11
import styled from "styled-components";
12
import Spinner from "../../../shared/loading-spinner/Spinner";
13
 
14
const StyledSpinnerContainer = styled.div`
15
  position: absolute;
16
  left: 0;
17
  top: 0;
18
  width: 100%;
19
  height: 100%;
20
  background: rgba(255, 255, 255, 0.4);
21
  display: flex;
22
  justify-content: center;
23
  align-items: center;
24
  z-index: 300;
25
`;
26
 
4115 stevensc 27
const BasicSettings = ({
28
  addNotification = function () { }, // Redux action
29
  timezones = {}
30
}) => {
1 www 31
 
32
  const [loading, setLoading] = useState(false);
33
 
4115 stevensc 34
  const { register, handleSubmit, setValue, watch, setError, errors } = useForm({
35
    defaultValues: {
36
      phone: "",
37
      timezone: "",
38
    },
39
  })
1 www 40
 
41
  useEffect(() => {
42
    register("phone", {
43
      required: "Por favor ingrese su número de teléfono",
44
    });
4113 efrain 45
    register("timezone", {
46
      required: "Por favor ingrese su zona horaria",
47
    });
1 www 48
  }, []);
49
 
50
  const handleOnSubmit = async (data) => {
51
    setLoading(true);
52
    const formData = new FormData();
53
    Object.entries(data).map(([key, value]) => {
54
      formData.append(key, value);
55
    });
56
    await axios.post("/account-settings/basic", formData).then((response) => {
57
      const resData = response.data;
58
      if (resData.success) {
59
        addNotification({
60
          style: "success",
61
          msg: resData.data,
62
        });
63
      } else {
64
        if (typeof resData.data === "object") {
65
          resData.data;
66
          Object.entries(resData.data).map(([key, value]) => {
67
            setError(key, { type: "manual", message: value[0] });
68
          });
69
        } else {
70
          const errorMessage =
71
            typeof resData.data === "string"
72
              ? resData.data
73
              : "Ha ocurrido un error, Por favor intente mas tarde";
74
          addNotification({
75
            style: "danger",
76
            msg: errorMessage,
77
          });
78
        }
79
      }
80
    });
81
    setLoading(false);
82
  };
83
 
84
  useEffect(async () => {
85
    setLoading(true);
86
    await axios.get("/account-settings/basic").then((response) => {
87
      const resData = response.data;
88
      if (resData.success) {
89
        Object.entries(resData.data).map(([key, value]) => {
90
          setValue(key, value);
91
        });
92
      }
93
    });
94
    setLoading(false);
95
  }, []);
96
 
97
  return (
2641 stevensc 98
    <div className="settings-container">
2642 stevensc 99
      <h2>Básica</h2>
2641 stevensc 100
      <div className="acc-setting_content">
101
        <form onSubmit={handleSubmit(handleOnSubmit)}>
2643 stevensc 102
          <div className="d-flex flex-wrap pb-3" style={{ gap: '1rem' }}>
2641 stevensc 103
            <div className="cp-field">
104
              <label htmlFor="first_name">Nombre</label>
105
              <input
106
                type="text"
107
                name="first_name"
108
                id="first_name"
109
                ref={register({
110
                  required: "Por favor ingrese su nombre",
111
                })}
112
              />
113
              {<FormErrorFeedback>{errors?.first_name?.message}</FormErrorFeedback>}
114
            </div>
115
            <div className="cp-field">
116
              <label htmlFor="last_name">Apellido</label>
117
              <input
118
                type="text"
119
                name="last_name"
120
                id="last_name"
121
                ref={register({
122
                  required: "Por favor ingrese su apellido",
123
                })}
124
              />
125
              {<FormErrorFeedback>{errors?.last_name?.message}</FormErrorFeedback>}
126
            </div>
127
          </div>
2643 stevensc 128
          <div className="d-flex flex-wrap pb-3" style={{ gap: '1rem' }}>
2641 stevensc 129
            <div className="cp-field">
130
              <label htmlFor="last_name">Email</label>
131
              <input
132
                type="text"
133
                name="email"
134
                id="email"
135
                ref={register({
136
                  required: "Por favor ingrese su apellido",
137
                  pattern: {
4115 stevensc 138
                    value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
2641 stevensc 139
                    message: "Por favor ingrese un correo válido",
140
                  },
141
                })}
142
              />
143
              {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
144
            </div>
2642 stevensc 145
            <div className="cp-field">
2645 stevensc 146
              <label className="mb-1" htmlFor="phone">Teléfono</label>
2642 stevensc 147
              <PhoneInput
148
                name="phone"
2644 stevensc 149
                inputClass="custom-input"
2642 stevensc 150
                value={watch("phone")}
151
                onChange={(phone) => {
152
                  setValue("phone", phone);
153
                }}
154
              />
155
              {<FormErrorFeedback>{errors?.phone?.message}</FormErrorFeedback>}
156
            </div>
2641 stevensc 157
          </div>
2643 stevensc 158
          <div className="d-flex flex-wrap" style={{ gap: '1rem' }}>
2641 stevensc 159
            <div className="cp-field">
160
              <label htmlFor="gender">Género</label>
161
              <select
162
                name="gender"
163
                id="gender"
164
                defaultValue=""
165
                ref={register}
166
              >
167
                <option value="" hidden>
168
                  Seleccione un género
169
                </option>
170
                <option value="m">Masculino</option>
171
                <option value="f">Femenino</option>
172
              </select>
173
              {<FormErrorFeedback>{errors?.gender?.message}</FormErrorFeedback>}
174
            </div>
4115 stevensc 175
          </div>
176
          <div className="d-flex flex-wrap" style={{ gap: '1rem' }}>
4113 efrain 177
            <div className="cp-field">
4115 stevensc 178
              <label htmlFor="timezone">Zona horaria</label>
179
              <select
180
                name="timezone"
181
                id="timezone"
182
                defaultValue=""
183
                ref={register({
184
                  required: "Por favor elige una Zona horaria",
185
                })}
186
              >
187
                <option value="" hidden>
188
                  Zona horaria
189
                </option>
190
                {Object.entries(timezones).map(([key, value]) => (
191
                  <option value={key} key={key}>
192
                    {value}
193
                  </option>
194
                ))}
195
              </select>
196
              {<FormErrorFeedback>{errors?.timezone?.message}</FormErrorFeedback>}
4113 efrain 197
            </div>
2644 stevensc 198
            <div className="col-6 mx-auto d-flex align-items-center justify-content-center">
2643 stevensc 199
              <button type="submit" className="btn btn-secondary mt-4">
2642 stevensc 200
                Guardar
201
              </button>
202
            </div>
2641 stevensc 203
          </div>
204
        </form>
205
      </div>
206
      {loading &&
1 www 207
        <StyledSpinnerContainer>
208
          <Spinner />
209
        </StyledSpinnerContainer>
2641 stevensc 210
      }
1 www 211
    </div>
212
  );
213
};
214
 
215
// const mapStateToProps = (state) => ({
216
 
217
// })
218
 
219
const mapDispatchToProps = {
220
  addNotification: (notification) => addNotification(notification),
221
};
222
 
223
export default connect(null, mapDispatchToProps)(BasicSettings);