switchViewVanilla.js 654 B

123456789101112131415161718192021222324
  1. export function switchView(idSelector) {
  2. const button = document.querySelector(idSelector);
  3. if (!button) {
  4. return
  5. }
  6. button.addEventListener("click", (event) => {
  7. event.preventDefault();
  8. change();
  9. })
  10. function change() {
  11. const fetchPromise = fetch("/changeview");
  12. fetchPromise
  13. .then((response) => {
  14. if (!response.ok) {
  15. throw new Error(`Erreur Http : ${response.status}`);
  16. }
  17. return response.json();
  18. })
  19. .then((json) => {
  20. window.location.reload();
  21. })
  22. }
  23. }