Skip to content

Commit

Permalink
Manage filter query parameters with push state
Browse files Browse the repository at this point in the history
For comparison [see this commit][commit]

There is a slight difference here on how to get this functionality to work. In
the Stimulus version, its a change to the `window.history` when a specific filter
is selected. In the Superglue version, we have to copy the current state to the
next url, modify the maximum on that copy, then call a utility function
that gets passed to your component, `navigateTo`.

While the Superglue version is a bit lengthier, it makes "undoing" this action
easy, as `history.back` would properly revert that state.

[commit]: seanpdoyle@6236e5b
  • Loading branch information
jho406 committed Oct 8, 2023
1 parent 0aee530 commit 6aad18d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/views/seats/index.js
Expand Up @@ -8,6 +8,7 @@ import SeatingLegend from '../../components/SeatingLegend'
import FloorSwitcher from '../../components/FloorSwitcher'
import Layout from '../../components/Layout'
import { pagesSlice } from '../../javascript/slices/pages'
import { urlToPageKey } from '@thoughtbot/superglue'

const { setMaximum } = pagesSlice.actions

Expand All @@ -21,13 +22,29 @@ export default (props) => {
floors,
filters,
pageKey,
copyPage,
navigateTo,
} = props

const dispatch = useDispatch()

const handleFilter = (event, maximum) => {
dispatch(setMaximum({pageKey, maximum}))
event.stopPropagation()
const nextUrl = new URL(document.location)

if (maximum !== Infinity) {
nextUrl.searchParams.set("maximum", maximum)
const nextPageKey = urlToPageKey(nextUrl.href)
copyPage({from: pageKey, to: nextUrl.href})
dispatch(setMaximum({pageKey: nextPageKey, maximum}))

navigateTo(nextUrl.href)
} else {
nextUrl.searchParams.delete("maximum")
navigateTo(pageKey, {
action: 'replace'
})
}
}

return (
Expand Down

0 comments on commit 6aad18d

Please sign in to comment.