From dcc1a401ddffb312495b34a8f05c9685198c47f5 Mon Sep 17 00:00:00 2001 From: Nathanne Isip Date: Fri, 15 Nov 2024 05:07:43 +0800 Subject: [PATCH] Function book list rendition and uploading feature for QLibrary. --- demo/qlibrary_app/scripts/qlibrary.js | 241 ++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 demo/qlibrary_app/scripts/qlibrary.js diff --git a/demo/qlibrary_app/scripts/qlibrary.js b/demo/qlibrary_app/scripts/qlibrary.js new file mode 100644 index 0000000..c8e0d9a --- /dev/null +++ b/demo/qlibrary_app/scripts/qlibrary.js @@ -0,0 +1,241 @@ +/* + * This file is part of QLBase (https://github.com/nthnn/QLBase). + * Copyright 2024 - Nathanne Isip + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software + * and associated documentation files (the “Software”), + * to deal in the Software without restriction, + * including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice + * shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF + * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT + * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ + +$("#book-thumbnail").on("change", (e)=> { + if(!e.target.files.length) { + $("#thumbnail").attr("src", ""); + return; + } + + $("#thumbnail").attr("src", URL.createObjectURL(e.target.files.item(0))); + $("#thumbnail").removeClass("d-none"); + $("#thumbnail").addClass("d-block"); +}); + +$("#uploadModal").on("hidden.bs.modal", ()=> { + $("#upload-form").trigger("reset"); + + $("#thumbnail").attr("src", ""); + $("#thumbnail").removeClass("d-block"); + $("#thumbnail").addClass("d-none"); +}); + +$("#upload").on("click", () => { + const formData = new FormData(); + formData.append("name", "books"); + + $.ajax({ + url: Environment.action("db_get_by_name"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: formData, + contentType: false, + processData: false, + dataType: "json", + success: (data) => { + const bookTitle = $("#book-title").val(), + bookDesc = $("#book-desc").val(); + + + const uploadPdfData = new FormData(); + uploadPdfData.append( + "subject", + document.querySelector("#book-pdf").files[0] + ); + + $.ajax({ + url: Environment.action("file_upload"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: uploadPdfData, + contentType: false, + processData: false, + dataType: "json", + success: (pdfUpRes) => { + if(pdfUpRes.result === "1") { + const ticketData = new FormData(); + ticketData.append("name", pdfUpRes.value); + ticketData.append("should_expire", 0); + + $.ajax({ + url: Environment.action("file_download"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: ticketData, + contentType: false, + processData: false, + dataType: "json", + success: (pdfTicket) => { + const uploadThumbnailData = new FormData(); + uploadThumbnailData.append( + "subject", + document.querySelector("#book-thumbnail").files[0] + ); + + $.ajax({ + url: Environment.action("file_upload"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: uploadThumbnailData, + contentType: false, + processData: false, + dataType: "json", + success: (res) => { + if(res.result === "1") { + const ticketData = new FormData(); + ticketData.append("name", res.value); + ticketData.append("should_expire", 0); + + $.ajax({ + url: Environment.action("file_download"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: ticketData, + contentType: false, + processData: false, + dataType: "json", + success: (ticketRes) => { + const db = data.value[1]; + db[bookTitle] = [bookDesc, ticketRes.value, pdfTicket.value]; + formData.append("content", btoa(JSON.stringify(db))); + + $.ajax({ + url: Environment.action("db_write"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: formData, + contentType: false, + processData: false, + dataType: "json", + success: (result) => { + $("#uploadModal").modal("toggle"); + $("#uploadSuccessModal").modal("toggle"); + } + }); + } + }); + } + } + }); + } + }); + } + } + }); + } + }); +}); + +const renderBookCard = (title, description, thumbnail, pdf)=> { + let contents = "
"; + contents += "
" + title + "
"; + contents += "

" + + description + "Download

"; + contents += "" + contents += "
"; + + return contents; +}; + +let prevData = null; +const updateAvailableBooks = ()=> { + let formData = new FormData(); + formData.append("name", "books"); + + $.ajax({ + url: Environment.action("db_read"), + type: "POST", + headers: { + "QLBase-API-Key": Environment.key, + "QLBase-App-ID": Environment.id + }, + data: formData, + contentType: false, + processData: false, + dataType: "json", + success: (data)=> { + $("#loading-view").addClass("d-none"); + if(prevData == JSON.stringify(data)) + return; + + const bookList = $("#book-list"); + bookList.html(""); + prevData = JSON.stringify(data); + + console.log(data.value); + if(Object.keys(data.value).length == 0) { + $("#no-books-yet").removeClass("d-none"); + $("#no-books-yet").addClass("d-block"); + + return; + } + else { + $("#no-books-yet").removeClass("d-block"); + $("#no-books-yet").addClass("d-none"); + } + + let content = "
", count = 0; + for(const [key, values] of Object.entries(data.value)) { + if(count == 4) { + count = 0; + content += "
"; + } + + content += renderBookCard(key, values[0], values[1], values[2]); + count++; + } + + content += "
"; + bookList.append(content); + bookList.removeClass("d-none"); + bookList.addClass("d-block"); + } + }); +}; + +setInterval(updateAvailableBooks, 500); \ No newline at end of file