feat: now we can see all public wikis

This commit is contained in:
fantasticit 2022-03-18 17:09:00 +08:00
parent 94425befcf
commit e7031776a8
6 changed files with 152 additions and 3 deletions

View File

@ -9,11 +9,19 @@ import styles from './index.module.scss';
const { Text, Paragraph } = Typography; const { Text, Paragraph } = Typography;
export const WikiCard: React.FC<{ wiki: IWiki }> = ({ wiki }) => { export const WikiCard: React.FC<{ wiki: IWiki; shareMode?: boolean }> = ({
wiki,
shareMode = false,
}) => {
return ( return (
<div className={styles.cardWrap}> <div className={styles.cardWrap}>
<Link href={{ pathname: `/wiki/[wikiId]`, query: { wikiId: wiki.id } }}> <Link
<a> href={{
pathname: `${shareMode ? '/share' : ''}/wiki/[wikiId]`,
query: { wikiId: wiki.id },
}}
>
<a target={shareMode ? '_blank' : '_self'}>
<header> <header>
<Avatar <Avatar
shape="square" shape="square"

View File

@ -1,5 +1,6 @@
import { CollectType, IDocument, IUser, IWiki, IWikiUser } from '@think/domains'; import { CollectType, IDocument, IUser, IWiki, IWikiUser } from '@think/domains';
import useSWR from 'swr'; import useSWR from 'swr';
import { useState } from 'react';
import { HttpClient } from 'services/HttpClient'; import { HttpClient } from 'services/HttpClient';
export type ICreateWiki = Pick<IWiki, 'name' | 'description'>; export type ICreateWiki = Pick<IWiki, 'name' | 'description'>;
@ -276,3 +277,24 @@ export const usePublicWikiTocs = (wikiId) => {
return { data, loading, error, refresh: mutate }; return { data, loading, error, refresh: mutate };
}; };
/**
*
* @param documentId
* @returns
*/
export const useAllPublicWikis = () => {
const [page, setPage] = useState(1);
const { data, error, mutate } = useSWR<{
data: Array<IWiki>;
total: number;
}>(`/wiki/public/wikis?page=${page}`, (url) => HttpClient.get(url));
const loading = !data && !error;
return {
data,
loading,
error,
setPage,
};
};

View File

@ -78,6 +78,19 @@ export const RouterHeader: React.FC = () => {
}); });
}, },
}, },
{
itemKey: '/find',
text: (
<Link href="/find">
<a></a>
</Link>
),
onClick: () => {
Router.push({
pathname: `/find`,
});
},
},
]} ]}
footer={ footer={
<Space> <Space>

View File

@ -0,0 +1,71 @@
import type { NextPage } from 'next';
import React from 'react';
import { List, Pagination, Typography } from '@douyinfe/semi-ui';
import { SingleColumnLayout } from 'layouts/single-column';
import { useAllPublicWikis } from 'data/wiki';
import { Empty } from 'components/empty';
import { Seo } from 'components/seo';
import { DataRender } from 'components/data-render';
import { WikiCardPlaceholder, WikiCard } from 'components/wiki/card';
const grid = {
gutter: 16,
xs: 24,
sm: 12,
md: 12,
lg: 8,
xl: 8,
};
const { Title } = Typography;
const Page: NextPage = () => {
const { data, loading, error, setPage } = useAllPublicWikis();
console.log(data);
return (
<SingleColumnLayout>
<Seo title="发现" />
<div className="container">
<Title heading={3} style={{ marginBottom: 24 }}>
</Title>
<DataRender
loading={loading}
loadingContent={() => (
<List
grid={grid}
dataSource={[1, 2, 3]}
renderItem={() => (
<List.Item>
<WikiCardPlaceholder />
</List.Item>
)}
/>
)}
error={error}
normalContent={() => (
<>
<List
grid={grid}
dataSource={data.data}
renderItem={(wiki) => (
<List.Item>
<WikiCard wiki={wiki} shareMode />
</List.Item>
)}
emptyContent={<Empty message={'暂无数据'} />}
/>
<div style={{ display: 'flex', justifyContent: 'center' }}>
<Pagination total={data.total} pageSize={12} onPageChange={setPage} />
</div>
</>
)}
/>
</div>
</SingleColumnLayout>
);
};
export default Page;

View File

@ -174,4 +174,11 @@ export class WikiController {
async getPublicWorkspaceDetail(@Param('id') wikiId) { async getPublicWorkspaceDetail(@Param('id') wikiId) {
return await this.wikiService.getPublicWikiDetail(wikiId); return await this.wikiService.getPublicWikiDetail(wikiId);
} }
@UseInterceptors(ClassSerializerInterceptor)
@Get('public/wikis')
@HttpCode(HttpStatus.OK)
async getAllPublicWikis(@Query() pagination: IPagination) {
return await this.wikiService.getAllPublicWikis(pagination);
}
} }

View File

@ -604,4 +604,32 @@ export class WikiService {
async getPublicWikiTocs(wikiId) { async getPublicWikiTocs(wikiId) {
return await this.documentService.getPublicWikiTocs(wikiId); return await this.documentService.getPublicWikiTocs(wikiId);
} }
/**
*
* @param user
* @param pagination
* @returns
*/
async getAllPublicWikis(pagination: IPagination) {
const { page = 1, pageSize = 12 } = pagination;
const query = await this.wikiRepo
.createQueryBuilder('wiki')
.where('wiki.status=:status')
.setParameter('status', WikiStatus.public);
query.skip((+page - 1) * +pageSize);
query.take(+pageSize);
const [wikis, total] = await query.getManyAndCount();
const ret = await Promise.all(
wikis.map(async (wiki) => {
const createUser = await this.userService.findById(wiki.createUserId);
return { ...wiki, createUser };
})
);
return { data: ret, total };
}
} }