Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3685 | Rev 3688 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from 'react';
import { useNavigate } from 'react-router-dom';

import { useAlert, useAlertModal, useApi, useModal } from '@shared/hooks';
import { debounce } from '@shared/utils';
import { getMyGroups } from '@groups/services';

import { AddGroupForm } from '@groups/components';

export const useMyGroups = () => {
  const navigate = useNavigate();

  const { showModal, closeModal } = useModal();
  const { showAlert, closeAlert } = useAlertModal();
  const { showError, showSuccess } = useAlert();

  const {
    data: groups,
    loading,
    execute
  } = useApi(getMyGroups, {
    autoFetch: true
  });

  const { execute: executeDeleteGroup } = useApi(deleteGroup, {
    onSuccess: () => {
      showSuccess('Grupo eliminado correctamente');
      closeAlert();
      execute();
    },
    onError: (error) => {
      showError(error.message);
    }
  });

  const addGroup = () => {
    showModal(
      'Agregar grupo',
      <AddGroupForm
        onSubmit={() => {
          closeModal();
          execute();
        }}
      />
    );
  };

  const editGroup = (url) => {
    navigate(url);
  };

  const deleteGroup = (url) => {
    showAlert({
      title: '¿Estás seguro de querer eliminar este grupo?',
      onConfirm: () => executeDeleteGroup(url),
      onCancel: closeAlert
    });
  };

  const viewGroup = (url) => {
    navigate(url);
  };

  const searchGroups = debounce((value) => {
    execute({ search: value });
  }, 500);

  return { groups, loading, addGroup, editGroup, deleteGroup, viewGroup, searchGroups };
};