162 lines
4.3 KiB
TypeScript
162 lines
4.3 KiB
TypeScript
import { useState, useCallback } from 'react';
|
|
import { Models } from 'appwrite';
|
|
import { faturaService } from '@/services/faturaService';
|
|
|
|
export interface FaturaFormData {
|
|
nome: string;
|
|
}
|
|
|
|
export interface UseFaturasReturn {
|
|
faturas: Models.Document[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
totalFaturas: number;
|
|
currentPage: number;
|
|
listarFaturas: (page?: number) => Promise<void>;
|
|
buscarFaturas: (nome: string, page?: number) => Promise<void>;
|
|
cadastrarFatura: (data: FaturaFormData) => Promise<boolean>;
|
|
atualizarFatura: (id: string, data: FaturaFormData) => Promise<boolean>;
|
|
deletarFatura: (id: string) => Promise<boolean>;
|
|
setCurrentPage: (page: number) => void;
|
|
}
|
|
|
|
const PAGE_SIZE = 10;
|
|
|
|
export const useFaturas = (): UseFaturasReturn => {
|
|
const [faturas, setFaturas] = useState<Models.Document[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [totalFaturas, setTotalFaturas] = useState(0);
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
// Listagem usando SDK do Appwrite
|
|
const listarFaturas = useCallback(async (page = currentPage) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await faturaService.listar(page, PAGE_SIZE);
|
|
|
|
if (response.success) {
|
|
setFaturas(response.documents || []);
|
|
setTotalFaturas(response.total || 0);
|
|
setCurrentPage(page);
|
|
} else {
|
|
setError(response.error || 'Erro ao carregar faturas');
|
|
}
|
|
} catch (err) {
|
|
setError('Erro de conexão ao carregar faturas');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage]);
|
|
|
|
const buscarFaturas = useCallback(
|
|
async (nome: string, page = 1) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await faturaService.buscarPorNome(
|
|
nome,
|
|
page,
|
|
PAGE_SIZE
|
|
);
|
|
|
|
if (response.success) {
|
|
setFaturas(response.documents || []);
|
|
setTotalFaturas(response.total || 0);
|
|
setCurrentPage(page);
|
|
} else {
|
|
setError(response.error || 'Erro ao buscar faturas');
|
|
}
|
|
} catch (err) {
|
|
setError('Erro de conexão ao buscar faturas');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
},
|
|
[]
|
|
);
|
|
|
|
// Operações de criação, atualização e exclusão continuam usando API REST
|
|
const cadastrarFatura = useCallback(async (formData: FaturaFormData): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await faturaService.criar(formData);
|
|
|
|
if (response.success) {
|
|
await listarFaturas(currentPage);
|
|
return true;
|
|
} else {
|
|
setError(response.error || 'Erro ao cadastrar fatura');
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
setError('Erro de conexão ao cadastrar fatura');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarFaturas]);
|
|
|
|
const atualizarFatura = useCallback(async (id: string, formData: FaturaFormData): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await faturaService.atualizar(id, formData);
|
|
|
|
if (response.success) {
|
|
await listarFaturas(currentPage);
|
|
return true;
|
|
} else {
|
|
setError(response.error || 'Erro ao atualizar fatura');
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
setError('Erro de conexão ao atualizar fatura');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarFaturas]);
|
|
|
|
const deletarFatura = useCallback(async (id: string): Promise<boolean> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await faturaService.deletar(id);
|
|
|
|
if (response.success) {
|
|
await listarFaturas(currentPage);
|
|
return true;
|
|
} else {
|
|
setError(response.error || 'Erro ao deletar fatura');
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
setError('Erro de conexão ao deletar fatura');
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [currentPage, listarFaturas]);
|
|
|
|
return {
|
|
faturas,
|
|
loading,
|
|
error,
|
|
totalFaturas,
|
|
currentPage,
|
|
listarFaturas,
|
|
buscarFaturas,
|
|
cadastrarFatura,
|
|
atualizarFatura,
|
|
deletarFatura,
|
|
setCurrentPage,
|
|
};
|
|
};
|