Skip to content

Commit

Permalink
feat: init store
Browse files Browse the repository at this point in the history
  • Loading branch information
batleforc committed Aug 24, 2024
1 parent a39c1bb commit 0463b01
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
37 changes: 37 additions & 0 deletions apps/front/src/stores/doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DocCategory, getDocSidebar } from '@portfolio/api-client';
import { defineStore } from 'pinia';

export interface DocState {
inited: boolean;
docLoading: boolean;
loadingError?: string;
docContent?: DocCategory;
}

export const useDocStore = defineStore({
id: 'doc',
state: (): DocState => ({
inited: false,
docLoading: false,
}),
actions: {
init() {
if (this.inited) return;
this.docLoading = true;
getDocSidebar()
.then((body) => {
if (body.status === 200) {
this.docContent = body.data;
}
})
.catch((err) => {
console.error(err);
this.loadingError = 'Failed to load doc sidebar';
})
.finally(() => {
this.docLoading = false;
this.inited = true;
});
},
},
});
5 changes: 5 additions & 0 deletions apps/front/src/views/DocHomeView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script setup lang="ts">
import { useDocStore } from '../stores/doc';
const docStore = useDocStore();
if (!docStore.inited && !docStore.docLoading) {
docStore.init();
}
</script>

<template>
Expand Down
15 changes: 13 additions & 2 deletions apps/front/src/views/DocView.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<script setup lang="ts">
import { useDocStore } from '../stores/doc';
const docStore = useDocStore();
if (!docStore.inited && !docStore.docLoading) {
docStore.init();
}
</script>

<template>
<div id="title" class="container">
<div id="title" class="container" v-if="docStore.inited">
<h1>Doc sub page</h1>
<p>{{ $route.params.page }}</p>
<p>{{ $route.params.page.join("/") }}</p>
<div>
<p v-for="sidebar in docStore.docContent.sub_categories" :key="sidebar.name">
{{ sidebar.name }}
</p>
</div>
</div>
</template>

0 comments on commit 0463b01

Please sign in to comment.