Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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