AECC database project.
1const URL = "https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db";
2
3var last = 'buttons';
4const show = i => document.getElementById(i).hidden = false;
5const hide = i => document.getElementById(i).hidden = true;
6
7function next(i) {
8 hide(last);
9 last = i;
10 show(i);
11}
12
13function restart() {
14 show('buttons');
15 if (last != 'buttons')
16 hide(last);
17 last = 'buttons';
18}
19
20function tableFilter(event, table) {
21 var filter = event.target.value.toUpperCase();
22
23 var trs = table.getElementsByTagName("tr");
24
25 for (var i = 1; i < trs.length; i++) {
26 trs[i].style.display = "none";
27 tds = trs[i].getElementsByTagName("td");
28 for (var j = 0; j < tds.length; j++) {
29 td = tds[j];
30 if (td) {
31 td_text = td.textContent || td.innerText;
32 if (td_text.toUpperCase().indexOf(filter) > -1) {
33 trs[i].style.display = "";
34 }
35 }
36 }
37 }
38}
39
40function initTable(HTMLTable, SQLTable, editHandler, deleteHandler) {
41 const body = HTMLTable.tBodies[0];
42 body.innerHTML = "";
43 fetch (`${URL}/api/v1/read/?t=${SQLTable}`).then(resp => resp.json()).then(json => {
44 for (var i = 0; i < json.length; ++i) {
45 const obj = json[i];
46 let HTML = "<tr>";
47 for (var j = 0; j < obj.length; ++j) {
48 HTML += `<td>${obj[j]}</td>`;
49 }
50 HTML += `
51 <td style="border:0px;">
52 <button onclick='${editHandler}(${JSON.stringify(obj)})'>Edit</button>
53 </td>
54 `;
55 HTML += `
56 <td style="border:0px;">
57 <button style="color:#ff5454;" onclick='${deleteHandler}(${JSON.stringify(obj)})'>Delete</button>
58 </td>
59 `;
60 HTML += "</tr>";
61 body.innerHTML += HTML;
62 }
63 });
64}
65
66function fillSelect(select, SQLTable) {
67 const body = select;
68 console.log(select);
69}
70
71function fillTable(HTMLTable, SQLTable) {
72 const body = HTMLTable.tBodies[0];
73 body.innerHTML = "";
74 fetch(`${URL}/api/v1/read/?t=${SQLTable}`).then(resp => resp.json()).then(json => {
75 for (var i = 0; i < json.length; ++i) {
76 const obj = json[i];
77 let HTML = "<tr>";
78 for (var j = 0; j < obj.length; ++j) {
79 HTML += `<td>${obj[j]}</td>`;
80 }
81 HTML += "</tr>";
82 body.innerHTML += HTML;
83 }
84 });
85}
86
87async function get(endpoint, params) {
88 try {
89 return await fetch(`${URL}/api/v1/${endpoint}/?${params}`).then(resp => resp.json());
90 } catch (error) {
91 console.error(error.message);
92 return "";
93 }
94}
95
96async function post(endpoint, payload) {
97 try {
98 return await fetch(`${URL}/api/v1/${endpoint}/`, {
99 method: "POST",
100 headers: {
101 "Content-Type": "application/json"
102 },
103 body: JSON.stringify(payload)
104 }).then(resp => resp.json());
105 } catch (error) {
106 console.error(error.message);
107 return "";
108 }
109}