testes/src_2/features/rh/hooks/usePagamento.js

65 lines
1.9 KiB
JavaScript

import { useState, useCallback } from 'react';
import { rhService } from '../services/rhService';
import { toast } from 'sonner';
/**
* Hook customizado para apresentação de pagamento
*/
export const usePagamento = () => {
const [pagamento, setPagamento] = useState(null);
const [apresentacaoGeral, setApresentacaoGeral] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
/**
* Buscar apresentação de pagamento por colaborador
*/
const fetchPagamentoByColaborador = useCallback(async (idcolaborador, params = {}) => {
setLoading(true);
setError(null);
try {
const response = await rhService.getPagamentoByColaborador(idcolaborador, params);
const pagamentoData = response?.data || response;
setPagamento(pagamentoData);
return pagamentoData;
} catch (err) {
const errorMessage = err?.response?.data?.message || err?.message || 'Erro ao buscar apresentação de pagamento';
setError(errorMessage);
toast.error(errorMessage);
return null;
} finally {
setLoading(false);
}
}, []);
/**
* Buscar apresentação geral de pagamento
*/
const fetchApresentacaoGeral = useCallback(async (params = {}) => {
setLoading(true);
setError(null);
try {
const response = await rhService.getPagamentoApresentacao(params);
const colaboradores = response?.colaboradores || response?.data || [];
setApresentacaoGeral(colaboradores);
return response;
} catch (err) {
const errorMessage = err?.response?.data?.message || err?.message || 'Erro ao buscar apresentação geral';
setError(errorMessage);
toast.error(errorMessage);
return null;
} finally {
setLoading(false);
}
}, []);
return {
pagamento,
apresentacaoGeral,
loading,
error,
fetchPagamentoByColaborador,
fetchApresentacaoGeral
};
};