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 { 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 { useQuery } from 'react-query';
import { HttpClient } from 'services/http-client'; import { HttpClient } from 'services/http-client';
@ -22,7 +29,18 @@ export const getCollectedWikis = (cookie = null): Promise<IWikiWithIsMember[]> =
* @returns * @returns
*/ */
export const useCollectedWikis = () => { 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 }; return { data, error, loading: isLoading, refresh: refetch };
}; };
@ -73,6 +91,7 @@ export const useWikiCollectToggle = (wikiId) => {
const toggle = useCallback(async () => { const toggle = useCallback(async () => {
await toggleCollectWiki(wikiId); await toggleCollectWiki(wikiId);
refetch(); refetch();
triggerToggleCollectWiki();
}, [refetch, wikiId]); }, [refetch, wikiId]);
return { data, error, toggle }; return { data, error, toggle };
@ -97,8 +116,16 @@ export const getCollectedDocuments = (cookie = null): Promise<IDocument[]> => {
export const useCollectedDocuments = () => { export const useCollectedDocuments = () => {
const { data, error, isLoading, refetch } = useQuery( const { data, error, isLoading, refetch } = useQuery(
CollectorApiDefinition.documents.client(), 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 }; return { data, error, loading: isLoading, refresh: refetch };
}; };
@ -149,6 +176,7 @@ export const useDocumentCollectToggle = (documentId) => {
const toggle = useCallback(async () => { const toggle = useCallback(async () => {
await toggleCollectDocument(documentId); await toggleCollectDocument(documentId);
refetch(); refetch();
triggerToggleCollectDocument();
}, [refetch, documentId]); }, [refetch, documentId]);
return { data, error, toggle }; return { data, error, toggle };

View File

@ -5,6 +5,9 @@ export const event = new EventEmitter();
export const REFRESH_TOCS = `REFRESH_TOCS`; // 刷新知识库目录 export const REFRESH_TOCS = `REFRESH_TOCS`; // 刷新知识库目录
export const CREATE_DOCUMENT = `CREATE_DOCUMENT`; 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>) => { export const triggerJoinUser = (users: Array<CollaborationUser>) => {
event.emit(JOIN_USER, users); 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({ queryClient: new QueryClient({
defaultOptions: { defaultOptions: {
queries: { queries: {
refetchOnMount: true,
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
retry: false, retry: false,
staleTime: 30000, staleTime: 30000,