Skip to content

Commit

Permalink
list add category name in page title #128
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab authored Feb 6, 2024
1 parent a6cd623 commit 652c100
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 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
25 changes: 22 additions & 3 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 @@ -90,8 +90,6 @@ export default defineNuxtComponent({
fetchPoiByCategoryId,
])
useHead(headerFromSettings(settings.value))
return {
config,
settings,
Expand Down Expand Up @@ -133,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 @@ -144,6 +162,7 @@ export default defineNuxtComponent({
:initial-category-id="parseInt(id)"
:initial-pois="pois"
class="page-index"
@category-update="handleCategoryUpdate"
/>
</template>

Expand Down
10 changes: 10 additions & 0 deletions stores/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ export const menuStore = defineStore('menu', {
) as ApiMenuCategory[])
},

getCurrentCategory: (state: State): (categoryId: string) => ApiMenuCategory | undefined => {
return (categoryId) => {
return state.menuItems === undefined
? undefined
: Object.values(state.menuItems).find(
menuItem => menuItem.id.toString() === categoryId,
) as ApiMenuCategory
}
},

selectedCategories: (state: State): ApiMenuCategory[] | undefined => {
return state.menuItems === undefined
? undefined
Expand Down

0 comments on commit 652c100

Please sign in to comment.