refactor: add stores and type

This commit is contained in:
Thanaphon Frappet 2025-03-17 15:34:19 +07:00
parent 0cc46735e0
commit 37f59b7dd5
4 changed files with 83 additions and 6 deletions

View file

@ -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>
<div></div>

View file

@ -14,7 +14,10 @@ import mditHighlight from 'markdown-it-highlightjs';
import { initLang, initTheme } from 'src/utils/ui';
import { baseUrl } from 'src/stores/utils';
import { useManualStore } from 'src/stores/manual';
const ROUTE = useRoute();
const manualStore = useManualStore();
const md = new MarkdownIt()
.use(mditAnchor)
@ -55,11 +58,10 @@ onUnmounted(() => {
async function getContent() {
if (!category.value || !page.value) return;
const res = await fetch(
`${baseUrl}/manual/${category.value}/page/${page.value}`,
);
const res = await manualStore.getManualByPage({
category: category.value,
pageName: page.value,
});
if (res && res.ok) {
const text = await res.text();
content.value = text;

View 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,
};
});

View file

@ -0,0 +1,12 @@
export type Manual = {
label: string;
labelEN: string;
category: string;
page: Page[];
};
type Page = {
namee: string;
label: string;
labelEN: string;
};