-
Notifications
You must be signed in to change notification settings - Fork 3
/
getServiceId.jsx
122 lines (99 loc) · 3.74 KB
/
getServiceId.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { useContext } from "react";
import { createContext, useEffect, useState } from "react";
import { api } from "../services/api";
import { CodeContext } from "./getCode";
export const ServiceIdContext = createContext()
export function ServiceIdProvider({ children }) {
const [today, setToday] = useState('')
const [calendar, setCalendar] = useState([])
const [serviceId, setServiceId] = useState('')
const [weekDay, setWeekDay] = useState('')
const options = {
weekday: 'long',
timeZone: 'America/Sao_Paulo'
};
let allDates = [];
async function getDates(url) {
await api
.get(url)
.then(({ data }) => {
data.results.forEach((item) => {
allDates.push(item);
});
if (data.next) {
getDates(data.next);
} else {
setCalendar((prevCalendar) => [...prevCalendar, ...allDates]);
}
});
}
async function getDayOfWeek(url) {
let allDay = [];
await api
.get(url)
.then(({ data }) => {
data.results.forEach((item) => {
allDay.push(item);
});
setWeekDay(allDay);
});
}
useEffect(() => {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
setToday(formattedDate)
}, [])
useEffect(() => {
const fetchCalendars = async () => {
await Promise.all([getDayOfWeek('calendar/'), getDates('calendar_dates/')])
}
fetchCalendars()
}, [])
let exceptionService = []
let baseService = []
function findService(todayDate) {
const services = calendar.filter((item) => item.date === todayDate)
if (services) {
const hasExceptionType1 = services.filter(item => item.exception_type === '1')
const filteredServices = []
hasExceptionType1.forEach(item => {
const matchingExceptionType1 = filteredServices.find(service => service.service_id.includes(item.service_id))
if (!matchingExceptionType1) {
filteredServices.push(item)
}
})
filteredServices.forEach(i => {
exceptionService.push(i.service_id)
})
}
const dayOfWeek = new Intl.DateTimeFormat('en-US', options).format(new Date()).toLowerCase();
const serviceWorks = weekDay.filter((service) => service[dayOfWeek] === 1);
const currentDate = new Date(todayDate);
if (serviceWorks.length > 0) {
const todayService = serviceWorks.filter((service) => {
const startDate = new Date(service.start_date);
const endDate = new Date(service.end_date);
return currentDate >= startDate && currentDate <= endDate && !service.service_id.includes("DESAT") && !service.service_id.includes("OBRA")
});
baseService.push(todayService)
if (todayService && exceptionService) {
const baseService = todayService.map(item => item.service_id)
const allServices = exceptionService.concat(baseService)
setServiceId(allServices);
}
}
}
useEffect(() => {
if (calendar.length > 0) {
findService(today)
}
}, [calendar])
return (
<ServiceIdContext.Provider value={{ serviceId }}>
{children}
</ServiceIdContext.Provider>
)
}