Rev 3685 | 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);
const addGroup = () => {
showModal('Agregar grupo', <AddGroupForm onSubmit={closeModal} />);
};
const editGroup = (url) => {
navigate(url);
};
const deleteGroup = (url) => {
showAlert({
title: '¿Estás seguro de querer eliminar este grupo?',
onConfirm: () => {
executeDeleteGroup(url)
.then((message) => {
showSuccess(message);
closeAlert();
execute();
})
.catch((error) => {
showError(error.message);
});
},
onCancel: closeAlert
});
};
const viewGroup = (url) => {
navigate(url);
};
const searchGroups = debounce((e) => {
execute({ search: e.target.value });
}, 500);
return { groups, loading, addGroup, editGroup, deleteGroup, viewGroup, searchGroups };
};