update numbers after deletion

This commit is contained in:
Zhora Shalyapin 2025-01-09 13:49:27 +00:00
parent 49dc41ea41
commit 14ef4a91a0

69
main.js
View File

@ -16,6 +16,13 @@
(function () {
'use strict';
let fileInputs = {
"attachment1": "legend_photo",
"attachment4": "legend_files",
"attachment2": "admin_photo",
"attachment3": "admin_files"
}
class Property {
id;
name;
@ -90,6 +97,13 @@
css.rel = "stylesheet"
document.head.appendChild(css)
}
async function sendData(formData) {
return await fetch(`https://runcity.geo.rictum.ru/api/competitions/${window.location.pathname.split('/')[1]}/update`, {
method: 'POST',
body: formData
})
}
function makeDownloadLink(name, href = null) {
let downloadLink = document.createElement("a")
@ -102,9 +116,25 @@
return downloadLink
}
function addDisplayedFile(input, type, file, preview, removeLink) {
if (!input || !file || !removeLink) return
async function sendFileDeleted(inputName) {
let formData = new FormData()
let normalInputName = fileInputs[inputName]
formData.set(`cp[id]`, document.querySelector("input[name='cp[id]']").value)
formData.set(`cp[${normalInputName}]`, "-1")
let result = await sendData(formData)
try {
console.log(result.ok)
}
catch (e) {
console.log(e)
}
}
function addDisplayedFile(inputName, type, file, preview, removeLink) {
if (!inputName || !file || !removeLink) return
let input = document.querySelector(`input[name="${inputName}\[\]"]`)
let fileListContainer = input.parentElement.parentElement.querySelector(".file-list-container")
let fileContainer = document.createElement("div")
fileContainer.classList.add("file-container")
@ -139,10 +169,13 @@
deleteButton.type = "button"
deleteButton.classList.add("button-delete")
deleteButton.textContent = "x"
deleteButton.addEventListener("click", e => {
deleteButton.addEventListener("click", async e => {
if (!confirm("Точно?")) return
fetch(removeLink)
.then(res => fileListContainer.removeChild(fileContainer))
await fetch(removeLink)
await sendFileDeleted(inputName)
fileListContainer.removeChild(fileContainer)
})
fileButtonsContainer.appendChild(deleteButton)
@ -354,7 +387,7 @@
return
}
addDisplayedFile(document.querySelector(`input[name="${fileInput}\[\]"]`), type, fileUrl, preview, deleteUrl)
addDisplayedFile(fileInput, type, fileUrl, preview, deleteUrl)
}
}
}
@ -490,10 +523,7 @@
formData.set(`propname[${prop.name}]`, propName)
}
let result = await fetch(`https://runcity.geo.rictum.ru/api/competitions/${window.location.pathname.split('/')[1]}/update`, {
method: 'POST',
body: formData
})
let result = await sendData(formData)
try {
console.log(result.ok)
@ -510,6 +540,24 @@
}
}
function addUglyDeleteListener() {
let links = [...document.querySelectorAll("a[href*='del_stored']")]
for (const link of links) {
let shortHref = link.href.split("/").at(-1)
let fileInput = document.querySelector(`tr:has(a[href$="${shortHref}"]) ~ tr:has(input[type="file"]) input[type="file"]`)
link.addEventListener("click", async e => {
e.preventDefault()
if (!confirm("Точно?")) return
await sendFileDeleted(fileInput.name.split("[")[0])
location.href = link.href
})
}
}
let styles = `
#content-wrapper {
display: flex;
@ -884,6 +932,7 @@
topRowContentWrapper.appendChild(createAlwaysPrettifyInput(index))
})
addUglyDeleteListener()
createSendButtons()
/* OPTIONS */