Skip to content

Commit

Permalink
feat: add hook print
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 8, 2022
1 parent 912f237 commit 91ff8a5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/hooks/useTicket.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,9 +16,23 @@ export function useTicket() {

const [cliente, setCliente] = useState<ClienteMetadata>();
const [loading, setLoading] = useState(true);
const [showPrint, setShowPrint] = useState(false);

const componentToPrintRef = useRef<HTMLDivElement>(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);
Expand Down Expand Up @@ -56,5 +71,8 @@ export function useTicket() {
return {
cliente,
loading,
componentToPrintRef,
handleClickPrint,
showPrint,
};
}

0 comments on commit 91ff8a5

Please sign in to comment.