From 1011740ce42ba85e4472b43c2025c833e6c88960 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 9 May 2023 23:38:49 +0400 Subject: [PATCH] Fixed and completed guestbook_admin.js. --- web/assets/js/guestbook_admin.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/web/assets/js/guestbook_admin.js b/web/assets/js/guestbook_admin.js index fd22c74..b1695a8 100644 --- a/web/assets/js/guestbook_admin.js +++ b/web/assets/js/guestbook_admin.js @@ -1,13 +1,16 @@ -function get_entry_id(button) { - return button.form.children[0].value +function get_field(e, el) { + return e.form.elements[el].value } // EDIT ENTRY function edit_entry(e) { let data = new URLSearchParams(); - data.append("message", e.target.form[1].value); - fetch(`/api/guestbook/${e.target.form[0].value}`, {method: "PATCH", body: data}) + data.append("message", get_field(e.target, "message")); + data.append("name", get_field(e.target, "name")); + data.append("website", get_field(e.target, "website")); + data.append("hide_website", get_field(e.target, "hide_website")); + fetch(`/api/guestbook/${get_field(e.target, "entry_id")}`, {method: "PATCH", body: data}) .catch(e => console.log(e)) .then(() => window.location = "/guestbook/admin"); } @@ -18,7 +21,7 @@ for (const edit_entry_button of document.getElementsByName("edit-entry")) // DELETE ENTRY function delete_entry(e) { - fetch(`/api/guestbook/${e.target.form[0].value}`, {method:"DELETE"}) + fetch(`/api/guestbook/${get_field(e.target, "entry_id")}`, {method:"DELETE"}) .catch(e => console.log(e)) .then(() => window.location = "/guestbook/admin"); } @@ -30,8 +33,8 @@ for (const delete_entry_button of document.getElementsByName("delete-entry")) function edit_reply(e) { let data = new URLSearchParams(); - data.append("message", e.target.form[1].value); - fetch(`/api/guestbook/${e.target.form[0].value}/reply`, {method: "PATCH", body: data}) + data.append("message", get_field(e.target, "message")); + fetch(`/api/guestbook/${get_field(e.target, "entry_id")}/reply`, {method: "PATCH", body: data}) .catch(e => console.log(e)) .then(() => window.location = "/guestbook/admin"); } @@ -42,7 +45,7 @@ for (const edit_reply_button of document.getElementsByName("edit-reply")) // DELETE REPLY function delete_reply(e) { - fetch(`/api/guestbook/${e.target.form[0].value}/reply`, {method:"DELETE"}) + fetch(`/api/guestbook/${get_field(e.target, "entry_id")}/reply`, {method:"DELETE"}) .catch(e => console.log(e)) .then(() => window.location = "/guestbook/admin"); } @@ -54,8 +57,8 @@ for (const delete_reply_button of document.getElementsByName("delete-reply")) function send_reply(e) { let data = new URLSearchParams(); - data.append("message", e.target.form[1].value); - fetch(`/api/guestbook/${e.target.form[0].value}/reply`, {method: "PATCH", body: data}) + data.append("message", get_field(e.target, "message")); + fetch(`/api/guestbook/${get_field(e.target, "entry_id")}/reply`, {method: "PATCH", body: data}) .catch(e => console.log(e)) .then(() => window.location = "/guestbook/admin"); }