Proyectos de Subversion LeadersLinked - SPA

Rev

Ir a la última revisión | 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 = [
  {
    value: false,
    label: 'Cuando recibo una solicitud de conexión',
    input_name: 'receive_connection_request'
  },
  {
    value: false,
    label: 'Cuando recibo una solicitud para unirme a un grupo',
    input_name: 'receive_invitation_group'
  },
  {
    value: false,
    label: 'Cuando aceptan mi solicitud para unirme a un grupo',
    input_name: 'accept_my_request_join_group'
  },
  {
    value: false,
    label: 'Cuando recibo una solicitud para unirme a una empresa',
    input_name: 'receive_invitation_company'
  },
  {
    value: false,
    label: 'Cuando le dan me gusta a una de mis publicaciones',
    input_name: 'like_my_feed'
  },
  {
    value: false,
    label: 'Cuando comentan una de mis publicaciones',
    input_name: 'comment_my_feed'
  },
  {
    value: false,
    label: 'Cuando comparten una de mis publicaciones',
    input_name: 'share_my_feed'
  },
  {
    value: false,
    label: 'Cuando recibo un mensaje',
    input_name: 'receive_inmail'
  },
  {
    value: false,
    label: 'Cuando recibo una solicitud para unirse a mi grupo',
    input_name: 'receive_request_join_my_group'
  }
];

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

  const { loading } = useApi(getNotificationSettings, {
    autoFetch: true,
    onSuccess: (data) => {
      console.log(data);
    },
    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.map((notification) =>
        notification.input_name === inputName
          ? { ...notification, value: Boolean(value) }
          : notification
      )
    );
  };

  const updateSettings = async () => {
    const settingsData = {};
    notifications.forEach(({ input_name, value }) => {
      settingsData[input_name] = value;
    });
    await executeUpdate(settingsData);
  };

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