Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
4359 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useEffect, useState } from "react";
2716 stevensc 3
import { axios } from "../../../utils";
1 www 4
import { useForm } from "react-hook-form";
5
import { connect } from "react-redux";
6
import { addNotification } from "../../../redux/notification/notification.actions";
4359 stevensc 7
 
8
// Components
1 www 9
import Spinner from "../../../shared/loading-spinner/Spinner";
10
import SwitchInput from "./switch-input/SwitchInput";
11
 
4359 stevensc 12
const Notifications = ({ addNotification }) => {
1 www 13
 
4359 stevensc 14
  const { register, handleSubmit, setValue } = useForm();
1 www 15
  const [loading, setLoading] = useState(false);
16
 
4363 stevensc 17
  const handleOnSubmit = (data) => {
1 www 18
    setLoading(true);
19
    const formData = new FormData();
4363 stevensc 20
 
21
    Object.
22
      entries(data)
4364 stevensc 23
      .map(([key, value]) => value && formData.append(key, value === 'y' ? value : 'n'))
4363 stevensc 24
 
25
    axios
1 www 26
      .post("/account-settings/notification", formData)
4363 stevensc 27
      .then(({ data: response }) => {
28
        if (!response.success) {
29
          const errorMessage = typeof response.data === "string" ? response.data : "Ha ocurrido un error, Por favor intente mas tarde"
30
          return addNotification({ style: "danger", msg: errorMessage })
1 www 31
        }
4363 stevensc 32
 
33
        return addNotification({ style: "success", msg: response.data })
34
      })
35
      .finally(() => setLoading(false))
1 www 36
  };
37
 
4363 stevensc 38
  useEffect(() => {
1 www 39
    setLoading(true);
4363 stevensc 40
    axios
41
      .get("/account-settings/notification")
42
      .then(({ data: response }) => {
43
        if (response.success) {
44
          Object
45
            .entries(response.data)
46
            .map(([key, value]) => setValue(key, value))
47
        }
48
      })
49
      .finally(() => setLoading(false))
1 www 50
  }, []);
51
 
4359 stevensc 52
  const NOTIFICATION_OPTIONS = [
53
    {
54
      label: 'Cuando recibo una solicitud de conexión',
55
      input_name: 'receive_connection_request'
56
    },
57
    {
58
      label: 'Cuando recibo una solicitud para unirme a un grupo',
4360 stevensc 59
      input_name: "receive_invitation_group"
4359 stevensc 60
    },
61
 
62
    {
63
      label: 'Cuando aceptan mi solicitud para unirme a un grupo',
4360 stevensc 64
      input_name: "accept_my_request_join_group"
4359 stevensc 65
    },
66
    {
67
      label: 'Cuando recibo una solicitud para unirme a una empresa',
4360 stevensc 68
      input_name: "receive_invitation_company"
4359 stevensc 69
    },
70
    {
71
      label: 'Cuando le dan me gusta a una de mis publicaciones',
4360 stevensc 72
      input_name: "like_my_feed"
4359 stevensc 73
    },
74
    {
75
      label: 'Cuando comentan una de mis publicaciones',
4360 stevensc 76
      input_name: "comment_my_feed"
4359 stevensc 77
    },
78
    {
79
      label: 'Cuando comparten una de mis publicaciones',
4360 stevensc 80
      input_name: "share_my_feed"
4359 stevensc 81
    },
82
    {
83
      label: 'Cuando recibo un mensaje',
4360 stevensc 84
      input_name: "receive_inmail"
4359 stevensc 85
    },
86
    {
87
      label: 'Cuando recibo una solicitud para unirse a mi grupo',
88
      name: "receive_request_join_my_group"
89
    }
90
  ]
91
 
1 www 92
  return (
4359 stevensc 93
    <div className="acc-setting">
4363 stevensc 94
      <h3>Notificaciónes de Correo Electrónico</h3>
4359 stevensc 95
      {loading
96
        ? <Spinner />
97
        :
98
        <form onSubmit={handleSubmit(handleOnSubmit)}>
4363 stevensc 99
          {NOTIFICATION_OPTIONS.map((option, index) => {
4359 stevensc 100
            return (
101
              <div className="notbat" key={index}>
102
                <span>
103
                  {option.label}
104
                </span>
105
                <SwitchInput register={register} name={option.input_name} />
106
              </div>
107
            )
108
          })}
109
          <button type="submit" className="btn btn-primary">
110
            Guardar
111
          </button>
112
        </form>
113
      }
114
    </div>
1 www 115
  );
116
};
117
 
118
const mapDispatchToProps = {
119
  addNotification: (notification) => addNotification(notification),
120
};
121
 
122
export default connect(null, mapDispatchToProps)(Notifications);