-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPracticeApi.js
More file actions
57 lines (54 loc) · 1.46 KB
/
PracticeApi.js
File metadata and controls
57 lines (54 loc) · 1.46 KB
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
async function PracticeAPI(
setFirstPage,
setLastPage,
page,
PracticeToggle,
setPracticeToggle,
setPreviousPage,
setNextPage,
setCurrentPage,
setData,
setLoader,
platform
) {
const creds = JSON.parse(localStorage.getItem('creds'))
const acessToken = creds.access
const response = await platform(acessToken, page, PracticeToggle)
if (response.status === 200) {
const data = await response.json()
if (data.status === 'OK' && data.result.length > 0) {
const newLinks = data.links
setFirstPage(newLinks.first.split('=')[1])
setLastPage(newLinks.last.split('=')[1])
if (newLinks.prev !== null) {
setPreviousPage(newLinks.prev.split('=')[1])
}
if (newLinks.next !== null) {
setNextPage(newLinks.next.split('=')[1])
}
setLastPage(data.meta.last_page)
setCurrentPage(data.meta.current_page)
const result = await data.result
await setData(result)
} else if (PracticeToggle == true) {
localStorage.setItem(
'err',
'Please practice or compete to view this page'
)
window.location = '/home'
} else {
setPracticeToggle(true)
}
setLoader(false)
} else {
const data = await response.json()
if (
data.error ===
"You haven't Entered your Atcoder Handle in your Profile.. Update Now!"
) {
localStorage.setItem('err', data.error)
window.location = '/home'
}
}
}
export default PracticeAPI