Skip to content

Commit

Permalink
feat: add horarios dos barbeiros e hook schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 12, 2022
1 parent 1aa9efc commit d97d01a
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 3 deletions.
106 changes: 106 additions & 0 deletions src/hooks/useSchedule.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { useState } from 'react';

import { Schedule } from 'types/IContext';

import { horariosManha, horariosNoite, horariosTarde } from 'utils/horarios';

import { useUser } from 'contexts/User';

export function useSchedule() {
const { barbeiro } = useUser();
const weekDay = new Date().getDay();

const schedules = JSON.parse(barbeiro?.schedules || '[]');

if (!barbeiro) window.location.assign('/');

const [horarioInicialBarbeiroSchedule, setHorarioInicialBarbeiroSchedule] = useState(
JSON.parse(barbeiro?.schedules)[weekDay]?.from || '',
);
const [horarioFinalBarbeiroSchedule, setHorarioFinalBarbeiroSchedule] = useState(
JSON.parse(barbeiro?.schedules)[weekDay]?.to || '',
);

function getHorarioAtual(week_day: string) {
const horario = [] as string[];
schedules?.map((schedule: Schedule) => {
if (schedule.week_day === week_day) {
horario.push(schedule.from + ' às ' + schedule.to);
}
});
return horario;
}

function salvarWeekDays(schedules: Schedule[]) {
const weekDays = [] as number[];
schedules?.map((schedule) => {
weekDays.push(Number(schedule.week_day));
});
return weekDays;
}

const diasDaSemanaBarbeiro = salvarWeekDays(JSON.parse(barbeiro?.schedules));

function verificarNumerosFaltantes(weekDays: number[]) {
const numerosFaltantes = [] as number[];
for (let i = 0; i < 7; i++) {
if (!weekDays.includes(i)) {
numerosFaltantes.push(i);
}
}
return numerosFaltantes;
}

const numerosFaltantes = verificarNumerosFaltantes(diasDaSemanaBarbeiro);

function desabilitarHorariosAnteriores(horarioInicialBarbeiroSchedule: string) {
const horariosAnteriores = [] as string[];
horariosManha.map((horario) => {
if (horario < horarioInicialBarbeiroSchedule) {
horariosAnteriores.push(horario);
}
});
horariosTarde.map((horario) => {
if (horario < horarioInicialBarbeiroSchedule) {
horariosAnteriores.push(horario);
}
});
horariosNoite.map((horario) => {
if (horario < horarioInicialBarbeiroSchedule) {
horariosAnteriores.push(horario);
}
});
return horariosAnteriores;
}

function desabilitarHorariosPosteriores(horarioFinalBarbeiroSchedule: string) {
const horariosPosteriores = [] as string[];
horariosManha.map((horario) => {
if (horario > horarioFinalBarbeiroSchedule) {
horariosPosteriores.push(horario);
}
});
horariosTarde.map((horario) => {
if (horario > horarioFinalBarbeiroSchedule) {
horariosPosteriores.push(horario);
}
});
horariosNoite.map((horario) => {
if (horario > horarioFinalBarbeiroSchedule) {
horariosPosteriores.push(horario);
}
});
return horariosPosteriores;
}

return {
setHorarioInicialBarbeiroSchedule,
setHorarioFinalBarbeiroSchedule,
getHorarioAtual,
numerosFaltantes,
desabilitarHorariosAnteriores,
desabilitarHorariosPosteriores,
horarioInicialBarbeiroSchedule,
horarioFinalBarbeiroSchedule,
};
}
39 changes: 36 additions & 3 deletions src/pages/Schedule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { horariosManha, horariosTarde, horariosNoite } from 'utils/horarios';
import { useTheme } from 'contexts/Theme';
import { useUser } from 'contexts/User';

import { useSchedule } from 'hooks/useSchedule';

import { css } from 'styles/calendar.styles';

import styles from './Schedule.module.scss';
Expand All @@ -34,6 +36,17 @@ export function Schedule() {
status,
} = useUser();

const {
setHorarioInicialBarbeiroSchedule,
setHorarioFinalBarbeiroSchedule,
getHorarioAtual,
numerosFaltantes,
desabilitarHorariosAnteriores,
desabilitarHorariosPosteriores,
horarioInicialBarbeiroSchedule,
horarioFinalBarbeiroSchedule,
} = useSchedule();

return (
<>
<style>{css}</style>
Expand Down Expand Up @@ -66,13 +79,18 @@ export function Schedule() {
fromMonth={new Date()}
selected={selectDay}
onDayClick={(day) => {
const weekDay = day.getDay();
getHorarioAtual(String(weekDay));
setHorarioInicialBarbeiroSchedule(JSON.parse(barbeiro?.schedules)[weekDay]?.from);
setHorarioFinalBarbeiroSchedule(JSON.parse(barbeiro?.schedules)[weekDay]?.to);
setSelectDay(day);
}}
modifiers={{
available: { dayOfWeek: [0, 1, 2, 3, 4, 5, 6] },
}}
disabled={[
{
dayOfWeek: numerosFaltantes,
before: new Date(),
},
]}
Expand All @@ -88,7 +106,12 @@ export function Schedule() {
{horariosManha.map((horario) => (
<button
key={horario}
disabled={isHorarioMarcado(horario) || isDataEHorarioPassado(selectDayFormatted, horario)}
disabled={
isHorarioMarcado(horario) ||
isDataEHorarioPassado(selectDayFormatted, horario) ||
desabilitarHorariosAnteriores(horarioInicialBarbeiroSchedule).includes(horario) ||
desabilitarHorariosPosteriores(horarioFinalBarbeiroSchedule).includes(horario)
}
className={selectHours === horario ? styles.selected : styles.horario}
onClick={() => {
setSelectHours(horario);
Expand All @@ -105,7 +128,12 @@ export function Schedule() {
{horariosTarde.map((horario) => (
<button
key={horario}
disabled={isHorarioMarcado(horario) || isDataEHorarioPassado(selectDayFormatted, horario)}
disabled={
isHorarioMarcado(horario) ||
isDataEHorarioPassado(selectDayFormatted, horario) ||
desabilitarHorariosAnteriores(horarioInicialBarbeiroSchedule).includes(horario) ||
desabilitarHorariosPosteriores(horarioFinalBarbeiroSchedule).includes(horario)
}
className={selectHours === horario ? styles.selected : styles.horario}
onClick={() => {
setSelectHours(horario);
Expand All @@ -122,7 +150,12 @@ export function Schedule() {
{horariosNoite.map((horario) => (
<button
key={horario}
disabled={isHorarioMarcado(horario) || isDataEHorarioPassado(selectDayFormatted, horario)}
disabled={
isHorarioMarcado(horario) ||
isDataEHorarioPassado(selectDayFormatted, horario) ||
desabilitarHorariosAnteriores(horarioInicialBarbeiroSchedule).includes(horario) ||
desabilitarHorariosPosteriores(horarioFinalBarbeiroSchedule).includes(horario)
}
className={selectHours === horario ? styles.selected : styles.horario}
onClick={() => {
setSelectHours(horario);
Expand Down

0 comments on commit d97d01a

Please sign in to comment.