Skip to content

Commit dcc1a40

Browse files
committed
Function book list rendition and uploading feature for QLibrary.
1 parent 41cf5a6 commit dcc1a40

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

demo/qlibrary_app/scripts/qlibrary.js

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/*
2+
* This file is part of QLBase (https://github.com/nthnn/QLBase).
3+
* Copyright 2024 - Nathanne Isip
4+
*
5+
* Permission is hereby granted, free of charge,
6+
* to any person obtaining a copy of this software
7+
* and associated documentation files (the “Software”),
8+
* to deal in the Software without restriction,
9+
* including without limitation the rights to use, copy,
10+
* modify, merge, publish, distribute, sublicense, and/or
11+
* sell copies of the Software, and to permit persons to
12+
* whom the Software is furnished to do so, subject to
13+
* the following conditions:
14+
*
15+
* The above copyright notice and this permission notice
16+
* shall be included in all copies or substantial portions
17+
* of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF
20+
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
21+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
22+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
23+
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
24+
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
27+
* OR OTHER DEALINGS IN THE SOFTWARE.
28+
*/
29+
30+
$("#book-thumbnail").on("change", (e)=> {
31+
if(!e.target.files.length) {
32+
$("#thumbnail").attr("src", "");
33+
return;
34+
}
35+
36+
$("#thumbnail").attr("src", URL.createObjectURL(e.target.files.item(0)));
37+
$("#thumbnail").removeClass("d-none");
38+
$("#thumbnail").addClass("d-block");
39+
});
40+
41+
$("#uploadModal").on("hidden.bs.modal", ()=> {
42+
$("#upload-form").trigger("reset");
43+
44+
$("#thumbnail").attr("src", "");
45+
$("#thumbnail").removeClass("d-block");
46+
$("#thumbnail").addClass("d-none");
47+
});
48+
49+
$("#upload").on("click", () => {
50+
const formData = new FormData();
51+
formData.append("name", "books");
52+
53+
$.ajax({
54+
url: Environment.action("db_get_by_name"),
55+
type: "POST",
56+
headers: {
57+
"QLBase-API-Key": Environment.key,
58+
"QLBase-App-ID": Environment.id
59+
},
60+
data: formData,
61+
contentType: false,
62+
processData: false,
63+
dataType: "json",
64+
success: (data) => {
65+
const bookTitle = $("#book-title").val(),
66+
bookDesc = $("#book-desc").val();
67+
68+
69+
const uploadPdfData = new FormData();
70+
uploadPdfData.append(
71+
"subject",
72+
document.querySelector("#book-pdf").files[0]
73+
);
74+
75+
$.ajax({
76+
url: Environment.action("file_upload"),
77+
type: "POST",
78+
headers: {
79+
"QLBase-API-Key": Environment.key,
80+
"QLBase-App-ID": Environment.id
81+
},
82+
data: uploadPdfData,
83+
contentType: false,
84+
processData: false,
85+
dataType: "json",
86+
success: (pdfUpRes) => {
87+
if(pdfUpRes.result === "1") {
88+
const ticketData = new FormData();
89+
ticketData.append("name", pdfUpRes.value);
90+
ticketData.append("should_expire", 0);
91+
92+
$.ajax({
93+
url: Environment.action("file_download"),
94+
type: "POST",
95+
headers: {
96+
"QLBase-API-Key": Environment.key,
97+
"QLBase-App-ID": Environment.id
98+
},
99+
data: ticketData,
100+
contentType: false,
101+
processData: false,
102+
dataType: "json",
103+
success: (pdfTicket) => {
104+
const uploadThumbnailData = new FormData();
105+
uploadThumbnailData.append(
106+
"subject",
107+
document.querySelector("#book-thumbnail").files[0]
108+
);
109+
110+
$.ajax({
111+
url: Environment.action("file_upload"),
112+
type: "POST",
113+
headers: {
114+
"QLBase-API-Key": Environment.key,
115+
"QLBase-App-ID": Environment.id
116+
},
117+
data: uploadThumbnailData,
118+
contentType: false,
119+
processData: false,
120+
dataType: "json",
121+
success: (res) => {
122+
if(res.result === "1") {
123+
const ticketData = new FormData();
124+
ticketData.append("name", res.value);
125+
ticketData.append("should_expire", 0);
126+
127+
$.ajax({
128+
url: Environment.action("file_download"),
129+
type: "POST",
130+
headers: {
131+
"QLBase-API-Key": Environment.key,
132+
"QLBase-App-ID": Environment.id
133+
},
134+
data: ticketData,
135+
contentType: false,
136+
processData: false,
137+
dataType: "json",
138+
success: (ticketRes) => {
139+
const db = data.value[1];
140+
db[bookTitle] = [bookDesc, ticketRes.value, pdfTicket.value];
141+
formData.append("content", btoa(JSON.stringify(db)));
142+
143+
$.ajax({
144+
url: Environment.action("db_write"),
145+
type: "POST",
146+
headers: {
147+
"QLBase-API-Key": Environment.key,
148+
"QLBase-App-ID": Environment.id
149+
},
150+
data: formData,
151+
contentType: false,
152+
processData: false,
153+
dataType: "json",
154+
success: (result) => {
155+
$("#uploadModal").modal("toggle");
156+
$("#uploadSuccessModal").modal("toggle");
157+
}
158+
});
159+
}
160+
});
161+
}
162+
}
163+
});
164+
}
165+
});
166+
}
167+
}
168+
});
169+
}
170+
});
171+
});
172+
173+
const renderBookCard = (title, description, thumbnail, pdf)=> {
174+
let contents = "<div class=\"col-lg-3\"><div class=\"card border-secondary mb-3\">";
175+
contents += "<div class=\"card-header\">" + title + "</div>";
176+
contents += "<div class=\"card-body\"><p class=\"card-text\">" +
177+
description + "<a target=\"_blank\" href=\"" + Environment.cdp(pdf) +
178+
"\" class=\"btn btn-outline-primary w-100 mt-2\">Download</a></p></div>";
179+
contents += "<img src=\"" + Environment.cdp(thumbnail) + "\" class=\"card-img-bottom\" />"
180+
contents += "</div></div>";
181+
182+
return contents;
183+
};
184+
185+
let prevData = null;
186+
const updateAvailableBooks = ()=> {
187+
let formData = new FormData();
188+
formData.append("name", "books");
189+
190+
$.ajax({
191+
url: Environment.action("db_read"),
192+
type: "POST",
193+
headers: {
194+
"QLBase-API-Key": Environment.key,
195+
"QLBase-App-ID": Environment.id
196+
},
197+
data: formData,
198+
contentType: false,
199+
processData: false,
200+
dataType: "json",
201+
success: (data)=> {
202+
$("#loading-view").addClass("d-none");
203+
if(prevData == JSON.stringify(data))
204+
return;
205+
206+
const bookList = $("#book-list");
207+
bookList.html("");
208+
prevData = JSON.stringify(data);
209+
210+
console.log(data.value);
211+
if(Object.keys(data.value).length == 0) {
212+
$("#no-books-yet").removeClass("d-none");
213+
$("#no-books-yet").addClass("d-block");
214+
215+
return;
216+
}
217+
else {
218+
$("#no-books-yet").removeClass("d-block");
219+
$("#no-books-yet").addClass("d-none");
220+
}
221+
222+
let content = "<div class=\"row\">", count = 0;
223+
for(const [key, values] of Object.entries(data.value)) {
224+
if(count == 4) {
225+
count = 0;
226+
content += "</div><div class=\"row\">";
227+
}
228+
229+
content += renderBookCard(key, values[0], values[1], values[2]);
230+
count++;
231+
}
232+
233+
content += "</div>";
234+
bookList.append(content);
235+
bookList.removeClass("d-none");
236+
bookList.addClass("d-block");
237+
}
238+
});
239+
};
240+
241+
setInterval(updateAvailableBooks, 500);

0 commit comments

Comments
 (0)