Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3736 | | Comparar con el anterior | 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
 
3741 stevensc 5
const NOTIFICATION_OPTIONS = {
6
  accept_my_request_connection: {
3736 stevensc 7
    value: false,
3741 stevensc 8
    label: 'Cuando acepto una solicitud de conexión',
9
    input_name: 'accept_my_request_connection'
10
  },
11
  accept_my_request_join_group: {
12
    value: false,
13
    label: 'Cuando acepto una solicitud para unirme a un grupo',
14
    input_name: 'accept_my_request_join_group'
15
  },
16
  comment_my_feed: {
17
    value: false,
18
    label: 'Cuando comentan una de mis publicaciones',
19
    input_name: 'comment_my_feed'
20
  },
21
  like_my_feed: {
22
    value: false,
23
    label: 'Cuando le dan me gusta a una de mis publicaciones',
24
    input_name: 'like_my_feed'
25
  },
26
  receive_connection_request: {
27
    value: false,
3736 stevensc 28
    label: 'Cuando recibo una solicitud de conexión',
29
    input_name: 'receive_connection_request'
30
  },
3741 stevensc 31
  receive_inmail: {
3736 stevensc 32
    value: false,
3741 stevensc 33
    label: 'Cuando recibo un mensaje',
34
    input_name: 'receive_inmail'
3736 stevensc 35
  },
3741 stevensc 36
  receive_invitation_company: {
3736 stevensc 37
    value: false,
3741 stevensc 38
    label: 'Cuando recibo una invitación para unirme a una empresa',
39
    input_name: 'receive_invitation_company'
3736 stevensc 40
  },
3741 stevensc 41
  receive_invitation_group: {
3736 stevensc 42
    value: false,
3741 stevensc 43
    label: 'Cuando recibo una invitación para unirme a un grupo',
44
    input_name: 'receive_invitation_group'
3736 stevensc 45
  },
3741 stevensc 46
  receive_invitation_meeting: {
3736 stevensc 47
    value: false,
3741 stevensc 48
    label: 'Cuando recibo una invitación para una reunión',
49
    input_name: 'receive_invitation_meeting'
3736 stevensc 50
  },
3741 stevensc 51
  receive_records_available_meeting: {
3736 stevensc 52
    value: false,
3741 stevensc 53
    label: 'Cuando recibo un registro disponible para una reunión',
54
    input_name: 'receive_records_available_meeting'
3736 stevensc 55
  },
3741 stevensc 56
  receive_reminder_meeting: {
3736 stevensc 57
    value: false,
3741 stevensc 58
    label: 'Cuando recibo un recordatorio de una reunión',
59
    input_name: 'receive_reminder_meeting'
3736 stevensc 60
  },
3741 stevensc 61
  receive_request_join_my_group: {
3736 stevensc 62
    value: false,
3741 stevensc 63
    label: 'Cuando recibo una solicitud para unirme a un grupo',
64
    input_name: 'receive_request_join_my_group'
3736 stevensc 65
  },
3741 stevensc 66
  share_my_feed: {
3736 stevensc 67
    value: false,
3741 stevensc 68
    label: 'Cuando comparten una de mis publicaciones',
69
    input_name: 'share_my_feed'
3736 stevensc 70
  }
3741 stevensc 71
};
3736 stevensc 72
 
73
export function useNotificationSettings() {
74
  const { showSuccess, showError } = useAlert();
75
  const [notifications, setNotifications] = useState(NOTIFICATION_OPTIONS);
76
 
77
  const { loading } = useApi(getNotificationSettings, {
78
    autoFetch: true,
79
    onSuccess: (data) => {
3741 stevensc 80
      const prevNotifications = structuredClone(NOTIFICATION_OPTIONS);
81
      Object.keys(data).forEach((key) => {
82
        prevNotifications[key].value = data[key] === 1;
83
      });
84
 
85
      console.log({ prevNotifications });
86
      setNotifications(prevNotifications);
3736 stevensc 87
    },
88
    onError: (error) => {
89
      showError(error.message || 'Error al cargar las configuraciones');
90
    }
91
  });
92
 
93
  const { execute: executeUpdate } = useApi(updateNotificationSettings, {
94
    onSuccess: (data) => {
95
      showSuccess(data);
96
    },
97
    onError: (error) => {
98
      showError(error.message || 'Error al actualizar las notificaciones');
99
    }
100
  });
101
 
102
  const handleChecked = (value, inputName) => {
3741 stevensc 103
    setNotifications((prevNotifications) => ({
104
      ...prevNotifications,
105
      [inputName]: Boolean(value)
106
    }));
3736 stevensc 107
  };
108
 
109
  const updateSettings = async () => {
110
    const settingsData = {};
3741 stevensc 111
    Object.keys(notifications).forEach((key) => {
112
      settingsData[key] = notifications[key] ? 1 : 0;
3736 stevensc 113
    });
114
    await executeUpdate(settingsData);
115
  };
116
 
117
  return {
118
    notifications,
119
    loading,
120
    handleChecked,
121
    updateSettings
122
  };
123
}