-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetTheme.jsx
76 lines (62 loc) · 2.12 KB
/
getTheme.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
import { createContext, useContext, useEffect, useState } from "react";
import { CodeContext } from "./getCode";
import { RoutesContext } from "./getRoutes";
import { api } from "../services/api";
import { ServiceIdContext } from "./getServiceId";
export const ThemeContext = createContext()
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("")
const [routeType, setRouteType] = useState([])
const { stopId } = useContext(CodeContext)
const {serviceId} = useContext(ServiceIdContext)
const setBrt = () => {
document.documentElement.setAttribute("data-theme", "brt");
}
const setSppo = () => {
document.documentElement.setAttribute("data-theme", "sppo");
};
async function findTheme(url) {
let routeTypes = [];
await api
.get(url)
.then(({ data }) => {
data.results.forEach((item) => {
routeTypes.push(item.trip_id.route_id.route_type);
});
const brtRoute = (e) => e === 702
if (routeTypes.some(brtRoute)) {
setBrt();
setTheme("")
} else if (routeTypes === 3 || 700) {
setSppo();
setTheme("sppo")
}
setRouteType([...routeTypes]);
});
}
function multiModalTheme(PlatformRouteType){
if(PlatformRouteType === 702){
setBrt();
setTheme("")
} else if (PlatformRouteType === 700 || 3) {
setSppo();
setTheme("sppo")
}
}
function resetMultiModalTheme(){
if(theme == "sppo"){
setBrt()
setTheme("")
}
}
useEffect(() => {
if (stopId && serviceId) {
findTheme(`/stop_times/?stop_id=${stopId}&service_id=${serviceId}`)
}
}, [stopId, serviceId])
return (
<ThemeContext.Provider value={{ theme, setTheme, setSppo, setRouteType, routeType, multiModalTheme, resetMultiModalTheme }}>
{children}
</ThemeContext.Provider>
)
}