-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSimpleApi.js
59 lines (53 loc) · 1.56 KB
/
SimpleApi.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
async function SimpleAPIData(
setFirstPage,
setLastPage,
page,
setPreviousPage,
setNextPage,
setCurrentPage,
setData,
setLoader,
platform
) {
const creds = JSON.parse(localStorage.getItem('creds'))
const acessToken = creds.access
const response = await platform(acessToken, page)
if (response.status === 200) {
const data = await response.json()
if (data.status === 'OK') {
if (data.result.length > 0) {
const newLinks = data.links
await setFirstPage(newLinks.first.split('=')[1])
await setLastPage(newLinks.last.split('=')[1])
if (newLinks.prev !== null) {
setPreviousPage(newLinks.prev.split('=')[1])
}
if (newLinks.next !== null) {
setNextPage(newLinks.next.split('=')[1])
}
await setLastPage(data.meta.last_page)
await setCurrentPage(data.meta.current_page)
} else {
localStorage.setItem(
'err',
'Codechef upsolve is available when you participate in atleast one contest'
)
window.location = '/home'
}
} else {
localStorage.setItem('err', 'No contest found for this handle')
window.location = '/home'
}
const result = await data.result
await setData(result)
setLoader(false)
} else if (response.status == 500) {
localStorage.setItem('err', 'No contest found for this handle')
window.location = '/home'
} else {
const data = await response.json()
localStorage.setItem('err', data.error)
window.location = '/home'
}
}
export default SimpleAPIData