Skip to content

Commit

Permalink
feat: button cancelamento agendado
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 12, 2022
1 parent f8f4397 commit 9ea097a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/hooks/useTicket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { useReactToPrint } from 'react-to-print';
import { format } from 'date-fns';
import { ClienteMetadata } from 'types/IContext';

import { useToast } from 'contexts/Toast';
import { useUser } from 'contexts/User';

import { deleteSchedule } from 'services/delete/schedule';
import { getHorarioSelecionado } from 'services/get/horarioMarcado';

export function useTicket() {
const navigate = useNavigate();
const params = useParams();
const { toast } = useToast();
const { selectHours, selectDay, setSelectHours, setSelectDay } = useUser();

const [cliente, setCliente] = useState<ClienteMetadata>();
Expand All @@ -33,6 +36,37 @@ export function useTicket() {
handlePrint();
}

async function cancelarAgendamento(idAgendamento: string) {
const { data, error } = await deleteSchedule(idAgendamento);

if (error) {
toast.error(error.message, { id: 'toast' });
return;
}

if (data) {
toast.success('Agendamento cancelado com sucesso!', { id: 'toast' });
navigate('/');
}
}

function verificaHorarioCancelamento(dataAgendamento: ClienteMetadata) {
const dataAtual = new Date();
const dataAtualFormatted = format(dataAtual, 'yyyy-MM-dd');
const horaAtual = format(dataAtual, 'HH:mm:ss');
const dataHoraAtual = `${dataAtualFormatted}T${horaAtual}`;
const dataAgenda = dataAgendamento?.appointment_date;

const diff = new Date(dataAgenda).getTime() - new Date(dataHoraAtual).getTime();
const diffMinutes = Math.round(diff / 1000 / 60);

if (diffMinutes < 60) {
return true;
}

return false;
}

async function buscaCliente() {
setLoading(true);
const { data, error, status } = await getHorarioSelecionado(params?.id || '', dayFormatted, selectHours);
Expand Down Expand Up @@ -74,5 +108,7 @@ export function useTicket() {
componentToPrintRef,
handleClickPrint,
showPrint,
cancelarAgendamento,
verificaHorarioCancelamento,
};
}
11 changes: 10 additions & 1 deletion src/pages/MyTicket/MyTicket.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@

.containerButton {
display: flex;
justify-content: center;
justify-content: space-around;
margin-top: 1rem;
margin-bottom: 2rem;
flex-direction: column;
height: 20vh;
}

.containerAlert {
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}

@media screen and (min-width: 839px) {
Expand Down
23 changes: 22 additions & 1 deletion src/pages/MyTicket/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SpinnerCircular } from 'spinners-react';
import { ClienteMetadata } from 'types/IContext';

import { Alert } from 'components/Alert';
import { Button } from 'components/Button';
import ComponentToPrint from 'components/ComponentToPrint';
import { Header } from 'components/Header';
Expand All @@ -13,14 +15,20 @@ import styles from './MyTicket.module.scss';

export function MyTicket() {
const { theme } = useTheme();
const { loading, cliente, componentToPrintRef, handleClickPrint } = useTicket();
const { loading, cliente, componentToPrintRef, handleClickPrint, cancelarAgendamento, verificaHorarioCancelamento } =
useTicket();

return (
<>
<div className={styles.home} data-theme={theme}>
<Header back />

<div className={styles.container}>
{verificaHorarioCancelamento(cliente as ClienteMetadata) && (
<div className={styles.containerAlert}>
<Alert title="Você só cancelar o agendamento 1 hora antes do seu horário" warning />
</div>
)}
<h2>Apresente esse ticket para o seu barbeiro</h2>
{loading ? (
<SpinnerCircular color="#ff9000" size={64} />
Expand All @@ -40,6 +48,19 @@ export function MyTicket() {
>
Salvar ticket
</Button>
<Button
type="button"
disabled={verificaHorarioCancelamento(cliente as ClienteMetadata)}
style={{
backgroundColor: '#CA0B00',
color: '#FFF',
}}
onClick={() => {
cancelarAgendamento(cliente?.id || '');
}}
>
Cancelar agendamento
</Button>
</div>
</div>
</div>
Expand Down

0 comments on commit 9ea097a

Please sign in to comment.