Skip to content

Commit

Permalink
fix: update category page title on category change #128
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Feb 6, 2024
1 parent b6d3bad commit e5d604e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions components/PoisList/PoisList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export default defineNuxtComponent({
categoryId() {
this.pois = undefined
// Send new categoryId to parent in order to update meta title
this.$emit('categoryUpdate', this.categoryId)
// Change history directly to avoid resetup the page with this.$router.push
history.pushState({}, '', `/category/${this.categoryId}`)
Expand Down
36 changes: 22 additions & 14 deletions pages/category/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { definePageMeta } from '#imports'
import PoisList from '~/components/PoisList/PoisList.vue'
import type { ContentEntry } from '~/lib/apiContent'
import { getContents } from '~/lib/apiContent'
import type { MenuItem } from '~/lib/apiMenu'
import type { ApiMenuCategory, MenuItem } from '~/lib/apiMenu'
import { getMenu } from '~/lib/apiMenu'
import type { ApiPois } from '~/lib/apiPois'
import { getPoiByCategoryId } from '~/lib/apiPois'
Expand Down Expand Up @@ -116,19 +116,6 @@ export default defineNuxtComponent({
if (this.menuItems)
menuStore().fetchConfig(this.menuItems)
// Fetching category by ID
// TODO: Has to be done in setup() but menuItems is touched in created() hook
const category = menuStore().getCurrentCategory(useRoute().params.id as string)
if (!category) {
throw createError({
statusCode: 404,
statusMessage: 'Category Not Found',
})
}
this.settings.themes[0].title = category.category.name
useHead(headerFromSettings(this.settings))
this.globalSettings = this.settings
this.globalContents = this.contents
this.globalTranslations = this.propertyTranslations
Expand All @@ -144,6 +131,26 @@ export default defineNuxtComponent({
mounted() {
this.locale = this.$i18n.locale
this.handleCategoryUpdate(useRoute().params.id as string)
},
methods: {
getCategory(categoryId: string): ApiMenuCategory {
// Fetching category by ID
// TODO: Has to be done in setup() but menuItems is touched in created() hook
const category = menuStore().getCurrentCategory(categoryId)
if (!category) {
throw createError({
statusCode: 404,
statusMessage: 'Category Not Found',
})
}
return category
},
handleCategoryUpdate(categoryId: number | string) {
const category = this.getCategory(categoryId.toString())
this.settings.themes[0].title = category.category.name
useHead(headerFromSettings(this.settings))
},
},
})
</script>
Expand All @@ -155,6 +162,7 @@ export default defineNuxtComponent({
:initial-category-id="parseInt(id)"
:initial-pois="pois"
class="page-index"
@category-update="handleCategoryUpdate"
/>
</template>

Expand Down

0 comments on commit e5d604e

Please sign in to comment.