Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New catalog filter updates work with back/forward buttons #998

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 51 additions & 49 deletions resources/js/components/catalog-new/ItemsFilterController.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import qs from 'qs'
import { useApiClient } from '../composables/useApiClient'
import { useTitleUpdater } from './useTitleUpdater'

function getParsedFilterFromUrl() {
function getQueryFromCurrentUrl() {
const parsedUrl = qs.parse(window.location.search, {
arrayFormat: 'bracket',
ignoreQueryPrefix: true,
Expand All @@ -12,6 +12,7 @@ function getParsedFilterFromUrl() {
const { sort, q } = parsedUrl || {}

return {
...EMPTY_QUERY,
...rest,
yearRange: (() => {
if (!date_earliest || !date_latest) {
Expand All @@ -25,6 +26,9 @@ function getParsedFilterFromUrl() {
}

function stringifyUrl({ url, params }) {
const searchParams = stringifyUrlQuery(params)
if (!searchParams) return url

return url + '?' + stringifyUrlQuery(params)
}

Expand Down Expand Up @@ -135,14 +139,21 @@ export default {
last_page: 1,
artworks_total: null,
aggregations: {},
query: { ...EMPTY_QUERY, ...getParsedFilterFromUrl() },
query: getQueryFromCurrentUrl(),
page: 1,
}
},
async created() {
created() {
this.fetchAggregations()
this.fetchArtworks({ replaceArtworks: true })
},
mounted() {
window.addEventListener('popstate', this.handleHistoryPopState)
window.history.scrollRestoration = 'manual' // Do not change scroll position on back/forward
},
beforeUnmount() {
window.removeEventListener('popstate', this.handleHistoryPopState)
},
computed: {
selectedOptionsAsLabels() {
return Object.entries(this.query)
Expand Down Expand Up @@ -176,86 +187,72 @@ export default {
},
},
methods: {
handleHistoryPopState() {
this.query = getQueryFromCurrentUrl()
},
hasFilterOptions(filterName) {
return (
this.query[filterName].length ||
(this.aggregations[filterName] && this.aggregations[filterName].length > 0)
)
},
clearFilterSelection(filterName) {
this.query = {
...this.query,
[filterName]: EMPTY_QUERY[filterName],
}
this.updateQuery({ [filterName]: EMPTY_QUERY[filterName] })
},
clearAllSelections() {
this.query = { ...EMPTY_QUERY }
this.replaceQuery(EMPTY_QUERY)
},
handleColorChange(color) {
this.query = {
...this.query,
color: color,
}
this.updateQuery({ color })
},
handleYearRangeChange(yearRangeValue) {
this.query = {
...this.query,
yearRange: yearRangeValue,
}
handleYearRangeChange(yearRange) {
this.updateQuery({ yearRange })
},
handleSortChange(sortValue) {
this.query = {
...this.query,
sort: sortValue,
}
handleSortChange(sort) {
this.updateQuery({ sort })
},
handleCheckboxChange(e) {
const { name, checked } = e.target

this.query = {
...this.query,
this.updateQuery({
[name]: checked || undefined,
}
})
},
removeSelection({ filterName: name, value }) {
if (SINGLE_ITEM_FILTERS.includes(name)) {
this.query = {
...this.query,
this.updateQuery({
[name]: EMPTY_QUERY[name],
}
})
return
}

this.query = {
...this.query,
this.updateQuery({
[name]: this.query[name].filter((v) => v !== value),
}
})
},
handleMultiSelectChange(name, e) {
const { checked, value } = e.target
if (checked) {
// Add to query
this.query = {
...this.query,
this.updateQuery({
[name]: [...this.query[name], value],
}
})
return
}
// Remove from query
this.query = {
...this.query,
this.updateQuery({
[name]: this.query[name].filter((v) => v !== value),
}
})
},
loadMore() {
this.page++
},
handleSelectRandomly() {
this.query = {
this.replaceQuery({
...EMPTY_QUERY,
sort: 'random',
has_image: true,
}
})
},
async fetchAggregations() {
try {
Expand Down Expand Up @@ -311,6 +308,21 @@ export default {
this.isFetchingArtworks = false
}
},
updateQuery(data, { replace = false } = {}) {
this.query = replace ? { ...data } : { ...this.query, ...data }

window.history.pushState(
JSON.parse(JSON.stringify(this.query)),
'', // unused param
stringifyUrl({
url: window.location.pathname,
params: { filter: { ...this.query } },
})
)
},
replaceQuery(data) {
this.updateQuery(data, { replace: true })
},
},
watch: {
page(newPage, oldPage) {
Expand All @@ -320,21 +332,11 @@ export default {
},
query(newQuery) {
const newParams = { filter: { ...newQuery } }
const newUrl = stringifyUrl({
url: window.location.pathname,
params: newParams,
})

this.page = 1
this.refreshTitle(stringifyUrlQuery(newParams))
this.fetchAggregations()
this.fetchArtworks({ append: false })

window.history.replaceState(
newUrl,
'', // unused param
newUrl
)
},
},
render() {
Expand Down
6 changes: 0 additions & 6 deletions resources/js/components/catalog-new/NewColorSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,6 @@ export default {
slider: VueSlider,
},
watch: {
defaultColor(newDefaultColor) {
this.hue = newDefaultColor ? tinycolor(newDefaultColor).toHsl()?.h : defaultHue
this.lightness = newDefaultColor
? tinycolor(newDefaultColor)?.toHsl()?.l
: defaultLightness
},
hue(newHue) {
this.immediateHue = newHue
this.$emit('change', this.color)
Expand Down