12345678910111213141516171819202122232425262728293031323334353637383940 |
- export const switchEtat = (selector) => {
- const buttons = document.querySelectorAll(selector);
- /** @type (Element) */
- for (const button of buttons) {
- button.addEventListener("click", (event) => {
- event.preventDefault();
- reqSwitch(button);
- })
- }
- function reqSwitch(button) {
- const icone = button.querySelector('i');
- const id = button.dataset.content;
- const state1 = button.dataset.iconeActif;
- const state2 = button.dataset.iconeInactif;
- const path = button.dataset.path;
- const req = fetch(
- path, {
- method: "PATCH",
- body: JSON.stringify({"id_film": id})
- }
- );
- req
- .then((response) => {
- if (!response.ok) {
- throw new Error(`Erreur Http ${response.status}`);
- }
- return response.json();
- })
- .then((json) => {
- if (icone.className === state1) {
- icone.className = state2;
- } else {
- icone.className = state1;
- }
- })
- }
- }
|