Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4383 | Rev 4395 | 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
4384 stevensc 9
import Spinner from "../../../shared/loading-spinner/Spinner";
4383 stevensc 10
import SwitchInput from "../shared/switch-input/SwitchInput";
1 www 11
 
4379 stevensc 12
const PRIVACY_OPTIONS = [
13
  {
4380 stevensc 14
    label: 'Mostrar en la búsqueda',
15
    input_name: 'show_in_search',
4379 stevensc 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
 
4380 stevensc 50
  const handleChecked = (value, element) => {
51
    setOptions((prevNotifications) => prevNotifications
52
      .map((notification) =>
53
        notification.input_name === element
54
          ? { ...notification, value: Boolean(value) }
55
          : notification
56
      ))
57
  }
58
 
4379 stevensc 59
  useEffect(() => {
60
    setLoading(true)
61
    axios
62
      .get("/account-settings/privacy")
63
      .then(({ data: response }) => {
64
        if (response.success) {
65
          Object
66
            .entries(response.data)
67
            .map(([key, value]) =>
4381 stevensc 68
              setOptions((prevOption) => prevOption
69
                .map((option) =>
70
                  option.input_name === key
4382 stevensc 71
                    ? { ...option, value: Boolean(value) }
4381 stevensc 72
                    : option
4379 stevensc 73
                )))
74
        }
75
      })
76
      .finally(() => setLoading(false))
77
 
1 www 78
  }, []);
79
 
80
  return (
4379 stevensc 81
    <div className="acc-setting">
4380 stevensc 82
      <h3>Privacidad</h3>
1 www 83
      <form onSubmit={handleSubmit(handleOnSubmit)}>
4380 stevensc 84
        {options.map((option, index) => {
85
          return (
86
            <div className="notbat" key={index}>
87
              <span>
88
                {option.label}
89
              </span>
90
              <SwitchInput
91
                isChecked={option.value}
92
                setValue={(value) => handleChecked(value, option.input_name)}
93
              />
94
            </div>
95
          )
96
        })}
4379 stevensc 97
        <button type="submit" className="btn btn-primary">
98
          Guardar
99
        </button>
1 www 100
      </form>
4379 stevensc 101
      {loading && <Spinner />}
102
    </div>
1 www 103
  );
104
};
105
 
106
const mapDispatchToProps = {
107
  addNotification: (notification) => addNotification(notification),
108
};
109
 
110
export default connect(null, mapDispatchToProps)(Privacy);