import { useState, useCallback, useRef, useEffect } from 'react'; import { Models } from 'appwrite'; import { pedidoService, PedidoData } from '@/services/pedidoService'; export type { PedidoData }; export const PAGE_SIZE = 10; export const usePedidos = () => { const [pedidos, setPedidos] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [totalPedidos, setTotalPedidos] = useState(0); const [currentPage, setCurrentPage] = useState(1); const [isChangingPage, setIsChangingPage] = useState(false); const [isCreating, setIsCreating] = useState(false); const [searchTerm, setSearchTerm] = useState(''); const isInitialMount = useRef(true); const listarPedidos = useCallback( async (page = currentPage, status = '', search = searchTerm) => { setIsChangingPage(true); setLoading(true); setError(null); try { const response = await pedidoService.listar(page, PAGE_SIZE, status, search); if (response.success) { setPedidos((response.documents as unknown as Models.Document[]) || []); setTotalPedidos(response.total || 0); setCurrentPage(page); setSearchTerm(search); } else { setError(response.error || 'Erro ao carregar pedidos'); } } catch (err) { setError('Erro de conexão ao carregar pedidos'); } finally { setLoading(false); setIsChangingPage(false); } }, [currentPage, searchTerm] ); const cadastrarPedido = useCallback( async (formData: PedidoData): Promise => { setIsCreating(true); setError(null); try { const response = await pedidoService.criar(formData); if (response.success) { await listarPedidos(1, '', searchTerm); return true; } else { setError(response.error || 'Erro ao cadastrar pedido'); return false; } } catch (err) { setError('Erro de conexão ao cadastrar pedido'); return false; } finally { setIsCreating(false); } }, [listarPedidos, searchTerm] ); const atualizarPedido = useCallback( async (id: string, formData: PedidoData): Promise => { setLoading(true); setError(null); try { const response = await pedidoService.atualizar(id, formData); if (response.success) { await listarPedidos(currentPage, '', searchTerm); return true; } else { setError(response.error || 'Erro ao atualizar pedido'); return false; } } catch (err) { setError('Erro de conexão ao atualizar pedido'); return false; } finally { setLoading(false); } }, [currentPage, listarPedidos] ); useEffect(() => { if (isInitialMount.current) { isInitialMount.current = false; return; } listarPedidos(currentPage, '', searchTerm); }, [currentPage, searchTerm, listarPedidos]); const buscarPedidos = useCallback( async (nome: string, page = 1) => { setIsChangingPage(true); setLoading(true); setError(null); try { const response = await pedidoService.buscarPorNome( nome, page, PAGE_SIZE ); if (response.success) { setPedidos((response.documents as unknown as Models.Document[]) || []); setTotalPedidos(response.total || 0); setCurrentPage(page); setSearchTerm(nome); } else { setError(response.error || 'Erro ao buscar pedidos'); } } catch (err) { setError('Erro de conexão ao buscar pedidos'); } finally { setLoading(false); setIsChangingPage(false); } }, [] ); const deletarPedido = useCallback( async (id: string): Promise => { setLoading(true); setError(null); try { const response = await pedidoService.deletar(id); if (response.success) { await listarPedidos(currentPage, '', searchTerm); return true; } else { setError(response.error || 'Erro ao deletar pedido'); return false; } } catch (err) { setError('Erro de conexão ao deletar pedido'); return false; } finally { setLoading(false); } }, [currentPage, listarPedidos] ); return { pedidos, loading, isChangingPage, isCreating, error, totalPedidos, currentPage, listarPedidos, buscarPedidos, cadastrarPedido, atualizarPedido, deletarPedido, setCurrentPage, searchTerm, setSearchTerm, }; };