refactor: 添加好友

pull/13/head
moonrailgun 4 years ago
parent 6339efcba5
commit 47a72e3568

@ -37,6 +37,7 @@ export {
} from './manager/ui'; } from './manager/ui';
// model // model
export { addFriendRequest } from './model/friend';
export type { UserBaseInfo } from './model/user'; export type { UserBaseInfo } from './model/user';
export { export {
loginWithEmail, loginWithEmail,

@ -0,0 +1,21 @@
import { request } from '../api/request';
export interface FriendRequest {
from: string;
to: string;
message: string;
}
/**
*
* @param targetId id
*/
export async function addFriendRequest(
targetId: string
): Promise<FriendRequest> {
const { data } = await request.post('/api/friend/request/add', {
to: targetId,
});
return data;
}

@ -13,12 +13,6 @@ export interface UserLoginInfo extends UserBaseInfo {
createdAt: string; createdAt: string;
} }
export interface FriendRequest {
from: string;
to: string;
message: string;
}
/** /**
* *
* @param email * @param email

@ -1,7 +1,8 @@
import type { AppStore } from './store'; import type { AppStore } from './store';
import type { AppSocket } from '../api/socket'; import type { AppSocket } from '../api/socket';
import { userActions } from './slices'; import { userActions } from './slices';
import type { FriendRequest, UserBaseInfo } from '../model/user'; import type { UserBaseInfo } from '../model/user';
import type { FriendRequest } from '../model/friend';
/** /**
* Redux * Redux

@ -25,7 +25,7 @@ if (window.localStorage.getItem('serviceUrl')) {
setToasts((msg, type = 'info') => { setToasts((msg, type = 'info') => {
message.open({ message.open({
type, type,
duration: 30000, duration: 3,
content: String(msg), content: String(msg),
}); });
}); });

@ -2,8 +2,11 @@ import { Avatar } from '@/components/Avatar';
import { Highlight } from '@/components/Highlight'; import { Highlight } from '@/components/Highlight';
import { Button, Divider, Empty } from 'antd'; import { Button, Divider, Empty } from 'antd';
import { import {
addFriendRequest,
searchUserWithUniqueName, searchUserWithUniqueName,
showErrorToasts, showErrorToasts,
showToasts,
t,
useAsyncFn, useAsyncFn,
UserBaseInfo, UserBaseInfo,
} from 'pawchat-shared'; } from 'pawchat-shared';
@ -12,8 +15,15 @@ import React, { useCallback, useState } from 'react';
const SearchFriendResult: React.FC<{ const SearchFriendResult: React.FC<{
result: UserBaseInfo | undefined | null; result: UserBaseInfo | undefined | null;
}> = React.memo(({ result }) => { }> = React.memo(({ result }) => {
const handleAddFriend = useCallback((userId: string) => { const [hasSentUserId, setHasSentUserId] = useState(''); // 记录已发送的
console.log(userId); const handleAddFriend = useCallback(async (userId: string) => {
try {
await addFriendRequest(userId);
setHasSentUserId(userId);
showToasts(t('已发送申请'), 'success');
} catch (err) {
showErrorToasts(err);
}
}, []); }, []);
if (result === undefined) { if (result === undefined) {
@ -24,6 +34,8 @@ const SearchFriendResult: React.FC<{
return <Empty />; return <Empty />;
} }
const hasSent = hasSentUserId === result._id;
return ( return (
<div> <div>
<Divider /> <Divider />
@ -47,9 +59,10 @@ const SearchFriendResult: React.FC<{
<Button <Button
type="primary" type="primary"
className="bg-green-600 border-0" className="bg-green-600 border-0"
disabled={hasSent}
onClick={() => handleAddFriend(result._id)} onClick={() => handleAddFriend(result._id)}
> >
{hasSent ? t('已申请') : t('申请好友')}
</Button> </Button>
</div> </div>
</div> </div>

Loading…
Cancel
Save