From 5735222778c77012a73900e427a040a712a0ebc5 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 12 Mar 2022 17:52:03 +0800 Subject: [PATCH] fix: fix refresh tocs --- .../src/components/document/delete/index.tsx | 2 ++ .../client/src/components/wiki/tocs/index.tsx | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/client/src/components/document/delete/index.tsx b/packages/client/src/components/document/delete/index.tsx index 43c421b..4f6f29d 100644 --- a/packages/client/src/components/document/delete/index.tsx +++ b/packages/client/src/components/document/delete/index.tsx @@ -3,6 +3,7 @@ import Router from 'next/router'; import { Typography, Space, Modal } from '@douyinfe/semi-ui'; import { IconDelete } from '@douyinfe/semi-icons'; import { useDeleteDocument } from 'data/document'; +import { triggerRefreshTocs } from 'components/wiki/tocs'; interface IProps { wikiId: string; @@ -26,6 +27,7 @@ export const DocumentDeletor: React.FC = ({ wikiId, documentId, onDelete : Router.push({ pathname: `/wiki/${wikiId}`, }); + triggerRefreshTocs(); }); }, okButtonProps: { loading, type: 'danger' }, diff --git a/packages/client/src/components/wiki/tocs/index.tsx b/packages/client/src/components/wiki/tocs/index.tsx index c4479cf..7b3be02 100644 --- a/packages/client/src/components/wiki/tocs/index.tsx +++ b/packages/client/src/components/wiki/tocs/index.tsx @@ -9,10 +9,18 @@ import { findParents } from 'components/wiki/tocs/utils'; import { IconDocument, IconSetting, IconOverview } from 'components/icons'; import { DocumentCreator } from 'components/document/create'; import { DataRender } from 'components/data-render'; +import { EventEmitter } from 'helpers/event-emitter'; import { NavItem } from './NavItem'; import { Tree } from './tree'; import styles from './index.module.scss'; +const em = new EventEmitter(); +const EVENT_KEY = 'REFRESH_TOCS'; + +export const triggerRefreshTocs = () => { + em.emit(EVENT_KEY); +}; + interface IProps { wikiId: string; documentId?: string; @@ -33,7 +41,7 @@ export const WikiTocs: React.FC = ({ const { pathname } = useRouter(); const [visible, toggleVisible] = useToggle(false); const { data: wiki, loading: wikiLoading, error: wikiError } = useWikiDetail(wikiId); - const { data: tocs, loading: tocsLoading, error: tocsError } = useWikiTocs(wikiId); + const { data: tocs, loading: tocsLoading, error: tocsError, refresh } = useWikiTocs(wikiId); const [parentIds, setParentIds] = useState>([]); useEffect(() => { @@ -42,6 +50,16 @@ export const WikiTocs: React.FC = ({ setParentIds(parentIds); }, [tocs, documentId]); + useEffect(() => { + em.on(EVENT_KEY, () => { + refresh(); + }); + + return () => { + em.destroy(); + }; + }, []); + return (