fix: fix refresh tocs

This commit is contained in:
fantasticit 2022-03-12 17:52:03 +08:00
parent f99d766e15
commit 5735222778
2 changed files with 21 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import Router from 'next/router';
import { Typography, Space, Modal } from '@douyinfe/semi-ui'; import { Typography, Space, Modal } from '@douyinfe/semi-ui';
import { IconDelete } from '@douyinfe/semi-icons'; import { IconDelete } from '@douyinfe/semi-icons';
import { useDeleteDocument } from 'data/document'; import { useDeleteDocument } from 'data/document';
import { triggerRefreshTocs } from 'components/wiki/tocs';
interface IProps { interface IProps {
wikiId: string; wikiId: string;
@ -26,6 +27,7 @@ export const DocumentDeletor: React.FC<IProps> = ({ wikiId, documentId, onDelete
: Router.push({ : Router.push({
pathname: `/wiki/${wikiId}`, pathname: `/wiki/${wikiId}`,
}); });
triggerRefreshTocs();
}); });
}, },
okButtonProps: { loading, type: 'danger' }, okButtonProps: { loading, type: 'danger' },

View File

@ -9,10 +9,18 @@ import { findParents } from 'components/wiki/tocs/utils';
import { IconDocument, IconSetting, IconOverview } from 'components/icons'; import { IconDocument, IconSetting, IconOverview } from 'components/icons';
import { DocumentCreator } from 'components/document/create'; import { DocumentCreator } from 'components/document/create';
import { DataRender } from 'components/data-render'; import { DataRender } from 'components/data-render';
import { EventEmitter } from 'helpers/event-emitter';
import { NavItem } from './NavItem'; import { NavItem } from './NavItem';
import { Tree } from './tree'; import { Tree } from './tree';
import styles from './index.module.scss'; import styles from './index.module.scss';
const em = new EventEmitter();
const EVENT_KEY = 'REFRESH_TOCS';
export const triggerRefreshTocs = () => {
em.emit(EVENT_KEY);
};
interface IProps { interface IProps {
wikiId: string; wikiId: string;
documentId?: string; documentId?: string;
@ -33,7 +41,7 @@ export const WikiTocs: React.FC<IProps> = ({
const { pathname } = useRouter(); const { pathname } = useRouter();
const [visible, toggleVisible] = useToggle(false); const [visible, toggleVisible] = useToggle(false);
const { data: wiki, loading: wikiLoading, error: wikiError } = useWikiDetail(wikiId); 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<Array<string>>([]); const [parentIds, setParentIds] = useState<Array<string>>([]);
useEffect(() => { useEffect(() => {
@ -42,6 +50,16 @@ export const WikiTocs: React.FC<IProps> = ({
setParentIds(parentIds); setParentIds(parentIds);
}, [tocs, documentId]); }, [tocs, documentId]);
useEffect(() => {
em.on(EVENT_KEY, () => {
refresh();
});
return () => {
em.destroy();
};
}, []);
return ( return (
<div className={styles.wrap}> <div className={styles.wrap}>
<DataRender <DataRender