Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3736 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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