Skip to content

Commit

Permalink
fix: correção week days
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 12, 2022
1 parent 0ea07d3 commit be8f256
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
27 changes: 22 additions & 5 deletions src/hooks/useSchedule.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';

import { Schedule } from 'types/IContext';

Expand All @@ -8,17 +8,28 @@ import { useUser } from 'contexts/User';

export function useSchedule() {
const { barbeiro } = useUser();
const weekDay = new Date().getDay();
const [weekDay, setWeekDay] = useState<string>(String(new Date().getDay()));

const schedules = JSON.parse(barbeiro?.schedules || '[]');
function getSchedule(weekDay: string) {
const schedule = [] as Schedule[];
const schedules = JSON.parse(barbeiro?.schedules || '[]');
schedules.map((scheduleItem: Schedule) => {
if (scheduleItem.week_day === weekDay) {
schedule.push(scheduleItem);
}
});
return schedule;
}

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

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

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

function getHorarioAtual(week_day: string) {
Expand Down Expand Up @@ -93,6 +104,11 @@ export function useSchedule() {
return horariosPosteriores;
}

useEffect(() => {
setHorarioInicialBarbeiroSchedule(getSchedule(weekDay || '0').map((schedule) => schedule.from));
setHorarioFinalBarbeiroSchedule(getSchedule(weekDay || '0').map((schedule) => schedule.to));
}, [weekDay]);

return {
setHorarioInicialBarbeiroSchedule,
setHorarioFinalBarbeiroSchedule,
Expand All @@ -102,5 +118,6 @@ export function useSchedule() {
desabilitarHorariosPosteriores,
horarioInicialBarbeiroSchedule,
horarioFinalBarbeiroSchedule,
setWeekDay,
};
}
13 changes: 6 additions & 7 deletions src/pages/Schedule/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ export function Schedule() {
} = useUser();

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

return (
Expand Down Expand Up @@ -79,10 +78,10 @@ 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);
const weekDayClick = day.getDay();
getHorarioAtual(String(weekDayClick));
setWeekDay(String(weekDayClick));

setSelectDay(day);
}}
modifiers={{
Expand Down

0 comments on commit be8f256

Please sign in to comment.