Merge branch 'main' of http://work.gct.com.vn/minhnt/NSG_PORTAL_V2
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import type { Category } from "~/server/models/category";
|
||||
|
||||
export const useCategoryStore = defineStore("category-v2", () => {
|
||||
const categories = ref<Category[]>([]);
|
||||
|
||||
async function fetchCategories() {
|
||||
const { data, error } = await useFetch<Category[]>("/api/v2/categories");
|
||||
if (error.value) {
|
||||
return [] as Category[];
|
||||
}
|
||||
|
||||
categories.value = Object.assign([], data.value);
|
||||
|
||||
return categories.value;
|
||||
}
|
||||
|
||||
function findByCode(code?: string) {
|
||||
if (code) return categories.value.find((c) => c.code === code);
|
||||
}
|
||||
|
||||
function findById(id?: number) {
|
||||
return categories.value.find((c) => c.id === id);
|
||||
}
|
||||
|
||||
function findParents(category?: Category) {
|
||||
if (!category) return [];
|
||||
|
||||
const parents = [];
|
||||
let parent = findById(category.parentId);
|
||||
while (parent) {
|
||||
parents.push(parent);
|
||||
parent = findById(parent.parentId);
|
||||
}
|
||||
|
||||
return parents.reverse().concat(category);
|
||||
}
|
||||
|
||||
function findSubTree(category?: Category) {
|
||||
if (!category) return [];
|
||||
|
||||
let subTree = [] as Category[];
|
||||
|
||||
function findChildren(category: Category) {
|
||||
const children = categories.value.filter((c:Category) => c.parentId === category.id);
|
||||
if (children.length === 0) return;
|
||||
|
||||
subTree.push(...children,category);
|
||||
}
|
||||
|
||||
if(category.parentId === 41){
|
||||
findChildren(category);
|
||||
}else{
|
||||
const parent = findById(category.parentId);
|
||||
if(parent){
|
||||
findChildren(parent);
|
||||
}
|
||||
}
|
||||
|
||||
return subTree.reverse();
|
||||
}
|
||||
|
||||
function findChildren(category: Category) {
|
||||
const children = categories.value.filter((c:Category) => c.parentId === category.id);
|
||||
if (children.length === 0) return;
|
||||
else return [...children]
|
||||
}
|
||||
|
||||
return { categories, fetchCategories, findByCode, findById, findParents,findSubTree, findChildren };
|
||||
});
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useCategoryStore, import.meta.hot));
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineStore, acceptHMRUpdate } from "pinia";
|
||||
|
||||
export const useLayoutStore = defineStore("layout", () => {
|
||||
const megaMenuActive = ref<boolean>(false);
|
||||
function setStatus(status: boolean) {
|
||||
megaMenuActive.value = status;
|
||||
}
|
||||
return { megaMenuActive, setStatus };
|
||||
});
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useLayoutStore, import.meta.hot));
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
|
||||
export const useNavigationStoreV2 = defineStore('navigation-v2', () => {
|
||||
const topMenu = ref('')
|
||||
|
||||
async function fetchNavigation() {
|
||||
const {data, error } = await useFetch('/api/services/navigation')
|
||||
|
||||
if (error.value) {
|
||||
return ''
|
||||
}
|
||||
if(data.value) {
|
||||
topMenu.value = data.value
|
||||
}
|
||||
|
||||
return topMenu.value
|
||||
}
|
||||
|
||||
return {topMenu, fetchNavigation}
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useNavigationStoreV2, import.meta.hot))
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
|
||||
export const useWidgetsStore = defineStore('widgets', () => {
|
||||
const weather = ref<any>(null)
|
||||
const locations = ref(["Hanoi", "Ho Chi Minh City", "Huế", "Danang", "Hai Phong", "Nha Trang"])
|
||||
const selectedLocation = ref("Hanoi")
|
||||
|
||||
async function fetchWeatherByLocation(location?:string){
|
||||
try {
|
||||
if(!location){
|
||||
location = selectedLocation.value
|
||||
}
|
||||
const response = await $fetch(
|
||||
`https://api.weatherapi.com/v1/current.json?key=56e1a8576f0c482280d84625230905&q=${location}&aqi=yes`
|
||||
);
|
||||
weather.value = response;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
return {locations,selectedLocation,weather,fetchWeatherByLocation}
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useWidgetsStore, import.meta.hot))
|
||||
}
|
||||
Reference in New Issue
Block a user