Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1011 | Rev 4380 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4379 stevensc 1
/* eslint-disable react/prop-types */
1 www 2
import React, { useEffect, useState } from "react";
3
import { useForm } from "react-hook-form";
4379 stevensc 4
import { axios } from "../../../utils";
5
import { connect } from "react-redux";
6
import { addNotification } from "../../../redux/notification/notification.actions";
7
 
8
// Components
1 www 9
import SwitchInput from "./switch-input/SwitchInput";
10
import Spinner from "../../../shared/loading-spinner/Spinner";
11
 
4379 stevensc 12
const PRIVACY_OPTIONS = [
13
  {
14
    label: '',
15
    input_name: '',
16
    value: false
1 www 17
  }
4379 stevensc 18
]
1 www 19
 
4379 stevensc 20
const Privacy = ({ addNotification }) => {
1 www 21
 
22
  const [loading, setLoading] = useState(false);
4379 stevensc 23
  const [options, setOptions] = useState(PRIVACY_OPTIONS);
24
  const { handleSubmit } = useForm();
1 www 25
 
4379 stevensc 26
  const handleOnSubmit = () => {
1 www 27
    setLoading(true);
28
    const formData = new FormData();
4379 stevensc 29
 
30
    options.map(({ input_name, value }) => {
31
      if (value) return formData.append(input_name, value)
1 www 32
    });
4379 stevensc 33
 
34
    axios
35
      .post("/account-settings/privacy", formData)
36
      .then(({ data: response }) => {
37
        if (!response.success) {
38
          typeof response.data === "string"
39
            ? addNotification({ style: "danger", msg: response.data })
40
            : Object
41
              .entries(response.data)
42
              .map(([key, value]) => addNotification({ style: "success", msg: `${key} error: ${value} ` }))
43
 
1 www 44
        }
4379 stevensc 45
        addNotification({ style: "success", msg: response.data })
46
      })
47
      .finally(() => setLoading(false))
1 www 48
  };
49
 
4379 stevensc 50
  useEffect(() => {
51
    setLoading(true)
52
    axios
53
      .get("/account-settings/privacy")
54
      .then(({ data: response }) => {
55
        if (response.success) {
56
          Object
57
            .entries(response.data)
58
            .map(([key, value]) =>
59
              setOptions((prevNotifications) => prevNotifications
60
                .map((notification) =>
61
                  notification.input_name === key
62
                    ? { ...notification, value: Boolean(value) }
63
                    : notification
64
                )))
65
        }
66
      })
67
      .finally(() => setLoading(false))
68
 
1 www 69
  }, []);
70
 
71
  return (
4379 stevensc 72
    <div className="acc-setting">
1011 stevensc 73
      <h3>Notificaciones</h3>
1 www 74
      <form onSubmit={handleSubmit(handleOnSubmit)}>
75
        <div className="notbat">
1011 stevensc 76
          Mostrar en la búsqueda
4379 stevensc 77
          <SwitchInput />
1 www 78
        </div>
4379 stevensc 79
        <button type="submit" className="btn btn-primary">
80
          Guardar
81
        </button>
1 www 82
      </form>
4379 stevensc 83
      {loading && <Spinner />}
84
    </div>
1 www 85
  );
86
};
87
 
88
const mapDispatchToProps = {
89
  addNotification: (notification) => addNotification(notification),
90
};
91
 
92
export default connect(null, mapDispatchToProps)(Privacy);