132 lines
3.6 KiB
TypeScript
132 lines
3.6 KiB
TypeScript
import { useState, useCallback } from 'react';
|
|
import { Models } from 'appwrite';
|
|
import { pagamentoService, PagamentoData } from '@/services/pagamentoService';
|
|
export type { PagamentoData };
|
|
|
|
export interface UsePagamentosReturn {
|
|
pagamentos: Models.Document[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
totalPagamentos: number;
|
|
currentPage: number;
|
|
listarPagamentos: (page?: number, search?: string) => Promise<void>;
|
|
buscarPagamentos: (termo: string, page?: number) => Promise<void>;
|
|
cadastrarPagamento: (data: PagamentoData) => Promise<boolean>;
|
|
atualizarPagamento: (id: string, data: PagamentoData) => Promise<boolean>;
|
|
deletarPagamento: (id: string) => Promise<boolean>;
|
|
setCurrentPage: (page: number) => void;
|
|
}
|
|
|
|
const PAGE_SIZE = 10;
|
|
|
|
export const usePagamentos = (): UsePagamentosReturn => {
|
|
const [pagamentos, setPagamentos] = useState<Models.Document[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [totalPagamentos, setTotalPagamentos] = useState(0);
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
const listarPagamentos = useCallback(
|
|
async (page = currentPage, search = '') => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await pagamentoService.listar(page, PAGE_SIZE, search);
|
|
|
|
setPagamentos(response.documents || []);
|
|
setTotalPagamentos(response.total || 0);
|
|
setCurrentPage(page);
|
|
} catch (err) {
|
|
setError('Erro de conexão ao carregar pagamentos');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage]);
|
|
|
|
const buscarPagamentos = useCallback(
|
|
async (termo: string, page = 1) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await pagamentoService.buscarPorNome(
|
|
termo,
|
|
page,
|
|
PAGE_SIZE
|
|
);
|
|
|
|
setPagamentos(response.documents || []);
|
|
setTotalPagamentos(response.total || 0);
|
|
setCurrentPage(page);
|
|
} catch (err) {
|
|
setError('Erro de conexão ao buscar pagamentos');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
},
|
|
[]
|
|
);
|
|
|
|
const cadastrarPagamento = useCallback(async (formData: PagamentoData): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
await pagamentoService.criar(formData);
|
|
await listarPagamentos(currentPage);
|
|
return true;
|
|
} catch (err) {
|
|
setError('Erro de conexão ao cadastrar pagamento');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarPagamentos]);
|
|
|
|
const atualizarPagamento = useCallback(async (id: string, formData: PagamentoData): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
await pagamentoService.atualizar(id, formData);
|
|
await listarPagamentos(currentPage);
|
|
return true;
|
|
} catch (err) {
|
|
setError('Erro de conexão ao atualizar pagamento');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarPagamentos]);
|
|
|
|
const deletarPagamento = useCallback(async (id: string): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
await pagamentoService.deletar(id);
|
|
await listarPagamentos(currentPage);
|
|
return true;
|
|
} catch (err) {
|
|
setError('Erro de conexão ao deletar pagamento');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarPagamentos]);
|
|
|
|
return {
|
|
pagamentos,
|
|
loading,
|
|
error,
|
|
totalPagamentos,
|
|
currentPage,
|
|
listarPagamentos,
|
|
buscarPagamentos,
|
|
cadastrarPagamento,
|
|
atualizarPagamento,
|
|
deletarPagamento,
|
|
setCurrentPage,
|
|
};
|
|
};
|