|
|
|
@ -1,18 +1,105 @@ |
|
|
|
|
// supprime un livre basé sur son ID
|
|
|
|
|
function deleteLivre(id) { |
|
|
|
|
fetch ('/livres/' + id, { |
|
|
|
|
method: 'DELETE', |
|
|
|
|
}).then(async (response) => { |
|
|
|
|
const tr = document.querySelector('tr[data-id="' + id + '"]'); |
|
|
|
|
const attributes = {}; |
|
|
|
|
tr.querySelectorAll("td").forEach((td) => { |
|
|
|
|
const name = td.dataset["name"]; |
|
|
|
|
const value = td.textContent; |
|
|
|
|
attributes[name] = value; |
|
|
|
|
}); |
|
|
|
|
let suppr = window.confirm('Êtes-vous sûr de vouloir supprimer le livre ' + attributes["titre"] + ' ?'); |
|
|
|
|
if (suppr) { |
|
|
|
|
fetch ('/livres/' + id, { |
|
|
|
|
method: 'DELETE', |
|
|
|
|
}).then(async (response) => { |
|
|
|
|
if (response.status === 200) { |
|
|
|
|
tr.parentNode.removeChild(tr); |
|
|
|
|
} else { |
|
|
|
|
const error = livre["error"]; |
|
|
|
|
alert(error); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// modifie un livre basé sur son ID
|
|
|
|
|
function editLivre(id) { |
|
|
|
|
const tr = document.querySelector('tr[data-id="' + id + '"]'); |
|
|
|
|
const attributes = {}; |
|
|
|
|
tr.querySelectorAll("td[data-name]").forEach((td) => { |
|
|
|
|
const name = td.dataset["name"]; |
|
|
|
|
const value = td.textContent; |
|
|
|
|
attributes[name] = value; |
|
|
|
|
td.removeAttribute("data-name"); |
|
|
|
|
td.textContent = ''; |
|
|
|
|
let input = document.createElement("input"); |
|
|
|
|
input.setAttribute("data-name", name); |
|
|
|
|
input.setAttribute("value", value); |
|
|
|
|
td.append(input); |
|
|
|
|
}); |
|
|
|
|
const btns = tr.querySelector('div.btn-group'); |
|
|
|
|
btns.querySelectorAll('a').forEach((a) => { |
|
|
|
|
console.info(a); |
|
|
|
|
if (a.dataset["action"] == "edit") { |
|
|
|
|
a.setAttribute("data-action", "save"); |
|
|
|
|
a.removeAttribute("onclick"); |
|
|
|
|
a.setAttribute("onclick", "saveLivre("+id+")"); |
|
|
|
|
a.textContent = "Sauver"; |
|
|
|
|
} |
|
|
|
|
if (a.dataset["action"] == "destroy") { |
|
|
|
|
a.setAttribute("data-action", "cancel"); |
|
|
|
|
a.removeAttribute("onclick"); |
|
|
|
|
a.setAttribute("onclick", "cancel("+id+")"); |
|
|
|
|
a.textContent = "Annuler"; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
console.info(tr); |
|
|
|
|
/*fetch ('/livres/' + id, { |
|
|
|
|
method: 'POST', |
|
|
|
|
headers: { |
|
|
|
|
'Accept': 'application/json', |
|
|
|
|
'Content-Type': 'application/json' |
|
|
|
|
}, |
|
|
|
|
body: JSON.stringify(attributes) |
|
|
|
|
})*/ |
|
|
|
|
/*.then(async (response) => { |
|
|
|
|
if (response.status === 200) { |
|
|
|
|
let tr = document.querySelector('tr[data-id="' + id + '"]'); |
|
|
|
|
tr.parentNode.removeChild(tr); |
|
|
|
|
} else { |
|
|
|
|
const error = livre["error"]; |
|
|
|
|
alert(error); |
|
|
|
|
} |
|
|
|
|
})*/; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// annuler la modification d’un livre
|
|
|
|
|
function cancel(id) { |
|
|
|
|
const tr = document.querySelector('tr[data-id="' + id + '"]'); |
|
|
|
|
const attributes = {}; |
|
|
|
|
tr.querySelectorAll("input[data-name]").forEach((input) => { |
|
|
|
|
const name = input.dataset["name"]; |
|
|
|
|
const value = input.value; |
|
|
|
|
const td = input.offsetParent; |
|
|
|
|
td.setAttribute("data-name", name); |
|
|
|
|
td.textContent = value; |
|
|
|
|
}); |
|
|
|
|
const btns = tr.querySelector('div.btn-group'); |
|
|
|
|
btns.querySelectorAll('a').forEach((a) => { |
|
|
|
|
if (a.dataset["action"] == "save") { |
|
|
|
|
a.setAttribute("data-action", "edit"); |
|
|
|
|
a.removeAttribute("onclick"); |
|
|
|
|
a.setAttribute("onclick", "editLivre("+id+")"); |
|
|
|
|
a.textContent = "Modifier"; |
|
|
|
|
} |
|
|
|
|
if (a.dataset["action"] == "cancel") { |
|
|
|
|
a.setAttribute("data-action", "destroy"); |
|
|
|
|
a.removeAttribute("onclick"); |
|
|
|
|
a.setAttribute("onclick", "deleteLivre("+id+")"); |
|
|
|
|
a.textContent = "Supprimer"; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ajoute un livre
|
|
|
|
|
document.addEventListener('DOMContentLoaded', async function () { |
|
|
|
|
const table = document.querySelector("table"); |
|
|
|
|
const tbody = table.querySelector("tbody"); |
|
|
|
@ -43,6 +130,7 @@ document.addEventListener('DOMContentLoaded', async function () { |
|
|
|
|
tbody.insertBefore(tr, tbody.firstChild); |
|
|
|
|
for (const name in attributes) { |
|
|
|
|
const td = document.createElement("td"); |
|
|
|
|
td.setAttribute("data-name", name); |
|
|
|
|
td.textContent = livre[name]; |
|
|
|
|
tr.appendChild(td); |
|
|
|
|
} |
|
|
|
|