-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathfetch.js
More file actions
116 lines (100 loc) · 3.21 KB
/
fetch.js
File metadata and controls
116 lines (100 loc) · 3.21 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
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
/**
* Execute a fetch request with the given options
* @param {string} method: GET, POST, PUT, DELETE
* @param {String} endpoint: The endpoint to call
* @param {Object} data: The data to send to the endpoint, if any
* @returns response
*/
function callApi(method, endpoint, token, data = null) {
const headers = new Headers();
const bearer = `Bearer ${token}`;
headers.append('Authorization', bearer);
if (data) {
headers.append('Content-Type', 'application/json');
}
const options = {
method: method,
headers: headers,
body: data ? JSON.stringify(data) : null,
};
return fetch(endpoint, options)
.then((response) => {
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return response.json();
} else {
return response;
}
});
}
/**
* Handles todolist actions
* @param {Object} task
* @param {string} method
* @param {string} endpoint
*/
async function handleToDoListActions(task, method, endpoint) {
let listData;
try {
const accessToken = await getToken();
const data = await callApi(method, endpoint, accessToken, task);
switch (method) {
case 'POST':
listData = JSON.parse(localStorage.getItem('todolist'));
listData = [data, ...listData];
localStorage.setItem('todolist', JSON.stringify(listData));
AddTaskToToDoList(data);
break;
case 'DELETE':
listData = JSON.parse(localStorage.getItem('todolist'));
const index = listData.findIndex((todoItem) => todoItem.id === task.id);
localStorage.setItem('todolist', JSON.stringify([...listData.splice(index, 1)]));
showToDoListItems(listData);
break;
default:
console.log('Unrecognized method.')
break;
}
} catch (error) {
console.error(error);
}
}
/**
* Handles todolist action GET action.
*/
async function getToDos() {
try {
const accessToken = await getToken();
const data = await callApi(
'GET',
protectedResources.toDoListAPI.endpoint,
accessToken
);
if (data) {
localStorage.setItem('todolist', JSON.stringify(data));
showToDoListItems(data);
}
} catch (error) {
console.error(error);
}
}
/**
* Retrieves an access token.
*/
async function getToken() {
let tokenResponse;
if (typeof getTokenPopup === 'function') {
tokenResponse = await getTokenPopup({
scopes: [...protectedResources.toDoListAPI.scopes.read],
redirectUri: '/redirect'
});
} else {
tokenResponse = await getTokenRedirect({
scopes: [...protectedResources.toDoListAPI.scopes.read],
});
}
if (!tokenResponse) {
return null;
}
return tokenResponse.accessToken;
}