Skip to content

Commit 23b3201

Browse files
authored
feat: mark one or all message (#24)
1 parent fa0d8d1 commit 23b3201

File tree

7 files changed

+128
-9
lines changed

7 files changed

+128
-9
lines changed

src/component/MessageList/index.less

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import '~antd/dist/antd.less';
2+
13
.list {
24
:global {
35
.ant-card {
@@ -8,3 +10,11 @@
810
}
911
}
1012
}
13+
14+
.link {
15+
color: @text-color;
16+
17+
&:hover {
18+
color: @primary-color;
19+
}
20+
}

src/component/MessageList/index.tsx

+20-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { MESSAGE_TYPE_MAP, MessageType } from '@/constants';
99

1010
import * as styles from './index.less';
1111

12-
const MessageList: React.FC<Props> = ({ dataSource, loading, toolbar }) => {
12+
const MessageList: React.FC<Props> = ({
13+
dataSource,
14+
loading,
15+
toolbar,
16+
onClick,
17+
}) => {
1318
const metas: ProListMetas = {
1419
avatar: {
1520
dataIndex: 'author.avatar_url',
@@ -25,7 +30,7 @@ const MessageList: React.FC<Props> = ({ dataSource, loading, toolbar }) => {
2530
width: '200px',
2631
}}
2732
>
28-
<Link to={`/user/${loginname}`}>
33+
<Link to={`/user/${loginname}`} className={styles.link}>
2934
<Space size={8}>
3035
<Avatar size="small" src={avatar_url} />
3136
<span>{loginname}</span>
@@ -43,9 +48,20 @@ const MessageList: React.FC<Props> = ({ dataSource, loading, toolbar }) => {
4348
valueType: 'text',
4449
render: (_, entity: MessageModel) => {
4550
const {
51+
id: messageId,
4652
topic: { id, title },
4753
} = entity;
48-
return <Link to={`/topic/${id}`}>{title}</Link>;
54+
return (
55+
<Link
56+
to={`/topic/${id}`}
57+
className={styles.link}
58+
onClick={() => {
59+
onClick && onClick(messageId);
60+
}}
61+
>
62+
{title}
63+
</Link>
64+
);
4965
},
5066
},
5167
actions: {
@@ -74,4 +90,5 @@ interface Props {
7490
dataSource?: MessageModel[];
7591
loading?: boolean;
7692
toolbar?: ListToolBarProps;
93+
onClick?: (id: string) => void;
7794
}

src/component/TopicList/index.less

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import '~antd/dist/antd.less';
2+
13
.list {
24
:global {
35
.ant-card {
@@ -8,3 +10,11 @@
810
}
911
}
1012
}
13+
14+
.link {
15+
color: @text-color;
16+
17+
&:hover {
18+
color: @primary-color;
19+
}
20+
}

src/component/TopicList/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ const TopicList: React.FC<Props> = ({ dataSource, loading, toolbar }) => {
5858
valueType: 'text',
5959
render: (_, entity: TopicModel) => {
6060
const { id, title } = entity;
61-
return <Link to={`/topic/${id}`}>{title}</Link>;
61+
return (
62+
<Link to={`/topic/${id}`} className={styles.link}>
63+
{title}
64+
</Link>
65+
);
6266
},
6367
},
6468
actions: {

src/model/message.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,34 @@ export default () => {
4545
setUnreadMessage(data.hasnot_read_messages);
4646
}, [token]);
4747

48-
return { count, message, unreadMessage, load, fetch };
48+
const mark = useCallback(
49+
async (id: string) => {
50+
if (!token) {
51+
return;
52+
}
53+
54+
await API.markMessage(id, {
55+
accesstoken: token,
56+
});
57+
58+
load();
59+
fetch();
60+
},
61+
[token],
62+
);
63+
64+
const markAll = useCallback(async () => {
65+
if (!token) {
66+
return;
67+
}
68+
69+
await API.markAllMessage({
70+
accesstoken: token,
71+
});
72+
73+
load();
74+
fetch();
75+
}, [token]);
76+
77+
return { count, message, unreadMessage, load, fetch, mark, markAll };
4978
};

src/page/message/index.tsx

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { useModel } from 'umi';
3-
import { Divider } from 'antd';
3+
import { Divider, Button } from 'antd';
44
import ProCard from '@ant-design/pro-card';
55
import MessageList from '@/component/MessageList';
66

77
const MessagePage: React.FC<Props> = (props) => {
8-
const { message, unreadMessage } = useModel('message');
8+
const { message, unreadMessage, mark, markAll } = useModel('message');
99

1010
console.debug('===message', message);
1111
console.debug('===unreadMessage', unreadMessage);
@@ -15,12 +15,30 @@ const MessagePage: React.FC<Props> = (props) => {
1515
return <span>暂无新消息</span>;
1616
}
1717

18-
return <MessageList dataSource={unreadMessage} />;
18+
return (
19+
<MessageList dataSource={unreadMessage} onClick={(id) => mark(id)} />
20+
);
1921
};
2022

2123
return (
2224
<div>
23-
<ProCard title="未读消息">{renderUnreadMessage()}</ProCard>
25+
<ProCard
26+
title="未读消息"
27+
extra={
28+
<Button
29+
size="small"
30+
type="primary"
31+
disabled={unreadMessage?.length === 0}
32+
onClick={() => {
33+
markAll();
34+
}}
35+
>
36+
标记全部
37+
</Button>
38+
}
39+
>
40+
{renderUnreadMessage()}
41+
</ProCard>
2442
<Divider />
2543
<ProCard title="已读消息">
2644
<MessageList dataSource={message} />

src/service/message.ts

+31
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ export const getMessages = async (params: {
2929
return res;
3030
};
3131

32+
export const markMessage = async (
33+
id: string,
34+
data: {
35+
accesstoken: string;
36+
},
37+
) => {
38+
const options: any = {
39+
method: 'POST',
40+
data,
41+
};
42+
const res: any = await request(
43+
`${BASE_URL}/api/v1/message/mark_one/${id}`,
44+
options,
45+
);
46+
47+
return res;
48+
};
49+
50+
export const markAllMessage = async (data: { accesstoken: string }) => {
51+
const options: any = {
52+
method: 'POST',
53+
data,
54+
};
55+
const res: any = await request(
56+
`${BASE_URL}/api/v1/message/mark_all`,
57+
options,
58+
);
59+
60+
return res;
61+
};
62+
3263
interface MessageCollection {
3364
has_read_messages: MessageModel[];
3465
hasnot_read_messages: MessageModel[];

0 commit comments

Comments
 (0)