-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (83 loc) · 2.78 KB
/
index.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
// #region ----- GLOBAL DECLARATION --------
const _baseURL = `https://jsonplaceholder.typicode.com/`; //set base URL
var _tableBody; // store tableBody content
// #endregion ----- GLOBAL DECLARATION --------
// handle API call
const _getPostDetails = new Promise((resolve, reject) => {
fetch(_baseURL + `posts`).then((res) => {
// check if response object is not null/undefined
if (res && res.status) {
let resStatus = res.status; // get response status here
// handle different response code status scenirio
switch (resStatus) {
case 200:
resolve(res);
break;
case 401:
throw Error(`Unauthorized Request`);
default:
throw Error(`Invalid Request`);
}
} else {
// throw an error or display the error message on toaster.
throw Error('Invalid Request');
}
});
});
// #region ----- CRUD OPERATIONS --------
// get all posts
_getPostDetails
.then((apiResponse) => apiResponse.json())
.then((data) => {
intializeTable(); // clear table content before binding to avoid data overwritten issue
data.forEach(preparePostsTableView); // bind table data
});
// edit post
function editPost(postId) {
console.log(`post id to be edited => `, postId);
}
// delete post
function deletePost(postId) {
console.log(`post id to be deleted => `, postId);
}
// #endregion ----- CRUD OPERATIONS --------
// #region ----- TABLE MODIFCATION --------
function intializeTable() {
_tableBody = document.getElementById('postsTable');
_tableBody.innerHTML = '';
}
function preparePostsTableView(item) {
let tr = document.createElement('tr');
let idRow = document.createElement('td');
idRow.appendChild(document.createTextNode(item.id));
tr.appendChild(idRow);
let titleRow = document.createElement('td');
titleRow.appendChild(document.createTextNode(item.title));
tr.appendChild(titleRow);
let actionRow = document.createElement('td');
actionRow.appendChild(
createButtonElement(`edit`, `btn btn-info`, editPost, item.id)
);
actionRow.appendChild(
createButtonElement(`delete`, `btn btn-danger`, deletePost, item.id)
);
tr.appendChild(actionRow);
_tableBody.appendChild(tr);
}
// #endregion ----- TABLE MODIFCATION --------
// #region ----- GENERAL METHODS --------
function createButtonElement(
buttonText,
cssClassName,
buttonClickFuction,
funcArgument
) {
let buttonElement = document.createElement('button');
buttonElement.innerHTML = buttonText;
buttonElement.className = cssClassName;
buttonElement.addEventListener('click', () => {
buttonClickFuction(funcArgument);
});
return buttonElement;
}
// #endregion ----- GENERAL METHODS --------