refactor: add stores and type
This commit is contained in:
parent
0cc46735e0
commit
37f59b7dd5
4 changed files with 83 additions and 6 deletions
|
|
@ -1,4 +1,27 @@
|
||||||
<script setup lang="ts"></script>
|
<script setup lang="ts">
|
||||||
|
// NOTE: Library
|
||||||
|
import { storeToRefs } from 'pinia';
|
||||||
|
|
||||||
|
// NOTE: Components
|
||||||
|
|
||||||
|
// NOTE: Stores & Type
|
||||||
|
import { useManualStore } from 'src/stores/manual';
|
||||||
|
import { onMounted } from 'vue';
|
||||||
|
|
||||||
|
// NOTE: Variable
|
||||||
|
const manualStore = useManualStore();
|
||||||
|
|
||||||
|
const { dataManual } = storeToRefs(manualStore);
|
||||||
|
|
||||||
|
async function fatchManual() {
|
||||||
|
const res = await manualStore.getManual();
|
||||||
|
dataManual.value = res ? res : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
await fatchManual();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div></div>
|
<div></div>
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,10 @@ import mditHighlight from 'markdown-it-highlightjs';
|
||||||
import { initLang, initTheme } from 'src/utils/ui';
|
import { initLang, initTheme } from 'src/utils/ui';
|
||||||
import { baseUrl } from 'src/stores/utils';
|
import { baseUrl } from 'src/stores/utils';
|
||||||
|
|
||||||
|
import { useManualStore } from 'src/stores/manual';
|
||||||
|
|
||||||
const ROUTE = useRoute();
|
const ROUTE = useRoute();
|
||||||
|
const manualStore = useManualStore();
|
||||||
|
|
||||||
const md = new MarkdownIt()
|
const md = new MarkdownIt()
|
||||||
.use(mditAnchor)
|
.use(mditAnchor)
|
||||||
|
|
@ -55,11 +58,10 @@ onUnmounted(() => {
|
||||||
|
|
||||||
async function getContent() {
|
async function getContent() {
|
||||||
if (!category.value || !page.value) return;
|
if (!category.value || !page.value) return;
|
||||||
|
const res = await manualStore.getManualByPage({
|
||||||
const res = await fetch(
|
category: category.value,
|
||||||
`${baseUrl}/manual/${category.value}/page/${page.value}`,
|
pageName: page.value,
|
||||||
);
|
});
|
||||||
|
|
||||||
if (res && res.ok) {
|
if (res && res.ok) {
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
content.value = text;
|
content.value = text;
|
||||||
|
|
|
||||||
40
src/stores/manual/index.ts
Normal file
40
src/stores/manual/index.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import { defineStore } from 'pinia';
|
||||||
|
import { api } from 'src/boot/axios';
|
||||||
|
import { getToken } from 'src/services/keycloak';
|
||||||
|
import { Manual } from './types';
|
||||||
|
|
||||||
|
const ENDPOINT = 'manual';
|
||||||
|
const baseUrl = '/api/v1';
|
||||||
|
|
||||||
|
export async function getManual() {
|
||||||
|
const res = await api.get<Manual[]>(`/${ENDPOINT}`);
|
||||||
|
if (res.status < 400) {
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getManualByPage(opt: {
|
||||||
|
category: string;
|
||||||
|
pageName: string;
|
||||||
|
}) {
|
||||||
|
const res = await fetch(
|
||||||
|
`${baseUrl}/${ENDPOINT}/${opt.category}/page/${opt.pageName}`,
|
||||||
|
);
|
||||||
|
if (res.status < 400) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useManualStore = defineStore('manual-store', () => {
|
||||||
|
const dataManual = ref<Manual[]>([]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
getManual,
|
||||||
|
getManualByPage,
|
||||||
|
|
||||||
|
dataManual,
|
||||||
|
};
|
||||||
|
});
|
||||||
12
src/stores/manual/types.ts
Normal file
12
src/stores/manual/types.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
export type Manual = {
|
||||||
|
label: string;
|
||||||
|
labelEN: string;
|
||||||
|
category: string;
|
||||||
|
page: Page[];
|
||||||
|
};
|
||||||
|
|
||||||
|
type Page = {
|
||||||
|
namee: string;
|
||||||
|
label: string;
|
||||||
|
labelEN: string;
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue