URL Handling

This commit is contained in:
Jai A
2020-09-16 22:35:58 -07:00
parent eb78e3d640
commit aa8215df34

View File

@@ -395,16 +395,22 @@ export default {
results: [], results: [],
pages: [], pages: [],
currentPage: 1, currentPage: 1,
overrideOffset: 0,
sortType: 'relevance', sortType: 'relevance',
maxResults: 6, maxResults: 6,
} }
}, },
async mounted() { async mounted() {
// if (this.$route.query.query) this.query = this.$route.query.query if (this.$route.query.q) this.query = this.$route.query.q
// if (this.$route.query.facets) this.facets = this.$route.query.facets[0] if (this.$route.query.f) {
// if (this.$route.query.sortType) this.sortType = this.$route.query.sortType const facets = this.$route.query.f.split(',')
//
// console.log(this.facets) for (const facet of facets) await this.toggleFacet(facet, false)
}
if (this.$route.query.v)
this.selectedVersions = this.$route.query.v.split(',')
if (this.$route.query.s) this.sortType = this.$route.query.s
if (this.$route.query.o) this.overrideOffset = this.$route.query.o
await this.fillInitialVersions() await this.fillInitialVersions()
@@ -445,7 +451,7 @@ export default {
} }
}, },
async clearFilters() { async clearFilters() {
for (const facet of this.facets) await this.toggleFacet(facet, false) for (const facet of [...this.facets]) await this.toggleFacet(facet, true)
this.selectedVersions = [] this.selectedVersions = []
await this.onSearchChange(1) await this.onSearchChange(1)
@@ -494,8 +500,13 @@ export default {
params.push(`facets=${JSON.stringify(formattedFacets)}`) params.push(`facets=${JSON.stringify(formattedFacets)}`)
} }
if (newPageNumber !== 1) { const offset = (newPageNumber - 1) * this.maxResults
params.push(`offset=${(newPageNumber - 1) * this.maxResults}`) if (this.overrideOffset > 0) {
console.log(this.overrideOffset)
params.push(`offset=${this.overrideOffset}`)
this.overrideOffset = 0
} else if (newPageNumber !== 1) {
params.push(`offset=${offset}`)
} }
let url = 'https://api.modrinth.com/api/v1/mod' let url = 'https://api.modrinth.com/api/v1/mod'
@@ -535,6 +546,16 @@ export default {
} else { } else {
this.pages = Array.from({ length: pageAmount }, (_, i) => i + 1) this.pages = Array.from({ length: pageAmount }, (_, i) => i + 1)
} }
url = `mods?q=${encodeURIComponent(
this.query
)}&o=${offset}&f=${encodeURIComponent(
this.facets.toString()
)}&v=${encodeURIComponent(
this.selectedVersions.toString()
)}&s=${encodeURIComponent(this.sortType)}`
window.history.pushState(new Date(), 'Mods', url)
} catch (err) { } catch (err) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(err) console.error(err)