client: fix get collector

This commit is contained in:
fantasticit 2022-05-24 19:29:30 +08:00
parent fc335fbc1f
commit a18918f0f9
3 changed files with 43 additions and 3 deletions

View File

@ -1,5 +1,12 @@
import { CollectorApiDefinition, CollectType, IDocument, IWiki } from '@think/domains';
import { useCallback } from 'react';
import {
event,
TOGGLE_COLLECT_DOUCMENT,
TOGGLE_COLLECT_WIKI,
triggerToggleCollectDocument,
triggerToggleCollectWiki,
} from 'event';
import { useCallback, useEffect } from 'react';
import { useQuery } from 'react-query';
import { HttpClient } from 'services/http-client';
@ -22,7 +29,18 @@ export const getCollectedWikis = (cookie = null): Promise<IWikiWithIsMember[]> =
* @returns
*/
export const useCollectedWikis = () => {
const { data, error, isLoading, refetch } = useQuery(CollectorApiDefinition.wikis.client(), getCollectedWikis);
const { data, error, isLoading, refetch } = useQuery(CollectorApiDefinition.wikis.client(), getCollectedWikis, {
staleTime: 0,
});
useEffect(() => {
event.on(TOGGLE_COLLECT_WIKI, refetch);
return () => {
event.off(TOGGLE_COLLECT_WIKI, refetch);
};
}, [refetch]);
return { data, error, loading: isLoading, refresh: refetch };
};
@ -73,6 +91,7 @@ export const useWikiCollectToggle = (wikiId) => {
const toggle = useCallback(async () => {
await toggleCollectWiki(wikiId);
refetch();
triggerToggleCollectWiki();
}, [refetch, wikiId]);
return { data, error, toggle };
@ -97,8 +116,16 @@ export const getCollectedDocuments = (cookie = null): Promise<IDocument[]> => {
export const useCollectedDocuments = () => {
const { data, error, isLoading, refetch } = useQuery(
CollectorApiDefinition.documents.client(),
getCollectedDocuments
getCollectedDocuments,
{ staleTime: 0 }
);
useEffect(() => {
event.on(TOGGLE_COLLECT_DOUCMENT, refetch);
return () => {
event.off(TOGGLE_COLLECT_DOUCMENT, refetch);
};
}, [refetch]);
return { data, error, loading: isLoading, refresh: refetch };
};
@ -149,6 +176,7 @@ export const useDocumentCollectToggle = (documentId) => {
const toggle = useCallback(async () => {
await toggleCollectDocument(documentId);
refetch();
triggerToggleCollectDocument();
}, [refetch, documentId]);
return { data, error, toggle };

View File

@ -5,6 +5,9 @@ export const event = new EventEmitter();
export const REFRESH_TOCS = `REFRESH_TOCS`; // 刷新知识库目录
export const CREATE_DOCUMENT = `CREATE_DOCUMENT`;
export const TOGGLE_COLLECT_WIKI = `TOGGLE_COLLECT_WIKI`; // 收藏或取消收藏知识库
export const TOGGLE_COLLECT_DOUCMENT = `TOGGLE_COLLECT_DOUCMENT`; // 收藏或取消收藏文档
/**
*
*/
@ -49,3 +52,11 @@ type CollaborationUser = {
export const triggerJoinUser = (users: Array<CollaborationUser>) => {
event.emit(JOIN_USER, users);
};
export const triggerToggleCollectWiki = () => {
event.emit(TOGGLE_COLLECT_WIKI);
};
export const triggerToggleCollectDocument = () => {
event.emit(TOGGLE_COLLECT_DOUCMENT);
};

View File

@ -16,6 +16,7 @@ class MyApp extends App<{ isMobile: boolean }> {
queryClient: new QueryClient({
defaultOptions: {
queries: {
refetchOnMount: true,
refetchOnWindowFocus: true,
retry: false,
staleTime: 30000,