Skip to content

Commit

Permalink
feat: add new state ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 8, 2022
1 parent 6c1d7dd commit f2cdf72
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/contexts/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function UserProvider({ children }: any) {
const [horariosAgendados, setHorariosAgendados] = useState<ClienteMetadata[]>([]);
const [selectDay, setSelectDay] = useState(new Date());
const [selectHours, setSelectHours] = useState<string>('');
const [ticket, setTicket] = useState<ClienteMetadata>();
const [status, setStatus] = useState<string>('');

const clientId = user?.id;
Expand Down Expand Up @@ -169,14 +170,14 @@ export function UserProvider({ children }: any) {
if (error) {
switch (status) {
default:
throw new Error('Erro ao buscar barbeiros');
return;
}
}

if (!data) return;

if (data[0].j === null) {
throw new Error('Erro ao buscar barbeiros');
return;
}

setBarbeiros(data[0].j);
Expand Down Expand Up @@ -259,7 +260,7 @@ export function UserProvider({ children }: any) {
setStatus('error');
switch (status) {
default:
throw new Error('Erro ao agendar');
return;
}
}

Expand Down Expand Up @@ -352,6 +353,8 @@ export function UserProvider({ children }: any) {
generateGoogleCalendarEvent,
startDate,
endDate,
ticket,
setTicket,
}}
>
{children}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Cliente/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import styles from './Cliente.module.scss';
export function Cliente() {
const navigate = useNavigate();
const { user } = useAuth();
const { setBarbeiro, barbeiros, horariosAgendados, buscarAgendamentosData, setSelectHours } = useUser();
const { setBarbeiro, barbeiros, horariosAgendados, buscarAgendamentosData, setSelectHours, setTicket } = useUser();

const [selectDay, setSelectDay] = useState(new Date());

Expand Down Expand Up @@ -69,6 +69,7 @@ export function Cliente() {
key={horario.id}
cliente={horario}
onClick={() => {
setTicket(horario);
setSelectHours(horario.hour);
navigate(`ticket/${horario.client_id}`);
}}
Expand Down
15 changes: 9 additions & 6 deletions src/pages/MyTicket/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';

import { format } from 'date-fns';
Expand All @@ -17,31 +17,34 @@ import styles from './MyTicket.module.scss';
export function MyTicket() {
const navigate = useNavigate();
const params = useParams();
const { selectHours, setSelectHours } = useUser();
const { selectHours, setSelectHours, ticket } = useUser();
const { theme } = useTheme();

const [cliente, setCliente] = useState<ClienteMetadata>();
const atualDayFormatted = format(new Date(), 'yyyy-MM-dd');

const dateFormatted = format(new Date(String(ticket?.appointment_date)), 'yyyy-MM-dd');

useEffect(() => {
setSelectHours('');
}, [params.id]);

async function buscaCliente() {
const { data, error, status } = await getHorarioSelecionado(params?.id || '', atualDayFormatted, selectHours);
const { data, error, status } = await getHorarioSelecionado(params?.id || '', dateFormatted, selectHours);

if (error) {
navigate('/');
switch (status) {
default:
throw new Error('Erro ao buscar barbeiros');
return;
}
}

if (!data) return;
if (!data[0].j) return;
if (!data[0].j[0]) return;

if (data[0].j === null) {
throw new Error('Erro ao buscar barbeiros');
return;
}

setCliente(data[0].j[0]);
Expand Down

0 comments on commit f2cdf72

Please sign in to comment.