Skip to content

Commit

Permalink
feat: update URL when usering filter & order_by queries (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
dq18 committed Feb 17, 2024
1 parent f29ad24 commit 4a5b9f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/constants.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/views/BrandDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export default {
// filter & order
productFilter: '',
productFilterList: constants.PRODUCT_FILTER_LIST,
productOrder: '-unique_scans_n',
productOrderList: constants.PRODUCT_ORDER_LIST,
productOrder: constants.PRODUCT_ORDER_BY_LIST[1].key,
productOrderList: constants.PRODUCT_ORDER_BY_LIST,
// data
brand: null, // see init
brandProductList: [],
Expand All @@ -107,6 +107,8 @@ export default {
},
},
mounted() {
this.productFilter = this.$route.query[constants.FILTER_PARAM] || this.productFilter
this.productOrder = this.$route.query[constants.ORDER_BY_PARAM] || this.productOrder
this.initBrand()
},
methods: {
Expand All @@ -128,12 +130,14 @@ export default {
},
toggleProductFilter(filterKey) {
this.productFilter = this.productFilter ? '' : filterKey
this.initBrand()
this.$router.push({ query: { ...this.$route.query, [constants.FILTER_PARAM]: this.productFilter } })
// this.initBrand() will be called in watch $route
},
selectProductOrder(orderKey) {
if (this.productOrder !== orderKey) {
this.productOrder = orderKey
this.initBrand()
this.$router.push({ query: { ...this.$route.query, [constants.ORDER_BY_PARAM]: this.productOrder } })
// this.initBrand() will be called in watch $route
}
}
},
Expand Down
17 changes: 14 additions & 3 deletions src/views/ProductList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export default {
// filter & order
productFilter: '',
productFilterList: constants.PRODUCT_FILTER_LIST,
productOrder: '-unique_scans_n',
productOrderList: constants.PRODUCT_ORDER_LIST,
productOrder: constants.PRODUCT_ORDER_BY_LIST[1].key,
productOrderList: constants.PRODUCT_ORDER_BY_LIST,
// data
productList: [],
productTotal: null,
Expand All @@ -83,6 +83,8 @@ export default {
},
},
mounted() {
this.productFilter = this.$route.query[constants.FILTER_PARAM] || this.productFilter
this.productOrder = this.$route.query[constants.ORDER_BY_PARAM] || this.productOrder
this.initProductList()
},
methods: {
Expand All @@ -103,11 +105,20 @@ export default {
},
toggleProductFilter(filterKey) {
this.productFilter = this.productFilter ? '' : filterKey
this.initProductList()
this.$router.push({ query: { ...this.$route.query, [constants.FILTER_PARAM]: this.productFilter } })
// this.initProductList() will be called in watch $route
},
selectProductOrder(orderKey) {
if (this.productOrder !== orderKey) {
this.productOrder = orderKey
this.$router.push({ query: { ...this.$route.query, [constants.ORDER_BY_PARAM]: this.productOrder } })
// this.initProductList() will be called in watch $route
}
}
},
watch: {
$route (newRoute, oldRoute) {
if (oldRoute.fullPath !== newRoute.fullPath) {
this.initProductList()
}
}
Expand Down

0 comments on commit 4a5b9f8

Please sign in to comment.