arimelody.me/admin/static/edit-release.js

101 lines
3.4 KiB
JavaScript
Raw Normal View History

import Stateful from "/script/silver.min.js"
const releaseID = document.getElementById("release").dataset.id;
const titleInput = document.getElementById("title");
const artworkImg = document.getElementById("artwork");
2024-09-01 23:15:23 +00:00
const removeArtworkBtn = document.getElementById("remove-artwork");
const artworkInput = document.getElementById("artwork-file");
const typeInput = document.getElementById("type");
const descInput = document.getElementById("description");
const dateInput = document.getElementById("release-date");
const buynameInput = document.getElementById("buyname");
const buylinkInput = document.getElementById("buylink");
2024-09-01 23:15:23 +00:00
const copyrightInput = document.getElementById("copyright");
const copyrightURLInput = document.getElementById("copyright-url");
const visInput = document.getElementById("visibility");
const saveBtn = document.getElementById("save");
const deleteBtn = document.getElementById("delete");
var artworkData = artworkImg.attributes.src.value;
2024-09-01 23:15:23 +00:00
saveBtn.addEventListener("click", () => {
fetch("/api/v1/music/" + releaseID, {
method: "PUT",
2024-09-01 23:15:23 +00:00
body: JSON.stringify({
visible: visInput.value === "true",
title: titleInput.value,
description: descInput.value,
type: typeInput.value,
releaseDate: dateInput.value + ":00Z",
artwork: artworkData,
buyname: buynameInput.value,
buylink: buylinkInput.value,
copyright: copyrightInput.value,
copyrightURL: copyrightURLInput.value,
}),
headers: { "Content-Type": "application/json" }
}).then(res => {
if (!res.ok) {
res.text().then(error => {
console.error(error);
alert("Failed to update release: " + error);
});
return;
}
location = location;
});
2024-09-01 23:15:23 +00:00
});
2024-09-01 23:15:23 +00:00
deleteBtn.addEventListener("click", () => {
if (releaseID != prompt(
"You are about to permanently delete " + releaseID + ". " +
"This action is irreversible. " +
"Please enter \"" + releaseID + "\" to continue.")) return;
fetch("/api/v1/music/" + releaseID, {
method: "DELETE",
}).then(res => {
if (!res.ok) {
res.text().then(error => {
console.error(error);
alert("Failed to delete release: " + error);
});
return;
}
location = "/admin";
});
2024-09-01 23:15:23 +00:00
});
2024-09-01 23:15:23 +00:00
[titleInput, typeInput, descInput, dateInput, buynameInput, buylinkInput, copyrightInput, copyrightURLInput, visInput].forEach(input => {
input.addEventListener("change", () => {
saveBtn.disabled = false;
});
input.addEventListener("keypress", () => {
saveBtn.disabled = false;
});
});
2024-09-01 23:15:23 +00:00
artworkImg.addEventListener("click", () => {
artworkInput.addEventListener("change", () => {
if (artworkInput.files.length > 0) {
const reader = new FileReader();
reader.onload = e => {
const data = e.target.result;
artworkImg.src = data;
artworkData = data;
2024-09-01 23:15:23 +00:00
saveBtn.disabled = false;
};
reader.readAsDataURL(artworkInput.files[0]);
}
});
artworkInput.click();
});
2024-09-01 23:15:23 +00:00
removeArtworkBtn.addEventListener("click", () => {
artworkImg.src = "/img/default-cover-art.png"
artworkData = "";
saveBtn.disabled = false;
});