Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2641 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React, { useEffect, useState } from "react";
2
import PhoneInput from "react-phone-input-2";
3
import "react-phone-input-2/lib/style.css";
4
import { useForm } from "react-hook-form";
5
import {axios} from '../../../utils';
6
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
7
import { connect } from "react-redux";
8
import { addNotification } from "../../../redux/notification/notification.actions";
9
import styled from "styled-components";
10
import Spinner from "../../../shared/loading-spinner/Spinner";
11
 
12
const StyledSpinnerContainer = styled.div`
13
  position: absolute;
14
  left: 0;
15
  top: 0;
16
  width: 100%;
17
  height: 100%;
18
  background: rgba(255, 255, 255, 0.4);
19
  display: flex;
20
  justify-content: center;
21
  align-items: center;
22
  z-index: 300;
23
`;
24
 
25
const BasicSettings = (props) => {
26
  // redux destructuring
27
  const { addNotification } = props;
28
 
29
  // states
30
  const [loading, setLoading] = useState(false);
31
 
32
  const { register, handleSubmit, setValue, watch, setError, errors } = useForm(
33
    {
34
      defaultValues: {
35
        phone: "",
36
      },
37
    }
38
  );
39
 
40
  useEffect(() => {
41
    register("phone", {
42
      required: "Por favor ingrese su número de teléfono",
43
    });
44
  }, []);
45
 
46
  const handleOnSubmit = async (data) => {
47
    setLoading(true);
48
    const formData = new FormData();
49
    Object.entries(data).map(([key, value]) => {
50
      formData.append(key, value);
51
    });
52
    await axios.post("/account-settings/basic", formData).then((response) => {
53
      const resData = response.data;
54
      if (resData.success) {
55
        addNotification({
56
          style: "success",
57
          msg: resData.data,
58
        });
59
      } else {
60
        if (typeof resData.data === "object") {
61
          resData.data;
62
          Object.entries(resData.data).map(([key, value]) => {
63
            setError(key, { type: "manual", message: value[0] });
64
          });
65
        } else {
66
          const errorMessage =
67
            typeof resData.data === "string"
68
              ? resData.data
69
              : "Ha ocurrido un error, Por favor intente mas tarde";
70
          addNotification({
71
            style: "danger",
72
            msg: errorMessage,
73
          });
74
        }
75
      }
76
    });
77
    setLoading(false);
78
  };
79
 
80
  useEffect(async () => {
81
    setLoading(true);
82
    await axios.get("/account-settings/basic").then((response) => {
83
      const resData = response.data;
84
      if (resData.success) {
85
        Object.entries(resData.data).map(([key, value]) => {
86
          setValue(key, value);
87
        });
88
      }
89
    });
90
    setLoading(false);
91
  }, []);
92
 
93
  return (
94
    <div className="acc-setting" style={{ position: "relative" }}>
95
      <h3>Básica</h3>
96
      <form onSubmit={handleSubmit(handleOnSubmit)}>
97
        <div className="cp-field">
98
          <label htmlFor="first_name">Nombre</label>
99
          <input
100
            type="text"
101
            name="first_name"
102
            id="first_name"
103
            ref={register({
104
              required: "Por favor ingrese su nombre",
105
            })}
106
          />
107
          {<FormErrorFeedback>{errors?.first_name?.message}</FormErrorFeedback>}
108
        </div>
109
        <div className="cp-field">
110
          <label htmlFor="last_name">Apellido</label>
111
          <input
112
            type="text"
113
            name="last_name"
114
            id="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 className="cp-field">
122
          <label htmlFor="phone">Teléfono</label>
123
          <PhoneInput
124
            name="phone"
125
            value={watch("phone")}
126
            onChange={(phone) => {
127
              setValue("phone", phone);
128
            }}
129
          />
130
          {<FormErrorFeedback>{errors?.phone?.message}</FormErrorFeedback>}
131
        </div>
132
        <div className="cp-field">
133
          <label htmlFor="last_name">Email</label>
134
          <input
135
            type="text"
136
            name="email"
137
            id="email"
138
            ref={register({
139
              required: "Por favor ingrese su apellido",
140
              pattern: {
141
                value: /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/,
142
                message: "Por favor ingrese un correo válido",
143
              },
144
            })}
145
          />
146
          {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
147
        </div>
148
        <div className="cp-field">
149
          <label htmlFor="gender">Género</label>
150
          <select
151
            name="gender"
152
            id="gender"
153
            defaultValue=""
154
            className="form-control"
155
            ref={register}
156
          >
157
            <option value="" hidden>
158
              Seleccione un género
159
            </option>
160
            <option value="m">Masculino</option>
161
            <option value="f">Femenino</option>
162
          </select>
163
          {<FormErrorFeedback>{errors?.gender?.message}</FormErrorFeedback>}
164
        </div>
165
        <div className="save-stngs pd2">
166
          <ul>
167
            <li>
168
              <button type="submit" className="btn-save-basic">
169
                Guardar
170
              </button>
171
            </li>
172
          </ul>
173
        </div>
174
        {/* <!--save-stngs end--> */}
175
        {/* <?php echo $this->form()->closeTag($form); ?>	 */}
176
      </form>
177
      {loading && (
178
        <StyledSpinnerContainer>
179
          <Spinner />
180
        </StyledSpinnerContainer>
181
      )}
182
    </div>
183
  );
184
};
185
 
186
// const mapStateToProps = (state) => ({
187
 
188
// })
189
 
190
const mapDispatchToProps = {
191
  addNotification: (notification) => addNotification(notification),
192
};
193
 
194
export default connect(null, mapDispatchToProps)(BasicSettings);