switch_view_controller.js 631 B

1234567891011121314151617181920212223
  1. import { Controller } from '@hotwired/stimulus';
  2. import { visit } from '@hotwired/turbo';
  3. export default class extends Controller {
  4. static values = {
  5. url: { type: String, default: '/changeview' }
  6. }
  7. change(event) {
  8. event.preventDefault();
  9. fetch(this.urlValue)
  10. .then((response) => {
  11. if (!response.ok) {
  12. throw new Error(`Erreur Http : ${response.status}`);
  13. }
  14. return response.json();
  15. })
  16. .then(() => {
  17. visit(window.location.href, { action: 'replace' });
  18. })
  19. }
  20. }