mirror of
https://github.com/robonen/lorem-blog.git
synced 2026-03-20 02:44:39 +00:00
refactor(blog): integrate useAsyncState for post loading and error handling
This commit is contained in:
@@ -41,6 +41,8 @@ describe('blog Context', () => {
|
|||||||
const context = useProvidingPosts(mockLoader);
|
const context = useProvidingPosts(mockLoader);
|
||||||
|
|
||||||
expect(context.loading.value).toBe(true);
|
expect(context.loading.value).toBe(true);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
expect(mockLoader).toHaveBeenCalled();
|
expect(mockLoader).toHaveBeenCalled();
|
||||||
@@ -115,6 +117,8 @@ describe('blog Context', () => {
|
|||||||
|
|
||||||
it('should return available tags from all posts', async () => {
|
it('should return available tags from all posts', async () => {
|
||||||
const context = useProvidingPosts(mockLoader);
|
const context = useProvidingPosts(mockLoader);
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
const expectedTags = ['vue', 'javascript', 'frontend', 'typescript', 'react', 'comparison'];
|
const expectedTags = ['vue', 'javascript', 'frontend', 'typescript', 'react', 'comparison'];
|
||||||
@@ -158,6 +162,7 @@ describe('blog Context', () => {
|
|||||||
|
|
||||||
const context = useProvidingPosts(errorLoader);
|
const context = useProvidingPosts(errorLoader);
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to load posts:', expect.any(Error));
|
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to load posts:', expect.any(Error));
|
||||||
expect(context.loading.value).toBe(false);
|
expect(context.loading.value).toBe(false);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { PostComment } from '@/entities/Comment';
|
import type { PostComment } from '@/entities/Comment';
|
||||||
import type { Post } from '@/entities/Post';
|
import type { Post } from '@/entities/Post';
|
||||||
import { useInjectionStore } from '@robonen/vue';
|
import { useAsyncState, useInjectionStore } from '@robonen/vue';
|
||||||
import { computed, reactive, ref, shallowRef } from 'vue';
|
import { computed, reactive, shallowRef } from 'vue';
|
||||||
import { kmpSearch } from '@/shared/utils';
|
import { kmpSearch } from '@/shared/utils';
|
||||||
|
|
||||||
type PostItem = Post & {
|
type PostItem = Post & {
|
||||||
@@ -16,8 +16,22 @@ export const {
|
|||||||
useProvidingState: useProvidingPosts,
|
useProvidingState: useProvidingPosts,
|
||||||
useInjectedState: useInjectedPosts,
|
useInjectedState: useInjectedPosts,
|
||||||
} = useInjectionStore((loader: () => Promise<any>) => {
|
} = useInjectionStore((loader: () => Promise<any>) => {
|
||||||
const posts = shallowRef<PostItem[]>([]);
|
const {
|
||||||
const loading = ref(false);
|
state: posts,
|
||||||
|
isLoading: loading,
|
||||||
|
executeImmediately: loadPosts,
|
||||||
|
} = useAsyncState<PostItem[]>(
|
||||||
|
async () => {
|
||||||
|
const response = await loader();
|
||||||
|
return response.data as PostItem[];
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
{
|
||||||
|
onError(error) {
|
||||||
|
console.error('Failed to load posts:', error);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const selectedPost = shallowRef<PostItem | null>(null);
|
const selectedPost = shallowRef<PostItem | null>(null);
|
||||||
|
|
||||||
@@ -54,21 +68,6 @@ export const {
|
|||||||
return Array.from(tagsSet);
|
return Array.from(tagsSet);
|
||||||
});
|
});
|
||||||
|
|
||||||
const loadPosts = async () => {
|
|
||||||
loading.value = true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await loader();
|
|
||||||
posts.value = data!.data as PostItem[];
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error('Failed to load posts:', error);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
loading.value = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function toggleTag(tag: string) {
|
function toggleTag(tag: string) {
|
||||||
if (filters.tags.has(tag))
|
if (filters.tags.has(tag))
|
||||||
filters.tags.delete(tag);
|
filters.tags.delete(tag);
|
||||||
@@ -85,9 +84,6 @@ export const {
|
|||||||
filters.tags.clear();
|
filters.tags.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial load
|
|
||||||
loadPosts();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
posts,
|
posts,
|
||||||
loading,
|
loading,
|
||||||
|
|||||||
Reference in New Issue
Block a user