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

101 lines
3.4 KiB
JavaScript

import Stateful from "/script/silver.min.js"
const releaseID = document.getElementById("release").dataset.id;
const titleInput = document.getElementById("title");
const artworkImg = document.getElementById("artwork");
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");
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;
saveBtn.addEventListener("click", () => {
fetch("/api/v1/music/" + releaseID, {
method: "PUT",
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;
});
});
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";
});
});
[titleInput, typeInput, descInput, dateInput, buynameInput, buylinkInput, copyrightInput, copyrightURLInput, visInput].forEach(input => {
input.addEventListener("change", () => {
saveBtn.disabled = false;
});
input.addEventListener("keypress", () => {
saveBtn.disabled = false;
});
});
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;
saveBtn.disabled = false;
};
reader.readAsDataURL(artworkInput.files[0]);
}
});
artworkInput.click();
});
removeArtworkBtn.addEventListener("click", () => {
artworkImg.src = "/img/default-cover-art.png"
artworkData = "";
saveBtn.disabled = false;
});