1
0

Fixed and completed guestbook_admin.js.

This commit is contained in:
Alexander Andreev 2023-05-09 23:38:49 +04:00
parent 2f6bff16b9
commit 1011740ce4
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -1,13 +1,16 @@
function get_entry_id(button) { function get_field(e, el) {
return button.form.children[0].value return e.form.elements[el].value
} }
// EDIT ENTRY // EDIT ENTRY
function edit_entry(e) { function edit_entry(e) {
let data = new URLSearchParams(); let data = new URLSearchParams();
data.append("message", e.target.form[1].value); data.append("message", get_field(e.target, "message"));
fetch(`/api/guestbook/${e.target.form[0].value}`, {method: "PATCH", body: data}) 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)) .catch(e => console.log(e))
.then(() => window.location = "/guestbook/admin"); .then(() => window.location = "/guestbook/admin");
} }
@ -18,7 +21,7 @@ for (const edit_entry_button of document.getElementsByName("edit-entry"))
// DELETE ENTRY // DELETE ENTRY
function delete_entry(e) { 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)) .catch(e => console.log(e))
.then(() => window.location = "/guestbook/admin"); .then(() => window.location = "/guestbook/admin");
} }
@ -30,8 +33,8 @@ for (const delete_entry_button of document.getElementsByName("delete-entry"))
function edit_reply(e) { function edit_reply(e) {
let data = new URLSearchParams(); let data = new URLSearchParams();
data.append("message", e.target.form[1].value); data.append("message", get_field(e.target, "message"));
fetch(`/api/guestbook/${e.target.form[0].value}/reply`, {method: "PATCH", body: data}) fetch(`/api/guestbook/${get_field(e.target, "entry_id")}/reply`, {method: "PATCH", body: data})
.catch(e => console.log(e)) .catch(e => console.log(e))
.then(() => window.location = "/guestbook/admin"); .then(() => window.location = "/guestbook/admin");
} }
@ -42,7 +45,7 @@ for (const edit_reply_button of document.getElementsByName("edit-reply"))
// DELETE REPLY // DELETE REPLY
function delete_reply(e) { 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)) .catch(e => console.log(e))
.then(() => window.location = "/guestbook/admin"); .then(() => window.location = "/guestbook/admin");
} }
@ -54,8 +57,8 @@ for (const delete_reply_button of document.getElementsByName("delete-reply"))
function send_reply(e) { function send_reply(e) {
let data = new URLSearchParams(); let data = new URLSearchParams();
data.append("message", e.target.form[1].value); data.append("message", get_field(e.target, "message"));
fetch(`/api/guestbook/${e.target.form[0].value}/reply`, {method: "PATCH", body: data}) fetch(`/api/guestbook/${get_field(e.target, "entry_id")}/reply`, {method: "PATCH", body: data})
.catch(e => console.log(e)) .catch(e => console.log(e))
.then(() => window.location = "/guestbook/admin"); .then(() => window.location = "/guestbook/admin");
} }