2024-08-05 00:23:17 +00:00
|
|
|
import Stateful from "/script/silver.min.js"
|
|
|
|
|
|
|
|
const releaseID = document.getElementById("release").dataset.id;
|
2024-08-31 14:25:44 +00:00
|
|
|
const titleInput = document.getElementById("title");
|
|
|
|
const artworkImg = document.getElementById("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 visInput = document.getElementById("visibility");
|
|
|
|
const saveBtn = document.getElementById("save");
|
|
|
|
const deleteBtn = document.getElementById("delete");
|
|
|
|
|
|
|
|
var artworkData = artworkImg.attributes.src.value;
|
2024-08-31 00:30:30 +00:00
|
|
|
var edited = new Stateful(false);
|
2024-08-31 14:25:44 +00:00
|
|
|
var releaseData = updateData(undefined);
|
|
|
|
|
|
|
|
function updateData(old) {
|
|
|
|
var releaseData = {
|
|
|
|
visible: visInput.value === "true",
|
|
|
|
title: titleInput.value,
|
|
|
|
description: descInput.value,
|
|
|
|
type: typeInput.value,
|
|
|
|
releaseDate: dateInput.value,
|
|
|
|
artwork: artworkData,
|
|
|
|
buyname: buynameInput.value,
|
|
|
|
buylink: buylinkInput.value,
|
2024-08-05 00:23:17 +00:00
|
|
|
};
|
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
if (releaseData && releaseData != old) {
|
2024-08-05 00:23:17 +00:00
|
|
|
edited.set(true);
|
|
|
|
}
|
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
return releaseData;
|
2024-08-05 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
function saveRelease() {
|
|
|
|
console.table(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
fetch("/api/v1/music/" + releaseID, {
|
|
|
|
method: "PUT",
|
|
|
|
body: JSON.stringify(releaseData),
|
|
|
|
headers: { "Content-Type": "application/json" }
|
|
|
|
}).then(res => {
|
2024-08-05 00:23:17 +00:00
|
|
|
if (!res.ok) {
|
2024-08-31 14:25:44 +00:00
|
|
|
res.text().then(error => {
|
|
|
|
console.error(error);
|
|
|
|
alert("Failed to update release: " + error);
|
|
|
|
});
|
2024-08-05 00:23:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
location = location;
|
2024-08-31 14:25:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteRelease() {
|
|
|
|
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-08-05 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edited.onUpdate(edited => {
|
2024-08-31 14:25:44 +00:00
|
|
|
saveBtn.disabled = !edited;
|
2024-08-05 00:23:17 +00:00
|
|
|
})
|
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
titleInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
artworkImg.addEventListener("click", () => {
|
|
|
|
artworkInput.addEventListener("change", () => {
|
|
|
|
if (artworkInput.files.length > 0) {
|
2024-08-31 00:30:30 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = e => {
|
|
|
|
const data = e.target.result;
|
2024-08-31 14:25:44 +00:00
|
|
|
artworkImg.src = data;
|
|
|
|
artworkData = data;
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-31 00:30:30 +00:00
|
|
|
};
|
2024-08-31 14:25:44 +00:00
|
|
|
reader.readAsDataURL(artworkInput.files[0]);
|
2024-08-31 00:30:30 +00:00
|
|
|
}
|
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
artworkInput.click();
|
2024-08-31 00:30:30 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
typeInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
descInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
dateInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
buynameInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
buylinkInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
2024-08-31 14:25:44 +00:00
|
|
|
visInput.addEventListener("change", () => {
|
|
|
|
releaseData = updateData(releaseData);
|
2024-08-05 00:23:17 +00:00
|
|
|
});
|
|
|
|
|
2024-08-31 14:25:44 +00:00
|
|
|
saveBtn.addEventListener("click", () => {
|
2024-08-05 00:23:17 +00:00
|
|
|
if (!edited.get()) return;
|
2024-08-31 14:25:44 +00:00
|
|
|
saveRelease();
|
|
|
|
});
|
2024-08-05 00:23:17 +00:00
|
|
|
|
2024-08-31 14:25:44 +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;
|
|
|
|
deleteRelease();
|
|
|
|
});
|