Skip to content

Instantly share code, notes, and snippets.

@msheiko
Created July 10, 2024 07:05
Show Gist options
  • Save msheiko/a1bbb7df44db34a601377715d160dbe2 to your computer and use it in GitHub Desktop.
Save msheiko/a1bbb7df44db34a601377715d160dbe2 to your computer and use it in GitHub Desktop.
simple pinia store
import { defineStore } from "pinia";
import type { Ref } from "vue";
export const useMenuStore = defineStore("menu", () => {
const { $api } = useNuxtApp();
const catalog = ref(null);
const categories: Ref<{ [key: string]: any }> = ref({});
const fetchCatalogMenu = async () => {
const { data } = await useAsyncData("menu-catalog", () => $api.menu.fetchCategoryMenu());
catalog.value = data.value?.data;
};
const fetchCategoryById = async (categoryId: number | string, mobile: boolean = false) => {
if (!categories.value[categoryId as string]) {
try {
const response = await $api.menu.fetchCategoryMenuById(categoryId, mobile);
categories.value[categoryId as string] = response.data;
} catch (e) {
console.error("Ошибка получения пунктов меню каталога", e);
return;
}
}
return categories.value[categoryId as string];
};
return { catalog, categories, fetchCatalogMenu, fetchCategoryById };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment