Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3736 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { useState } from 'react';
import { useApi, useAlert } from '@shared/hooks';
import { getNotificationSettings, updateNotificationSettings } from '@account-settings/services';

const NOTIFICATION_OPTIONS = {
  accept_my_request_connection: {
    value: false,
    label: 'Cuando acepto una solicitud de conexión',
    input_name: 'accept_my_request_connection'
  },
  accept_my_request_join_group: {
    value: false,
    label: 'Cuando acepto una solicitud para unirme a un grupo',
    input_name: 'accept_my_request_join_group'
  },
  comment_my_feed: {
    value: false,
    label: 'Cuando comentan una de mis publicaciones',
    input_name: 'comment_my_feed'
  },
  like_my_feed: {
    value: false,
    label: 'Cuando le dan me gusta a una de mis publicaciones',
    input_name: 'like_my_feed'
  },
  receive_connection_request: {
    value: false,
    label: 'Cuando recibo una solicitud de conexión',
    input_name: 'receive_connection_request'
  },
  receive_inmail: {
    value: false,
    label: 'Cuando recibo un mensaje',
    input_name: 'receive_inmail'
  },
  receive_invitation_company: {
    value: false,
    label: 'Cuando recibo una invitación para unirme a una empresa',
    input_name: 'receive_invitation_company'
  },
  receive_invitation_group: {
    value: false,
    label: 'Cuando recibo una invitación para unirme a un grupo',
    input_name: 'receive_invitation_group'
  },
  receive_invitation_meeting: {
    value: false,
    label: 'Cuando recibo una invitación para una reunión',
    input_name: 'receive_invitation_meeting'
  },
  receive_records_available_meeting: {
    value: false,
    label: 'Cuando recibo un registro disponible para una reunión',
    input_name: 'receive_records_available_meeting'
  },
  receive_reminder_meeting: {
    value: false,
    label: 'Cuando recibo un recordatorio de una reunión',
    input_name: 'receive_reminder_meeting'
  },
  receive_request_join_my_group: {
    value: false,
    label: 'Cuando recibo una solicitud para unirme a un grupo',
    input_name: 'receive_request_join_my_group'
  },
  share_my_feed: {
    value: false,
    label: 'Cuando comparten una de mis publicaciones',
    input_name: 'share_my_feed'
  }
};

export function useNotificationSettings() {
  const { showSuccess, showError } = useAlert();
  const [notifications, setNotifications] = useState(NOTIFICATION_OPTIONS);

  const { loading } = useApi(getNotificationSettings, {
    autoFetch: true,
    onSuccess: (data) => {
      const prevNotifications = structuredClone(NOTIFICATION_OPTIONS);
      Object.keys(data).forEach((key) => {
        prevNotifications[key].value = data[key] === 1;
      });

      console.log({ prevNotifications });
      setNotifications(prevNotifications);
    },
    onError: (error) => {
      showError(error.message || 'Error al cargar las configuraciones');
    }
  });

  const { execute: executeUpdate } = useApi(updateNotificationSettings, {
    onSuccess: (data) => {
      showSuccess(data);
    },
    onError: (error) => {
      showError(error.message || 'Error al actualizar las notificaciones');
    }
  });

  const handleChecked = (value, inputName) => {
    setNotifications((prevNotifications) => ({
      ...prevNotifications,
      [inputName]: Boolean(value)
    }));
  };

  const updateSettings = async () => {
    const settingsData = {};
    Object.keys(notifications).forEach((key) => {
      settingsData[key] = notifications[key] ? 1 : 0;
    });
    await executeUpdate(settingsData);
  };

  return {
    notifications,
    loading,
    handleChecked,
    updateSettings
  };
}