-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
248 lines (223 loc) · 6.78 KB
/
App.tsx
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { StatusBar } from "expo-status-bar";
import { View } from "react-native";
import React, { useState, useEffect, useRef } from "react";
import NavBar from "./src/components/Navbar";
import Modal from "./src/components/Modal";
import { Container, Content, List } from "native-base";
import SearchBar from "./src/components/SearchBar";
import Pagination from "./src/components/Pagination";
import * as Font from "expo-font";
import ListRender from "./src/components/ListRender";
import FilterPicker from "./src/components/FilterPicker";
interface IGame {
name: string;
platform: string;
msrp: number;
publisher: string;
developer: string;
esrb: string;
releasedate: string;
romfilesize: string;
genre: string;
storelink: string;
officialsite: string;
}
const App: React.FC<IGame> = () => {
const [games, setGames] = useState<IGame[]>([]);
//State used for filtering
const [filter, setFilter] = useState<string>("none");
//States used for modal
const [show, setShow] = useState(false);
const [details, setDetails] = useState();
const [index, setIndex] = useState();
//States used for pagination
const [pageNum, setPageNum] = useState<number>(1);
const [prevBtnDisabled, setPrevBtnDisabled] = useState<boolean>(true);
const [nextBtnDisabled, setNextBtnDisabled] = useState<boolean>(false);
let pageResults: number = 0;
if(filter!=='none'){
const filteredGames = games.filter((e) => {
return e.esrb === filter;
})
pageResults = filteredGames.length;
}
else {
pageResults= games.length;
}
//State used for searching
const [search, setSearch] = useState<string>("");
//Variable that returns a mutable ref object
const initialRender = useRef(true);
const fetchGames = () => {
let requestBody = {
query: `
query {
games(name: "${search}", skip: ${pageNum}) {
_id
name
platform
msrp
publisher
developer
esrb
releasedate
romfilesize
genre
storelink
officialsite
}
}
`,
};
fetch("http://it2810-40.idi.ntnu.no:3000/graphql", {
method: "POST",
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
if (res.status !== 200 && res.status !== 201) {
throw new Error("Failed!");
}
const data = res.json();
return data;
})
.then((resData) => {
const games = resData.data.games;
setGames(games);
})
.catch((err) => {
console.log(err);
});
};
//Native base uses fonts that need to be loaded async
useEffect(() => {
(async () =>
await Font.loadAsync({
Roboto: require("native-base/Fonts/Roboto.ttf"),
}))();
}, []);
//Renders the fetching games function when either the searchword or pagenumber changes
useEffect(() => {
fetchGames();
}, [pageNum, search, filter]);
//Resets the pagenumber and checks if there are more than 6 elements to page on when the searchword changes
useEffect(() => {
if (initialRender.current) {
initialRender.current = false;
} else {
setPageNum(1);
setPrevBtnDisabled(true);
checkIfNextBtnDisabled();
}
}, [search, filter]);
//Keeps track of the game element that has been clicked
const handleClick = (index) => {
setIndex(index);
if(filter!=='none'){
const filteredGames = games.filter((e) => {
return e.esrb === filter;
})
setDetails(filteredGames[index]);
}
else {
setDetails(games[index]);
}
setShow(true);
};
//Updates the filter value when a filter is chosen
function updateChange(value) {
setFilter(value);
}
//Closes the modal
const closeModal = () => {
setShow(false);
};
//Next button is clickable except when on the last page number
//The prev button is clickable as long as there are more than 6 elements left from the first page (we are not on the first page)
function nextButton() {
if (pageResults > 6) {
setNextBtnDisabled(false);
setPageNum(pageNum + 1);
setPrevBtnDisabled(false);
}
if (pageResults < 12) {
setNextBtnDisabled(true);
}
}
//Prev button is clickable except when on the first page
//Next button is always clickable after having paged backwards
function prevButton() {
if (pageNum > 1) {
setPrevBtnDisabled(false);
setPageNum(pageNum - 1);
setNextBtnDisabled(false);
}
if (pageNum <= 2) {
setPrevBtnDisabled(true);
}
}
//Checks if there are more than 6 elements in the pageResults list. If not, both the prev and next button will be greyed out and disabled.
function checkIfNextBtnDisabled() {
if (pageResults > 6) {
setNextBtnDisabled(false);
}
if (pageResults <= 6) {
setNextBtnDisabled(true);
}
}
return (
<Container>
<StatusBar style="auto" />
<NavBar />
<SearchBar setSearch={setSearch} />
<FilterPicker filter={filter} updateChange={updateChange} />
<Content>
<View style={show ? { opacity: 0.3 } : null}>
<List style={{ backgroundColor: "#DBDADA" }}>
{filter == "none"
? games.slice(0, 6).map((game, index) => {
return (
<ListRender
key={index}
game={game}
index={index}
handleClick={handleClick.bind(this, index)}
/>
);
})
: games
.filter((e) => {
return e.esrb == filter;
})
.slice(0, 6)
.map((game, index) => {
return (
<ListRender
key={index}
game={game}
index={index}
handleClick={handleClick.bind(this, index)}
/>
);
})}
</List>
</View>
</Content>
{show == true ? (
<View>
<Modal detail={details} close={closeModal} />
</View>
) : null}
<Pagination
pageNum={pageNum}
nextButton={() => nextButton()}
prevButton={() => prevButton()}
prevBtnDisabled={prevBtnDisabled}
nextBtnDisabled={nextBtnDisabled}
/>
</Container>
);
};
export default App;