Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4395 | | 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 }) => {
4395 stevensc 31
      if (value) return formData.append(input_name, value ? 'y' : 'n')
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)}>
4396 stevensc 84
        {loading
85
          ? <Spinner />
86
          : options.map((option, index) => {
87
            return (
88
              <div className="notbat" key={index}>
89
                <span>
90
                  {option.label}
91
                </span>
92
                <SwitchInput
93
                  isChecked={option.value}
94
                  setValue={(value) => handleChecked(value, option.input_name)}
95
                />
96
              </div>
97
            )
98
          })}
4379 stevensc 99
        <button type="submit" className="btn btn-primary">
100
          Guardar
101
        </button>
1 www 102
      </form>
4379 stevensc 103
    </div>
1 www 104
  );
105
};
106
 
107
const mapDispatchToProps = {
108
  addNotification: (notification) => addNotification(notification),
109
};
110
 
111
export default connect(null, mapDispatchToProps)(Privacy);