Skip to content

Commit aa26d0b

Browse files
authored
Merge pull request #2 from UCLALibrary/refactor_app_vue
fix: Move pinia store update from app.vue to init.server plugin
2 parents 31a9b56 + 799a9e8 commit aa26d0b

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Diff for: app.vue

-17
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,6 @@
22
33
const { enabled, state } = usePreviewMode()
44
5-
const globalStore = useGlobalStore()
6-
7-
const { data, error } = await useFetch('/api/fetchLayout')
8-
globalStore.header.primary = data.value.primary
9-
globalStore.header.secondary = data.value.secondary
10-
globalStore.footerPrimary.nodes = data.value.footerPrimary
11-
globalStore.footerSock.nodes = data.value.footerSock
12-
if (data.value) {
13-
const globalData = removeEmpties(data.value?.globalSets || [])
14-
// console.log("remove empties: " + JSON.stringify(globalData))
15-
// Shape data from Craft
16-
const craftData = Object.fromEntries(
17-
globalData?.map(item => [item.handle, item])
18-
)
19-
globalStore.globals = craftData
20-
}
21-
225
</script>
236
<template>
247
<div>

Diff for: plugins/init.server.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default defineNuxtPlugin(async (nuxtApp) => {
2+
const globalStore = useGlobalStore()
3+
4+
const { data, error } = await useFetch('/api/fetchLayout', {
5+
key: 'layout'
6+
})
7+
if (error.value) {
8+
throw createError({
9+
...error.value, statusMessage: 'Error while fetching data.' + error.value, fatal: true
10+
})
11+
}
12+
if (!data.value?.primary || !data.value?.secondary || !data.value?.footerPrimary || !data.value?.footerSock || !data.value?.globalSets) {
13+
throw createError({
14+
statusCode: 404,
15+
statusMessage: 'Layout Data not found',
16+
fatal: true
17+
})
18+
}
19+
globalStore.header.primary = data.value.primary
20+
globalStore.header.secondary = data.value.secondary
21+
globalStore.footerPrimary.nodes = data.value.footerPrimary
22+
globalStore.footerSock.nodes = data.value.footerSock
23+
24+
const globalData = removeEmpties(data.value?.globalSets || [])
25+
// console.log("remove empties: " + JSON.stringify(globalData))
26+
// Shape data from Craft
27+
const craftData = Object.fromEntries(
28+
globalData?.map(item => [item.handle, item])
29+
)
30+
globalStore.globals = craftData
31+
})

0 commit comments

Comments
 (0)