-
Notifications
You must be signed in to change notification settings - Fork 2
/
Context.js
178 lines (147 loc) · 4.46 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import axios from "axios";
import {
createContext,
useContext,
useEffect,
useState,
useTransition,
} from "react";
// external libraries
import moment from "moment";
export const cp = createContext();
const Context = ({ children }) => {
// all the events
const [events, setEvents] = useState([]);
// data related states that store data from the api
const [allMatches, setAllMatches] = useState([]);
const [todaysMatches, setTodaysMatches] = useState([]);
const [liveMatches, setLiveMatches] = useState([]);
const [upcomingMatches, setUpcomingMatches] = useState([]);
const [previousMatches, setPreviousMatches] = useState([]);
// contains the search Queries
const [query, setQuery] = useState();
// contains the search output.
const [result, setResult] = useState([]);
// this will show the details of the match
const [showDetails, setShowDetails] = useState(false);
// for the loading animation
const [loading, setLoading] = useState(true);
// gets all the events data from the api
const getAllEvents = async () => {
try {
const { data } = await axios.get(
"https://worldcupjson.net/matches?by_date=asc"
);
setAllMatches(data);
} catch (error) {
console.log(error.message);
}
};
const getLiveEvents = async () => {
try {
const { data } = await axios.get(
"https://worldcupjson.net/matches/current"
);
setLiveMatches(data);
} catch (error) {
console.log(error.message);
}
};
const getTodaysEvents = async () => {
try {
const { data } = await axios.get(
"https://worldcupjson.net/matches/today"
);
// contains todays matches
const matches = [];
data.forEach((match, index) => {
if (match.status === "in_progress") return;
matches.push(match);
});
setTodaysMatches(matches);
} catch (error) {
console.log(error.message);
}
};
const getUpcomingMatches = async () => {
try {
const { data } = await axios.get(
"https://worldcupjson.net/matches?by_date=ASC"
);
// this will contain all the upcoming matches
const matches = [];
if (!data) return;
data.forEach((match, index) => {
const days = moment.duration(moment(match.datetime).diff()).asDays();
// today will not be added to the matches
if (days > 1) matches.push({ ...match });
});
// updating the context
setUpcomingMatches(matches);
} catch (error) {
console.log(error.message);
}
};
const getPreviousMatches = async () => {
try {
const { data } = await axios.get(
"https://worldcupjson.net/matches?by_date=ASC"
);
// this will contain all the previous matches
const matches = [];
if (!data) return;
data?.forEach((match, index) => {
const days = moment.duration(moment(match.datetime).diff()).asDays();
// today is not added to the matches
if (days < -1) {
matches.push({ ...match });
}
});
// reversing the previous matches so, that the most recent previous matches gets mapped to the top.
matches.reverse();
// updating the context
setPreviousMatches(matches);
} catch (error) {
console.log(error.message);
}
};
useEffect(() => {
if (!allMatches) return;
const r = [];
previousMatches.forEach((match, index) => {
const c1 = match.home_team.name.toLowerCase();
const c2 = match.away_team.name.toLowerCase();
if (c1.includes(query.toLowerCase()) || c2.includes(query.toLowerCase()))
r.push(match);
});
setResult(r);
}, [query]);
// component did mount
useEffect(() => {
getAllEvents(); // gets all the matches
getTodaysEvents(); // gets all the matches that are being held today
getLiveEvents(); // gets live matches if available
getUpcomingMatches(); // gets upcoming matches that have a fixed team
getPreviousMatches(); // gets all the matches that were held before
}, []);
return (
<cp.Provider
value={{
events: {
all: allMatches,
today: todaysMatches,
live: liveMatches,
upNext: upcomingMatches,
pre: previousMatches,
},
search: [query, setQuery],
output: [result],
popup: [showDetails, setShowDetails],
animation: [loading, setLoading],
}}
>
{children}
</cp.Provider>
);
};
export default Context;