document.querySelectorAll('aside#actions #create') .forEach(elt => elt.onclick = () => { const url = `${topSection}/${uuid()}` fetch(`${document.body.dataset.editHost}${url}.create`, { method: 'PUT', body: topSection }) .then(() => sleep(1)) .then(() => window.location.assign('/' + url)) .catch(error => console.error(`Error creating #{topSection}`, error)) }) document.querySelectorAll('aside#actions #delete') .forEach(elt => elt.onclick = () => { if (confirm(elt.dataset.confirm)) fetch(document.body.dataset.editUrl.replace('/_index', '.delete'), { method: 'PUT' }) .then(() => sleep(1)) .then(() => window.location.assign('/' + topSection)) .catch(error => console.error(`Error deleting #{topSection}`, error)) }) document.querySelectorAll('aside#actions input#file') .forEach(elt => elt.onchange = () => [...elt.files].forEach(file => fetch(document.body.dataset.editUrl.replace('_index', file.name), { method: 'PUT', body: file }) .then(() => sleep(1)) .then(() => window.location.reload()) .catch(error => console.error(`Error uploading #{elt.files[0].name}`, error)))) document.querySelectorAll('.delete_image') .forEach(elt => elt.onclick = () => fetch(document.body.dataset.editHost + elt.dataset.target, { method: 'DELETE' }) .finally(() => elt.parentNode.remove()) .catch(error => console.error(`Error deleting file`, error)))