Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useState } from 'react';
2
import { connect } from 'react-redux';
3
 
4
import { axios } from '@utils/index.js';
5
import { addNotification } from '@store/notification/notification.actions';
6
 
7
import Widget from '@components/UI/Widget';
8
import Spinner from '@components/UI/Spinner';
9
import SwitchInput from '@components/UI/SwitchInput';
10
 
11
const NOTIFICATION_OPTIONS = [
12
  {
13
    value: false,
14
    label: 'Cuando recibo una solicitud de conexión',
15
    input_name: 'receive_connection_request'
16
  },
17
  {
18
    value: false,
19
    label: 'Cuando recibo una solicitud para unirme a un grupo',
20
    input_name: 'receive_invitation_group'
21
  },
22
 
23
  {
24
    value: false,
25
    label: 'Cuando aceptan mi solicitud para unirme a un grupo',
26
    input_name: 'accept_my_request_join_group'
27
  },
28
  {
29
    value: false,
30
    label: 'Cuando recibo una solicitud para unirme a una empresa',
31
    input_name: 'receive_invitation_company'
32
  },
33
  {
34
    value: false,
35
    label: 'Cuando le dan me gusta a una de mis publicaciones',
36
    input_name: 'like_my_feed'
37
  },
38
  {
39
    value: false,
40
    label: 'Cuando comentan una de mis publicaciones',
41
    input_name: 'comment_my_feed'
42
  },
43
  {
44
    value: false,
45
    label: 'Cuando comparten una de mis publicaciones',
46
    input_name: 'share_my_feed'
47
  },
48
  {
49
    value: false,
50
    label: 'Cuando recibo un mensaje',
51
    input_name: 'receive_inmail'
52
  },
53
  {
54
    value: false,
55
    label: 'Cuando recibo una solicitud para unirse a mi grupo',
56
    input_name: 'receive_request_join_my_group'
57
  }
58
];
59
 
60
const Notifications = ({ addNotification }) => {
61
  const [notifications, setNotifications] = useState(NOTIFICATION_OPTIONS);
62
  const [loading, setLoading] = useState(false);
63
 
64
  const handleOnSubmit = (e) => {
65
    e.preventDefault();
66
    setLoading(true);
67
    const formData = new FormData();
68
 
69
    notifications.map(
70
      ({ input_name, value }) => value && formData.append(input_name, value === 'y' ? value : 'n')
71
    );
72
 
73
    axios
74
      .post('/account-settings/notification', formData)
75
      .then((response) => {
76
        const { success, data } = response.data;
77
        if (!success) {
78
          const errorMessage =
79
            typeof data === 'string' ? data : 'Ha ocurrido un error, Por favor intente mas tarde';
80
          return addNotification({ style: 'danger', msg: errorMessage });
81
        }
82
 
83
        return addNotification({ style: 'success', msg: data });
84
      })
85
      .finally(() => setLoading(false));
86
  };
87
 
88
  const handleChecked = (value, element) => {
89
    setNotifications((prevNotifications) =>
90
      prevNotifications.map((notification) =>
91
        notification.input_name === element
92
          ? { ...notification, value: Boolean(value) }
93
          : notification
94
      )
95
    );
96
  };
97
 
98
  useEffect(() => {
99
    setLoading(true);
100
    axios
101
      .get('/account-settings/notification')
102
      .then((response) => {
103
        const { success, data } = response.data;
104
        if (success) {
105
          Object.entries(data).map(([key, value]) =>
106
            setNotifications((prevNotifications) =>
107
              prevNotifications.map((notification) =>
108
                notification.input_name === key
109
                  ? { ...notification, value: Boolean(value) }
110
                  : notification
111
              )
112
            )
113
          );
114
        }
115
      })
116
      .finally(() => setLoading(false));
117
  }, []);
118
 
119
  if (loading) {
120
    return <Spinner />;
121
  }
122
 
123
  return (
124
    <Widget>
125
      <Widget.Header title='Notificaciónes de Correo Electrónico' />
126
 
127
      <Widget.Body>
128
        <form onSubmit={handleOnSubmit}>
129
          {notifications.map((option, index) => {
130
            return (
131
              <div className='notbat' key={index}>
132
                <span>{option.label}</span>
133
                <SwitchInput
134
                  isChecked={option.value}
135
                  setValue={(value) => handleChecked(value, option.input_name)}
136
                />
137
              </div>
138
            );
139
          })}
140
 
141
          <button type='submit' className='btn btn-primary mt-3'>
142
            Guardar
143
          </button>
144
        </form>
145
      </Widget.Body>
146
    </Widget>
147
  );
148
};
149
 
150
const mapDispatchToProps = {
151
  addNotification: (notification) => addNotification(notification)
152
};
153
 
154
export default connect(null, mapDispatchToProps)(Notifications);