Skip to content

Commit

Permalink
WPNUX-15 configurable menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
vernaillen committed Jan 15, 2024
1 parent 1ca70c8 commit 2a61b48
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default defineNuxtModule<ModuleOptions>({
})
logger.start('WPNuxt ::: Starting setup ::: ')
logger.info('Connecting GraphQL to', options.wordpressUrl)
logger.info('Configured frontendUrl:', nuxt.options.runtimeConfig.public.frontendSiteUrl)

logger.debug('Debug mode enabled')

const resolver = createResolver(import.meta.url)
Expand Down Expand Up @@ -108,6 +110,7 @@ export default defineNuxtModule<ModuleOptions>({

await installModule('@vueuse/nuxt', {})
await installModule('nuxt-multi-cache', {
debug: true,
route: {
enabled: true
},
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/composables/useMenu.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { ref, useNuxtData, useFetch, createError, useTokens } from "#imports"

const _useMenu = async () => {
const _useMenu = async (name?: string) => {
const menu = ref()
const cacheKey = 'menu'
const cacheKey = 'menu-' + name ? name : 'main'
const cachedMenu = useNuxtData(cacheKey.value)
const tokens = useTokens()

console.log('useMenu, slug: ', name)

if (cachedMenu.data.value) {
menu.value = cachedMenu.data.value
} else {
const { data, error } = await useFetch('/api/graphql_middleware/query/Menu', {
params: {
name: name
},
key: cacheKey,
headers: {
Authorization: tokens.authorizationHeader
Expand Down
13 changes: 6 additions & 7 deletions src/runtime/composables/usePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { ref, computed, useNuxtData, useTokens, useFetch, createError, useWPNuxt
const _usePageById = async (id: number, asPreview?: boolean) => {
const logger = useWPNuxtLogger()
const page = ref()
//const cacheKey = computed(() => `page-${id}`)
const cacheKey = computed(() => `page-${id}`)

const tokens = useTokens()
//const cachedPage = useNuxtData(cacheKey.value)
const cachedPage = useNuxtData(cacheKey.value)

/*if (cachedPage.data.value) {
if (cachedPage.data.value) {
page.value = cachedPage.data.value
} else {*/
} else {
const { data, pending, refresh, error } = await useFetch("/api/graphql_middleware/query/PageById/", {
//key: cacheKey.value,
key: cacheKey.value,
params: {
id: id,
asPreview: asPreview
Expand All @@ -28,9 +28,8 @@ const _usePageById = async (id: number, asPreview?: boolean) => {
logger.error('usePageById, error: ', error.value)
throw createError({ statusCode: 500, message: 'Error fetching PageById', fatal: true })
}
logger.debug('usePageById: fetched page from WordPress: ', data.value.title)
page.value = data.value
//}
}
return {
data: page.value
}
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/queries/wp.gql
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ query Settings {
timeFormat
}
}
query Menu($id: ID! = "dGVybTo0") {
menu(id: $id) {
query Menu($name: ID! = "main", $idType: MenuNodeIdTypeEnum! = NAME) {
menu(id: $name, idType: $idType) {
id
name
menuItems {
nodes {
label
Expand Down

0 comments on commit 2a61b48

Please sign in to comment.