chore: improve get wiki home document

This commit is contained in:
fantasticit 2022-05-26 10:17:57 +08:00
parent f82d30f8c8
commit 914f7dcc00
6 changed files with 63 additions and 43 deletions

View File

@ -156,29 +156,6 @@ export const useAllPublicWikis = () => {
};
};
/**
*
* @returns
*/
export const getWikiHomeDocument = (wikiId, cookie = null): Promise<IDocument> => {
return HttpClient.request({
method: WikiApiDefinition.getHomeDocumentById.method,
url: WikiApiDefinition.getHomeDocumentById.client(wikiId),
cookie,
});
};
/**
*
* @returns
*/
export const useWikiHomeDocument = (wikiId) => {
const { data, error, isLoading } = useQuery(WikiApiDefinition.getHomeDocumentById.client(wikiId), () =>
getWikiHomeDocument(wikiId)
);
return { data, error, loading: isLoading };
};
/**
*
* @param wikiId

View File

@ -1,8 +1,9 @@
import { Spin } from '@douyinfe/semi-ui';
import { IWiki, WikiApiDefinition } from '@think/domains';
import { DataRender } from 'components/data-render';
import { DocumentReader } from 'components/document/reader';
import { WikiTocs } from 'components/wiki/tocs';
import { getWikiDetail, getWikiHomeDocument, getWikiTocs, useWikiHomeDocument } from 'data/wiki';
import { getWikiDetail, getWikiTocs, useWikiDetail } from 'data/wiki';
import { DoubleColumnLayout } from 'layouts/double-column';
import { NextPage } from 'next';
import React from 'react';
@ -13,7 +14,7 @@ interface IProps {
}
const Page: NextPage<IProps> = ({ wikiId }) => {
const { data: doc, loading, error } = useWikiHomeDocument(wikiId);
const { data: wiki, loading, error } = useWikiDetail(wikiId);
return (
<DoubleColumnLayout
@ -21,21 +22,30 @@ const Page: NextPage<IProps> = ({ wikiId }) => {
rightNode={
<DataRender
loading={loading}
loadingContent={
<div
style={{
minHeight: 240,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
margin: 'auto',
}}
>
<Spin />
</div>
}
error={error}
normalContent={() => <DocumentReader key={doc.id} documentId={doc.id} />}
normalContent={() => <DocumentReader key={wiki.homeDocumentId} documentId={wiki.homeDocumentId} />}
/>
}
></DoubleColumnLayout>
/>
);
};
Page.getInitialProps = async (ctx) => {
const { wikiId } = ctx.query;
const res = await serverPrefetcher(ctx, [
{
url: WikiApiDefinition.getHomeDocumentById.client(wikiId as IWiki['id']),
action: (cookie) => getWikiHomeDocument(cookie),
},
{
url: WikiApiDefinition.getDetailById.client(wikiId as IWiki['id']),
action: (cookie) => getWikiDetail(wikiId, cookie),

View File

@ -1,4 +1,5 @@
import { IUser } from './user';
import { IDocument } from './document';
/**
*
*/
@ -32,6 +33,7 @@ export interface IWiki {
createUserId: IUser['id'];
createUser: IUser;
status: WikiStatus;
homeDocumentId: IDocument['id'];
createdAt: Date;
updatedAt: Date;
}

View File

@ -1,4 +1,5 @@
import { IUser } from './user';
import { IDocument } from './document';
/**
*
@ -36,6 +37,7 @@ export interface IWiki {
createUserId: IUser['id'];
createUser: IUser;
status: WikiStatus;
homeDocumentId: IDocument['id'];
createdAt: Date;
updatedAt: Date;
}

View File

@ -23,6 +23,9 @@ export class WikiEntity {
@Column({ type: 'varchar', comment: '创建用户 Id' })
public createUserId: string;
@Column({ type: 'varchar', comment: '知识库首页文档Id', default: '' })
public homeDocumentId: string;
@Column({
type: 'enum',
enum: WikiStatus,

View File

@ -13,7 +13,7 @@ import { MessageService } from '@services/message.service';
import { UserService } from '@services/user.service';
import { OutUser } from '@services/user.service';
import { ViewService } from '@services/view.service';
import { DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains';
import { CollectType, DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains';
import { instanceToPlain } from 'class-transformer';
import * as lodash from 'lodash';
import { Repository } from 'typeorm';
@ -41,7 +41,26 @@ export class WikiService {
@Inject(forwardRef(() => ViewService))
private readonly viewService: ViewService
) {}
) {
this.fixWikiData();
}
// 修正脚本
async fixWikiData() {
const wikis = await this.wikiRepo.find();
const needFixWikis = wikis.filter((wiki) => !wiki.homeDocumentId);
await Promise.all(
needFixWikis.map(async (wiki) => {
const doc = await this.documentService.findWikiHomeDocument(wiki.id);
const homeDocumentId = doc.id;
const withHomeDocumentIdWiki = await this.wikiRepo.merge(wiki, { homeDocumentId });
await this.wikiRepo.save(withHomeDocumentIdWiki);
})
);
console.log('修正完成');
}
/**
* id
@ -310,6 +329,7 @@ export class WikiService {
targetUserRole: WikiUserRole.admin,
});
// 知识库首页文档
const [doc] = await Promise.all([
await this.documentService.createDocument(
user,
{
@ -318,8 +338,14 @@ export class WikiService {
title: wiki.name,
},
true
);
return wiki;
),
await this.collectorService.toggleStar(user, { type: CollectType.wiki, targetId: wiki.id }),
]);
const homeDocumentId = doc.id;
const withHomeDocumentIdWiki = await this.wikiRepo.merge(wiki, { homeDocumentId });
await this.wikiRepo.save(withHomeDocumentIdWiki);
return withHomeDocumentIdWiki;
}
/**