fix: add isMember for wiki collector

pull/44/head
fantasticit 2022-05-18 09:34:03 +08:00
parent 2a3bedcbfe
commit 232c2bb6fd
4 changed files with 18 additions and 5 deletions

View File

@ -1,21 +1,21 @@
import { IconUser } from '@douyinfe/semi-icons'; import { IconUser } from '@douyinfe/semi-icons';
import { Avatar, Skeleton, Space, Typography } from '@douyinfe/semi-ui'; import { Avatar, Skeleton, Space, Typography } from '@douyinfe/semi-ui';
import { IWiki } from '@think/domains';
import { IconDocument } from 'components/icons/IconDocument'; import { IconDocument } from 'components/icons/IconDocument';
import { LocaleTime } from 'components/locale-time'; import { LocaleTime } from 'components/locale-time';
import { WikiStar } from 'components/wiki/star'; import { WikiStar } from 'components/wiki/star';
import { IWikiWithIsMember } from 'data/wiki';
import Link from 'next/link'; import Link from 'next/link';
import styles from './index.module.scss'; import styles from './index.module.scss';
const { Text, Paragraph } = Typography; const { Text, Paragraph } = Typography;
export const WikiCard: React.FC<{ wiki: IWiki; shareMode?: boolean }> = ({ wiki, shareMode = false }) => { export const WikiCard: React.FC<{ wiki: IWikiWithIsMember; shareMode?: boolean }> = ({ wiki, shareMode = false }) => {
return ( return (
<div className={styles.cardWrap}> <div className={styles.cardWrap}>
<Link <Link
href={{ href={{
pathname: `${shareMode ? '/share' : ''}/wiki/[wikiId]`, pathname: `${shareMode || !wiki.isMember ? '/share' : ''}/wiki/[wikiId]`,
query: { wikiId: wiki.id }, query: { wikiId: wiki.id },
}} }}
> >

View File

@ -9,6 +9,7 @@ export type IWikiUserOpeateData = {
userName: Pick<IUser, 'name'>; userName: Pick<IUser, 'name'>;
userRole: Pick<IWikiUser, 'userRole'>; userRole: Pick<IWikiUser, 'userRole'>;
}; };
export type IWikiWithIsMember = IWiki & { isMember: boolean };
/** /**
* *
@ -239,7 +240,7 @@ export const useWikiStar = (wikiId) => {
* @returns * @returns
*/ */
export const useStaredWikis = () => { export const useStaredWikis = () => {
const { data, error, mutate } = useSWR<IWiki[]>('/collector/wikis', (url) => HttpClient.post(url), { const { data, error, mutate } = useSWR<IWikiWithIsMember[]>('/collector/wikis', (url) => HttpClient.post(url), {
revalidateOnFocus: true, revalidateOnFocus: true,
}); });
const loading = !data && !error; const loading = !data && !error;

View File

@ -50,7 +50,8 @@ export class CollectorService {
const withCreateUserRes = await Promise.all( const withCreateUserRes = await Promise.all(
res.map(async (wiki) => { res.map(async (wiki) => {
const createUser = await this.userService.findById(wiki.createUserId); const createUser = await this.userService.findById(wiki.createUserId);
return { createUser, ...wiki }; const isMember = await this.wikiService.isMember(wiki.id, user.id);
return { createUser, isMember, ...wiki };
}) })
); );

View File

@ -63,6 +63,17 @@ export class WikiService {
return ret; return ret;
} }
/**
*
* @param wikiId
* @param userId
* @returns
*/
public async isMember(wikiId: string, userId: string) {
const auth = await this.wikiUserRepo.findOne({ wikiId, userId });
return !!auth && [WikiUserRole.admin, WikiUserRole.normal].includes(auth.userRole);
}
/** /**
* *
* @param wikiId * @param wikiId