Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2642 | Rev 2644 | Ir a la última revisión | | Comparar con el anterior | 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";
2641 stevensc 5
import { axios } from '../../../utils';
1 www 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 (
2641 stevensc 94
    <div className="settings-container">
2642 stevensc 95
      <h2>Básica</h2>
2641 stevensc 96
      <div className="acc-setting_content">
97
        <form onSubmit={handleSubmit(handleOnSubmit)}>
2643 stevensc 98
          <div className="d-flex flex-wrap pb-3" style={{ gap: '1rem' }}>
2641 stevensc 99
            <div className="cp-field">
100
              <label htmlFor="first_name">Nombre</label>
101
              <input
102
                type="text"
103
                name="first_name"
104
                id="first_name"
105
                ref={register({
106
                  required: "Por favor ingrese su nombre",
107
                })}
108
              />
109
              {<FormErrorFeedback>{errors?.first_name?.message}</FormErrorFeedback>}
110
            </div>
111
            <div className="cp-field">
112
              <label htmlFor="last_name">Apellido</label>
113
              <input
114
                type="text"
115
                name="last_name"
116
                id="last_name"
117
                ref={register({
118
                  required: "Por favor ingrese su apellido",
119
                })}
120
              />
121
              {<FormErrorFeedback>{errors?.last_name?.message}</FormErrorFeedback>}
122
            </div>
123
          </div>
2643 stevensc 124
          <div className="d-flex flex-wrap pb-3" style={{ gap: '1rem' }}>
2641 stevensc 125
            <div className="cp-field">
126
              <label htmlFor="last_name">Email</label>
127
              <input
128
                type="text"
129
                name="email"
130
                id="email"
131
                ref={register({
132
                  required: "Por favor ingrese su apellido",
133
                  pattern: {
134
                    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])+)\])/,
135
                    message: "Por favor ingrese un correo válido",
136
                  },
137
                })}
138
              />
139
              {<FormErrorFeedback>{errors?.email?.message}</FormErrorFeedback>}
140
            </div>
2642 stevensc 141
            <div className="cp-field">
142
              <label htmlFor="phone">Teléfono</label>
143
              <PhoneInput
144
                name="phone"
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>
2643 stevensc 153
          <div className="d-flex flex-wrap" style={{ gap: '1rem' }}>
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>
2642 stevensc 170
            <div className="col-6 d-flex align-items-center justify-content-center">
2643 stevensc 171
              <button type="submit" className="btn btn-secondary mt-4">
2642 stevensc 172
                Guardar
173
              </button>
174
            </div>
2641 stevensc 175
          </div>
176
        </form>
177
      </div>
178
      {loading &&
1 www 179
        <StyledSpinnerContainer>
180
          <Spinner />
181
        </StyledSpinnerContainer>
2641 stevensc 182
      }
1 www 183
    </div>
184
  );
185
};
186
 
187
// const mapStateToProps = (state) => ({
188
 
189
// })
190
 
191
const mapDispatchToProps = {
192
  addNotification: (notification) => addNotification(notification),
193
};
194
 
195
export default connect(null, mapDispatchToProps)(BasicSettings);