Skip to content

Commit

Permalink
Merge pull request #2 from UCLALibrary/refactor_app_vue
Browse files Browse the repository at this point in the history
fix: Move pinia store update from app.vue to init.server plugin
  • Loading branch information
pghorpade authored Jun 27, 2024
2 parents 31a9b56 + 799a9e8 commit aa26d0b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
17 changes: 0 additions & 17 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@
const { enabled, state } = usePreviewMode()
const globalStore = useGlobalStore()
const { data, error } = await useFetch('/api/fetchLayout')
globalStore.header.primary = data.value.primary
globalStore.header.secondary = data.value.secondary
globalStore.footerPrimary.nodes = data.value.footerPrimary
globalStore.footerSock.nodes = data.value.footerSock
if (data.value) {
const globalData = removeEmpties(data.value?.globalSets || [])
// console.log("remove empties: " + JSON.stringify(globalData))
// Shape data from Craft
const craftData = Object.fromEntries(
globalData?.map(item => [item.handle, item])
)
globalStore.globals = craftData
}
</script>
<template>
<div>
Expand Down
31 changes: 31 additions & 0 deletions plugins/init.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default defineNuxtPlugin(async (nuxtApp) => {
const globalStore = useGlobalStore()

const { data, error } = await useFetch('/api/fetchLayout', {
key: 'layout'
})
if (error.value) {
throw createError({
...error.value, statusMessage: 'Error while fetching data.' + error.value, fatal: true
})
}
if (!data.value?.primary || !data.value?.secondary || !data.value?.footerPrimary || !data.value?.footerSock || !data.value?.globalSets) {
throw createError({
statusCode: 404,
statusMessage: 'Layout Data not found',
fatal: true
})
}
globalStore.header.primary = data.value.primary
globalStore.header.secondary = data.value.secondary
globalStore.footerPrimary.nodes = data.value.footerPrimary
globalStore.footerSock.nodes = data.value.footerSock

const globalData = removeEmpties(data.value?.globalSets || [])
// console.log("remove empties: " + JSON.stringify(globalData))
// Shape data from Craft
const craftData = Object.fromEntries(
globalData?.map(item => [item.handle, item])
)
globalStore.globals = craftData
})

0 comments on commit aa26d0b

Please sign in to comment.