diff --git a/src/hooks/useTicket.tsx b/src/hooks/useTicket.tsx index 800b7aa..a1e3b29 100644 --- a/src/hooks/useTicket.tsx +++ b/src/hooks/useTicket.tsx @@ -1,5 +1,6 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; +import { useReactToPrint } from 'react-to-print'; import { format } from 'date-fns'; import { ClienteMetadata } from 'types/IContext'; @@ -15,9 +16,23 @@ export function useTicket() { const [cliente, setCliente] = useState(); const [loading, setLoading] = useState(true); + const [showPrint, setShowPrint] = useState(false); + + const componentToPrintRef = useRef(null); const dayFormatted = format(selectDay, 'yyyy-MM-dd'); + const handlePrint = + useReactToPrint({ + content: () => componentToPrintRef.current, + onAfterPrint: () => setShowPrint(false), + }) || (() => {}); + + function handleClickPrint() { + setShowPrint(!showPrint); + handlePrint(); + } + async function buscaCliente() { setLoading(true); const { data, error, status } = await getHorarioSelecionado(params?.id || '', dayFormatted, selectHours); @@ -56,5 +71,8 @@ export function useTicket() { return { cliente, loading, + componentToPrintRef, + handleClickPrint, + showPrint, }; }