-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
133 lines (122 loc) · 3.16 KB
/
request.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
121
122
123
124
125
126
127
128
129
130
131
132
133
import { cardIdNfc } from "./js/main.js";
const contentEl = document.getElementById("api-content");
const fruitBtn = document.getElementById("fruit-btn");
const hostingBase = "https://learnol.cyclic.app";
const localhostBase = "http://localhost:9090";
// Fetches data from the backend
fruitBtn.addEventListener("click", function () {
let user = "fruit";
let exampleCard = "4a:2c:74:1b";
fetch(`${hostingBase}/${user}/${exampleCard}`, {
method: "GET",
headers: {
"Content-Type": "text/html",
},
})
.then((data) => {
return data.text();
})
.then((res) => {
contentEl.innerHTML = res;
});
});
async function startRequest() {
const idInput = await cardIdNfc();
if (idInput === "4a:2c:74:1b") {
fruitBtn.innerHTML = idInput;
}
if (idInput !== undefined) {
try {
let user = "fruit";
let card = idInput;
fetch(`${hostingBase}/${user}/${card}`, {
method: "GET",
headers: {
"Content-Type": "text/html",
},
})
.then((data) => {
return data.text();
})
.then((res) => {
contentEl.innerHTML = res;
});
} catch (error) {
contentEl.innerHTML = error;
}
} else {
fruitBtn.style.backgroundColor = "yellow";
}
}
startRequest();
//* Sends pre-specified data to the backend
const putBtn = document.getElementById("put-btn");
putBtn.addEventListener("click", function () {
fetch(`${hostingBase}/card`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user: "molekylverkstan",
card_type: "secondary_card",
card_id: "ABC123",
page_name: "about",
}),
})
.then((data) => {
return data.text();
})
.then((res) => {
console.log(res);
});
});
//* Sends custom data to the backend
const sendPutBtn = document.getElementById("send-btn");
sendPutBtn.addEventListener("click", async function putInput() {
const pageInput = document.getElementById("page-input").value;
const isPrimary = document.getElementById("is-primary").checked;
const userInput = document.getElementById("user-input").value;
// const idInput = await cardIdNfc();
let idInput = document.getElementById("id-input").value;
if (isPrimary) {
var cardType = "primary_card";
} else {
var cardType = "secondary_card";
}
fetch(`${hostingBase}/card`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user: userInput,
card_type: cardType,
page_name: pageInput,
card_id: idInput,
}),
})
.then((data) => {
return data.text();
})
.then((res) => {
console.log(res);
});
});
//* Refreshes the content of the database with possible changes in the markdown repos
const refreshBtn = document.getElementById("refresh-btn");
// let user = "fruit";
refreshBtn.addEventListener("click", async () => {
fetch(`${hostingBase}/refresh`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((data) => {
return data.text();
})
.then((res) => {
console.log(res);
});
});