Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1011 | 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 styled from "styled-components";
3
import { useForm } from "react-hook-form";
4
import SwitchInput from "./switch-input/SwitchInput";
5
import { addNotification } from "../../../redux/notification/notification.actions";
6
import { connect } from "react-redux";
7
import Spinner from "../../../shared/loading-spinner/Spinner";
8
import {axios} from "../../../utils";
9
 
10
const StyledSettings = styled.div`
11
  .notbat {
12
    display: flex;
13
    justify-content: space-between;
14
    align-items: center;
15
    padding: 1rem;
16
    border-bottom: 1px solid #f2f2f2;
17
    position: relative;
18
  }
19
`;
20
 
21
const StyledSpinnerContainer = styled.div`
22
  position: absolute;
23
  left: 0;
24
  top: 0;
25
  width: 100%;
26
  height: 100%;
27
  background: rgba(255, 255, 255, 0.4);
28
  display: flex;
29
  justify-content: center;
30
  align-items: center;
31
  z-index: 300;
32
`;
33
 
34
const Privacy = (props) => {
35
  // redux destructuring
36
  const { addNotification } = props;
37
 
38
  const { register, handleSubmit, setError, setValue } = useForm();
39
 
40
  // states
41
  const [loading, setLoading] = useState(false);
42
 
43
  const handleOnSubmit = async (data) => {
44
    setLoading(true);
45
    const formData = new FormData();
46
    Object.entries(data).map(([key, value]) => {
47
      if (value) formData.append(key, value);
48
    });
49
    await axios.post("/account-settings/privacy", formData).then((response) => {
50
      const resData = response.data;
51
      if (resData.success) {
52
        addNotification({
53
          style: "success",
54
          msg: resData.data,
55
        });
56
      } else {
57
        if (typeof resData.data === "object") {
58
          resData.data;
59
          Object.entries(resData.data).map(([key, value]) => {
60
            setError(key, { type: "manual", message: value[0] });
61
          });
62
        } else {
63
          const errorMessage =
64
            typeof resData.data === "string"
65
              ? resData.data
66
              : "Ha ocurrido un error, Por favor intente mas tarde";
67
          addNotification({
68
            style: "danger",
69
            msg: errorMessage,
70
          });
71
        }
72
      }
73
    });
74
    setLoading(false);
75
  };
76
 
77
  useEffect(async () => {
78
    setLoading(true);
79
    await axios.get("/account-settings/privacy").then((response) => {
80
      const resData = response.data;
81
      if (resData.success) {
82
        Object.entries(resData.data).map(([key, value]) => {
83
          setValue(key, value);
84
        });
85
      }
86
    });
87
    setLoading(false);
88
  }, []);
89
 
90
  return (
91
    <StyledSettings className="acc-setting">
92
      <h3>Notificationes</h3>
93
      <form onSubmit={handleSubmit(handleOnSubmit)}>
94
        <div className="notbat">
95
          Mostrar en la busqueda
96
          <SwitchInput register={register} name="show_in_search" />
97
        </div>
98
        <div className="save-stngs pd2">
99
          <ul>
100
            <li>
101
              <button type="submit" className="btn-save-basic">
102
                Guardar
103
              </button>
104
            </li>
105
          </ul>
106
        </div>
107
        {/* <!--save-stngs end--> */}
108
        {/* <?php echo $this->form()->closeTag($form); ?>	 */}
109
      </form>
110
      {loading && (
111
        <StyledSpinnerContainer>
112
          <Spinner />
113
        </StyledSpinnerContainer>
114
      )}
115
    </StyledSettings>
116
  );
117
};
118
 
119
// const mapStateToProps = (state) => ({
120
 
121
// })
122
 
123
const mapDispatchToProps = {
124
  addNotification: (notification) => addNotification(notification),
125
};
126
 
127
export default connect(null, mapDispatchToProps)(Privacy);