-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.js
137 lines (111 loc) · 3.92 KB
/
context.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use client'
import React, { createContext, useState, useEffect } from 'react';
export const DataContext = createContext(null);
export const WindowDimensionsContext = createContext(null);
export const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api';
export const DataProvider = ({ children }) => {
console.log(process.env.NEXT_PUBLIC_API_URL)
// 1. Intro section
const [stats, setStats] = useState({});
// 2. Contribution Section
const [yearly, setYearly] = useState({});
const [daily, setDaily] = useState({});
const [community, setCommunity] = useState({});
// 3. Impact Section
const [cumulativeYearly, setCumulativeYearly] = useState({});
const [heritage_destinations, setHeritageDestinations] = useState({});
const [locations, setLocations] = useState([]);
const [inceptions, setInceptions] = useState({});
// 4. Participant section
const [participants, setParticipants] = useState([]);
// 5. monuments
const [monument_images, setMonumentImages] = useState([]);
// 6. image submissions
const [submission_images, setSubmissionImages] = useState([]);
useEffect(() => {
// 1. Fetch competition statistics
fetch(`${apiUrl}/competition/statistics`)
.then(response => response.json())
.then(data => setStats(data));
// 2.1 Fetch yearly submissions
fetch(`${apiUrl}/submissions/yearly`)
.then(response => response.json())
.then(data => setYearly(data));
// 2.2 Fetch daily submissions
fetch(`${apiUrl}/submissions/daily`)
.then(response => response.json())
.then(data => setDaily(data));
// 2.3 Fetch community data
fetch(`${apiUrl}/participants/submissions`)
.then(response => response.json())
.then(data => setCommunity(data));
// 3.1 Fetch submission cumulative yearly data
fetch(`${apiUrl}/submissions/yearly/total`)
.then(response => response.json())
.then(data => setCumulativeYearly(data));
// 3.2 Fetch monument locations & heritage destination data
fetch(`${apiUrl}/monuments/locations`)
.then(response => response.json())
.then(data => setLocations(data));
// 3.2 Fetch heritage destination data
fetch(`${apiUrl}/monuments/destinations`)
.then(response => response.json())
.then(data => setHeritageDestinations(data));
// 3.4 Fetch monument inceptions date
fetch(`${apiUrl}/monuments/inceptions`)
.then(response => response.json())
.then(data => setInceptions(data));
// 4.1 Fetch all the participants
fetch(`${apiUrl}/participants`)
.then(response => response.json())
.then(data => setParticipants(data));
// 5.1 Fetch all the monuments
fetch(`${apiUrl}/monuments/images`)
.then(response => response.json())
.then(data => setMonumentImages(data));
// 6.1 Fetch all image submissions
fetch(`${apiUrl}/submissions/images`)
.then(response => response.json())
.then(data => setSubmissionImages(data));
}, []);
const contextValue = {
stats,
yearly,
daily,
community,
cumulativeYearly,
locations,
heritage_destinations,
inceptions,
participants,
monument_images,
submission_images
};
return (
<DataContext.Provider value={contextValue}>
{children}
</DataContext.Provider>
);
};
export const WindowDimensionsProvider = ({ children }) => {
const [dimensions, setDimensions] = useState({
width: undefined,
height: undefined,
});
useEffect(() => {
const handleResize = () => {
setDimensions({
width: window.innerWidth,
height: window.innerHeight,
});
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, []);
return (
<WindowDimensionsContext.Provider value={dimensions}>
{children}
</WindowDimensionsContext.Provider>
);
};