schon/storefront/app/composables/posts/usePostBySlug.ts
2026-02-27 21:59:51 +03:00

36 lines
734 B
TypeScript

import { GET_POST_BY_SLUG } from '@graphql/queries/standalone/blog';
import type { IPostResponse } from '@types';
export async function usePostBySlug(slug: string) {
const { locale } = useI18n();
const variables = reactive({
slug: slug,
first: 1,
});
const { data, pending, error, refresh } = await useAsyncQuery<IPostResponse>(GET_POST_BY_SLUG, variables);
const post = computed(() => data.value?.posts.edges[0]?.node ?? null);
watch(locale, async () => {
if (variables.first >= 100) {
variables.first = 0;
} else {
variables.first += 1;
}
await refresh();
});
watch(error, (err) => {
if (err) {
console.error('usePostBySlug error:', err);
}
});
return {
post,
pending,
refresh,
};
}