-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
120 lines (104 loc) · 3.13 KB
/
script.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const BASE_URL = "https://63539e82ccce2f8c02f9ceee.mockapi.io";
const getAllRecords = async () => {
const response = await fetch(`${BASE_URL}/api`);
const records = await response.json();
prepareGrid(records);
};
const prepareGrid = (records) => {
let tableContent = "";
records.map((record) => {
tableContent += `<tr>
<th scope="row">${record.id}</th>
<td>${record.name}</td>
<td><img src="${
record.avatar
}" style="width:2rem;" class="rounded shadow" alt="img"> </td>
<td>${new Date(record.createdAt).toLocaleDateString()}</td>
<td>
<button type="button" class="btn btn-danger"
onclick="deleteHandler(${record.id},'${record.name}')">
<img src="../../Assets/delete.png" style="width:1rem" alt="delete icon" />
</button>
<button type="button" class="btn btn-warning"
onclick="editHandler(${record.id})">
<img src="../../Assets/edit.png" style="width:1rem" alt="edit icon" />
</button>
</td>
</tr>`;
});
document.getElementById("tbody").innerHTML = tableContent;
};
const deleteHandler = (id, name) => {
if (confirm(`Are you sure to delete ${name} record?`)) {
if (deleteRecord(id)) {
showAlert("Record deleted successfully", "success");
getAllRecords();
} else showAlert("Record deleted successfully", "warning");
}
};
const deleteRecord = async (id) => {
try {
await fetch(`${BASE_URL}/api/${id}`, {
method: "DELETE",
});
return true;
} catch (er) {
console.error(er);
return false;
}
};
const addBtn = document.getElementById("btnAdd");
addBtn.addEventListener("click", () => {
const name = prompt("Enter your name");
if (name === "" || name === null) {
alert("name is mandatory");
return;
}
addRecord({ name });
// showAlert("Record Added successfully", "success");
const toastLiveExample = document.getElementById("liveToast");
const toast = new bootstrap.Toast(toastLiveExample);
toast.show();
});
const addRecord = async (record) => {
await fetch(`${BASE_URL}/api`, {
method: "POST",
headers: {
"Content-Type": "application/json",
//,"Authorization": "Bearer token"
},
body: JSON.stringify(record),
});
await getAllRecords();
};
const editHandler = (id) => {
const name = prompt("Enter your name");
if (name === "" || name === null) {
alert("name is mandatory");
return;
}
updateRecord(id, { name });
showAlert("Record updated successfully", "success");
getAllRecords();
};
const updateRecord = async (id, record) => {
await fetch(`${BASE_URL}/api/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(record),
});
};
const alertPlaceholder = document.getElementById("liveAlertPlaceholder");
const showAlert = (message, type) => {
const wrapper = document.createElement("div");
wrapper.innerHTML = [
`<div class="alert alert-${type} alert-dismissible" role="alert">`,
` <div>${message}</div>`,
' <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
"</div>",
].join("");
alertPlaceholder.append(wrapper);
};
getAllRecords();