Proyectos de Subversion LeadersLinked - SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3736 stevensc 1
import { useState } from 'react';
2
import { useApi, useAlert } from '@shared/hooks';
3
import { getNotificationSettings, updateNotificationSettings } from '@account-settings/services';
4
 
5
const NOTIFICATION_OPTIONS = [
6
  {
7
    value: false,
8
    label: 'Cuando recibo una solicitud de conexión',
9
    input_name: 'receive_connection_request'
10
  },
11
  {
12
    value: false,
13
    label: 'Cuando recibo una solicitud para unirme a un grupo',
14
    input_name: 'receive_invitation_group'
15
  },
16
  {
17
    value: false,
18
    label: 'Cuando aceptan mi solicitud para unirme a un grupo',
19
    input_name: 'accept_my_request_join_group'
20
  },
21
  {
22
    value: false,
23
    label: 'Cuando recibo una solicitud para unirme a una empresa',
24
    input_name: 'receive_invitation_company'
25
  },
26
  {
27
    value: false,
28
    label: 'Cuando le dan me gusta a una de mis publicaciones',
29
    input_name: 'like_my_feed'
30
  },
31
  {
32
    value: false,
33
    label: 'Cuando comentan una de mis publicaciones',
34
    input_name: 'comment_my_feed'
35
  },
36
  {
37
    value: false,
38
    label: 'Cuando comparten una de mis publicaciones',
39
    input_name: 'share_my_feed'
40
  },
41
  {
42
    value: false,
43
    label: 'Cuando recibo un mensaje',
44
    input_name: 'receive_inmail'
45
  },
46
  {
47
    value: false,
48
    label: 'Cuando recibo una solicitud para unirse a mi grupo',
49
    input_name: 'receive_request_join_my_group'
50
  }
51
];
52
 
53
export function useNotificationSettings() {
54
  const { showSuccess, showError } = useAlert();
55
  const [notifications, setNotifications] = useState(NOTIFICATION_OPTIONS);
56
 
57
  const { loading } = useApi(getNotificationSettings, {
58
    autoFetch: true,
59
    onSuccess: (data) => {
60
      console.log(data);
61
    },
62
    onError: (error) => {
63
      showError(error.message || 'Error al cargar las configuraciones');
64
    }
65
  });
66
 
67
  const { execute: executeUpdate } = useApi(updateNotificationSettings, {
68
    onSuccess: (data) => {
69
      showSuccess(data);
70
    },
71
    onError: (error) => {
72
      showError(error.message || 'Error al actualizar las notificaciones');
73
    }
74
  });
75
 
76
  const handleChecked = (value, inputName) => {
77
    setNotifications((prevNotifications) =>
78
      prevNotifications.map((notification) =>
79
        notification.input_name === inputName
80
          ? { ...notification, value: Boolean(value) }
81
          : notification
82
      )
83
    );
84
  };
85
 
86
  const updateSettings = async () => {
87
    const settingsData = {};
88
    notifications.forEach(({ input_name, value }) => {
89
      settingsData[input_name] = value;
90
    });
91
    await executeUpdate(settingsData);
92
  };
93
 
94
  return {
95
    notifications,
96
    loading,
97
    handleChecked,
98
    updateSettings
99
  };
100
}