summaryrefslogtreecommitdiffstats
path: root/src/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.js')
-rw-r--r--src/main.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main.js b/src/main.js
new file mode 100644
index 0000000..db9a00f
--- /dev/null
+++ b/src/main.js
@@ -0,0 +1,83 @@
+//import './style.css';
+function edit(e) {
+ e.preventDefault();
+ const tar = e.target;
+ const form = tar.parentNode;
+ form.onseeked;
+ const id = tar.dataset.id;
+ const tds = document.querySelectorAll(`[data-id="${id}"]`);
+ const names = ["name", "thumbnail", "author", "magazine",];
+ tds.forEach((el, i) => {
+ if (el.tagName === "TD") {
+ const v = el.innerHTML;
+ el.innerHTML = `<input name="${names[i]}" value="${v}">`;
+ }
+ else if (el === tar) {
+ el.style.display = "none";
+ }
+ else {
+ const iel = el;
+ if (iel.type !== "hidden") {
+ iel.value = "Save";
+ el.onclick = e => save(e, tds);
+ }
+ }
+ });
+}
+//@ts-expect-error
+window.edit = edit;
+async function save(e, tds) {
+ console.log(tds);
+ const tar = e.target;
+ const body = new FormData();
+ e.preventDefault();
+ let i = 0;
+ tds.forEach((el) => {
+ if (el.tagName === "TD") {
+ const v = el.firstChild.value;
+
+ if (i === 3)
+ el.innerHTML = v;
+ else
+ el.innerText = v;
+
+ switch (i) {
+ case 0:
+ body.append("name", v);
+ break;
+ case 1:
+ body.append("thumbnail", v);
+ break;
+ case 2:
+ body.append("author", v);
+ break;
+ case 3:
+ body.append("magazine", v);
+ break;
+ }
+ i++;
+ }
+ else if (el === tar) {
+ const iel = el;
+ iel.value = "Remove";
+ iel.onclick = null;
+ }
+ else {
+ const iel = el;
+ if (iel.type === "hidden") {
+ body.append(iel.name, iel.value);
+ }
+ else {
+ iel.style.display = "";
+ }
+ }
+ });
+ console.log(await (await fetch("api/add.php", {
+ method: "POST",
+ headers: {
+ // "Content-Type": "multipart/form-data",
+ },
+ body
+ })).text());
+}
+//# sourceMappingURL=main.js.map