Files
fizzy/app/javascript/controllers/turbo_navigation_controller.js
T
Jorge Manrubia 0347806ce1 Fix: Keep the previous filtering when navigating back from cards
https://fizzy.37signals.com/5986089/cards/1760/edit

This also adds a proper resource for filtered collections, instead of changing the URL to cards for filtering cards.
2025-09-30 15:48:56 +02:00

26 lines
639 B
JavaScript

import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
rememberLocation() {
sessionStorage.setItem("referrerUrl", window.location.href)
}
backIfSamePath(event) {
const link = event.target.closest("a")
const targetUrl = new URL(link.href)
if (this.#referrerPath && targetUrl.pathname === this.#referrerPath) {
event.preventDefault()
Turbo.visit(this.#referrerUrl)
}
}
get #referrerPath() {
if (!this.#referrerUrl) return null
return new URL(this.#referrerUrl).pathname
}
get #referrerUrl() {
return sessionStorage.getItem("referrerUrl")
}
}