Commit f7393b09d0c1bd4fed2b799e601f540cc62851f7
1 parent
1f27d82a
feat: update
Showing
1 changed file
with
140 additions
and
61 deletions
src/pages/Order/components/MessageListDrawer.tsx
1 | 1 | import { RESPONSE_CODE } from '@/constants/enum'; |
2 | -import { postOrderErpMessageQueryMyMessage } from '@/services'; | |
2 | +import { | |
3 | + postOrderErpMessageQueryMyMessage, | |
4 | + postOrderErpMessageRead, | |
5 | +} from '@/services'; | |
3 | 6 | import { formatDateTime, getUserInfo } from '@/utils'; |
4 | 7 | import { UserOutlined } from '@ant-design/icons'; |
5 | -import { Avatar, Badge, Drawer, Flex, List } from 'antd'; | |
6 | -import VirtualList from 'rc-virtual-list'; | |
8 | +import { | |
9 | + Avatar, | |
10 | + Badge, | |
11 | + Button, | |
12 | + Checkbox, | |
13 | + Drawer, | |
14 | + Flex, | |
15 | + List, | |
16 | + message, | |
17 | +} from 'antd'; | |
7 | 18 | import { useEffect, useState } from 'react'; |
8 | 19 | |
9 | 20 | export default ({ setVisible }) => { |
10 | 21 | const userInfo = getUserInfo(); |
11 | - const current = useState<number>(1); //当前页码 | |
12 | - // const [total, setTotal] = useState(0); | |
22 | + const [current, setCurrent] = useState<number>(1); //当前页码 | |
23 | + const [total, setTotal] = useState(0); | |
13 | 24 | const [messageListData, setMessageListData] = useState<any[]>([]); //列表数据 |
14 | - const ContainerHeight = 400; | |
25 | + const [loading, setLoading] = useState(false); | |
26 | + const [initLoading, setInitLoading] = useState(true); | |
27 | + const [onlyUnread, setOnlyUnread] = useState(false); | |
28 | + const [unreadNumList, setUnreadNumList] = useState<any[]>([]); //消息未读标识 | |
29 | + | |
15 | 30 | /** |
16 | 31 | * 获取消息列表 |
17 | 32 | */ |
18 | 33 | async function getMessageListData() { |
34 | + setLoading(true); | |
19 | 35 | let res = await postOrderErpMessageQueryMyMessage({ |
20 | - data: { username: userInfo.username, current: current[0] }, | |
36 | + data: { | |
37 | + username: userInfo.username, | |
38 | + current: current, | |
39 | + isReaded: onlyUnread, | |
40 | + }, | |
41 | + }).finally(() => { | |
42 | + setLoading(false); | |
21 | 43 | }); |
22 | 44 | if (res && res.result === RESPONSE_CODE.SUCCESS) { |
23 | 45 | setMessageListData([...messageListData, ...res?.data?.data]); |
24 | - // setTotal(res?.data?.total); | |
46 | + let reqReadNumList = res?.data?.data?.map((item: any) => { | |
47 | + return item.unreadNum; | |
48 | + }); | |
49 | + setUnreadNumList([...unreadNumList, ...reqReadNumList]); | |
50 | + setTotal(res?.data?.total); | |
25 | 51 | } |
26 | 52 | } |
27 | 53 | |
... | ... | @@ -32,22 +58,59 @@ export default ({ setVisible }) => { |
32 | 58 | window.open('/order?id=' + mainOrderId, '_blank'); |
33 | 59 | } |
34 | 60 | |
35 | - const onScroll = (e: React.UIEvent<HTMLElement, UIEvent>) => { | |
36 | - // Refer to: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#problems_and_solutions | |
37 | - if ( | |
38 | - Math.abs( | |
39 | - e.currentTarget.scrollHeight - | |
40 | - e.currentTarget.scrollTop - | |
41 | - ContainerHeight, | |
42 | - ) <= 1 | |
43 | - ) { | |
44 | - getMessageListData(); | |
61 | + /** | |
62 | + * 仅展示未读 | |
63 | + */ | |
64 | + function onlyUnreadChange() { | |
65 | + setOnlyUnread(!onlyUnread); | |
66 | + setCurrent(1); | |
67 | + setMessageListData([]); | |
68 | + setUnreadNumList([]); | |
69 | + } | |
70 | + | |
71 | + /** | |
72 | + * 仅展示未读 | |
73 | + */ | |
74 | + async function read(ids: any[]) { | |
75 | + let res = await postOrderErpMessageRead({ | |
76 | + data: ids, | |
77 | + }); | |
78 | + | |
79 | + if (res && res.result === RESPONSE_CODE.SUCCESS) { | |
80 | + message.success('已读'); | |
81 | + return true; | |
45 | 82 | } |
46 | - }; | |
83 | + | |
84 | + return false; | |
85 | + } | |
86 | + | |
87 | + const loadMore = | |
88 | + !initLoading && !loading && messageListData.length < total ? ( | |
89 | + <div | |
90 | + style={{ | |
91 | + textAlign: 'center', | |
92 | + marginTop: 12, | |
93 | + height: 32, | |
94 | + lineHeight: '32px', | |
95 | + }} | |
96 | + > | |
97 | + <Button | |
98 | + onClick={() => { | |
99 | + setCurrent(current + 1); | |
100 | + }} | |
101 | + > | |
102 | + 加载更多 | |
103 | + </Button> | |
104 | + </div> | |
105 | + ) : messageListData.length >= total ? ( | |
106 | + <span>没有更多消息了</span> | |
107 | + ) : null; | |
47 | 108 | |
48 | 109 | useEffect(() => { |
49 | - getMessageListData(); | |
50 | - }, []); | |
110 | + getMessageListData().finally(() => { | |
111 | + setInitLoading(false); | |
112 | + }); | |
113 | + }, [onlyUnread, current]); | |
51 | 114 | |
52 | 115 | return ( |
53 | 116 | <> |
... | ... | @@ -60,48 +123,64 @@ export default ({ setVisible }) => { |
60 | 123 | width={450} |
61 | 124 | styles={{ body: { paddingTop: 0 } }} |
62 | 125 | > |
63 | - <List> | |
64 | - <VirtualList | |
65 | - data={messageListData} | |
66 | - height={400} | |
67 | - itemHeight={47} | |
68 | - itemKey="email" | |
69 | - onScroll={onScroll} | |
70 | - > | |
71 | - {(item: any) => ( | |
72 | - <List.Item key={item.id}> | |
73 | - <List.Item.Meta | |
74 | - avatar={ | |
75 | - <Badge dot={item.unreadNum > 0}> | |
76 | - <Avatar shape="square" icon={<UserOutlined />} /> | |
77 | - </Badge> | |
126 | + <Flex align="center" justify="space-between"> | |
127 | + <div> | |
128 | + <Checkbox onChange={onlyUnreadChange} checked={onlyUnread}> | |
129 | + 仅展示未读 | |
130 | + </Checkbox> | |
131 | + </div> | |
132 | + | |
133 | + <Button className="p-0" type="link"> | |
134 | + 全部标为已读 | |
135 | + </Button> | |
136 | + </Flex> | |
137 | + | |
138 | + <List | |
139 | + className="demo-loadmore-list" | |
140 | + loading={initLoading} | |
141 | + itemLayout="horizontal" | |
142 | + loadMore={loadMore} | |
143 | + dataSource={messageListData} | |
144 | + renderItem={(item: any, index: any) => ( | |
145 | + <List.Item key={item.id}> | |
146 | + <List.Item.Meta | |
147 | + avatar={ | |
148 | + <Badge dot={unreadNumList[index] > 0}> | |
149 | + <Avatar shape="square" icon={<UserOutlined />} /> | |
150 | + </Badge> | |
151 | + } | |
152 | + /> | |
153 | + <Flex | |
154 | + vertical | |
155 | + className="hover:cursor-pointer" | |
156 | + onClick={async () => { | |
157 | + toOrderList(item.mainOrderId); | |
158 | + | |
159 | + let readSuccess = await read([item.id]); | |
160 | + if (readSuccess) { | |
161 | + let newUnreadNumList = [...unreadNumList]; | |
162 | + newUnreadNumList[index] = 0; | |
163 | + setUnreadNumList([...newUnreadNumList]); | |
78 | 164 | } |
79 | - /> | |
80 | - <Flex | |
81 | - vertical | |
82 | - className="hover:cursor-pointer" | |
83 | - onClick={() => { | |
84 | - toOrderList(item.mainOrderId); | |
85 | - }} | |
86 | - > | |
87 | - <Flex> | |
88 | - <div> | |
89 | - {item.content} | |
90 | - <span className="text-[#8C8C8C]"> | |
91 | - (点击跳转到对应主订单) | |
92 | - </span> | |
93 | - </div> | |
94 | - </Flex> | |
95 | - <Flex> | |
96 | - <span className="text-xs text-[#8C8C8C] pt-1"> | |
97 | - {formatDateTime(item.createTime)} | |
98 | - </span> | |
99 | - </Flex> | |
165 | + }} | |
166 | + > | |
167 | + <Flex> | |
168 | + <div> | |
169 | + {item.content} | |
170 | + {/* <span className="text-[#8C8C8C]"> | |
171 | + (点击跳转到对应主订单) | |
172 | + </span> */} | |
173 | + </div> | |
174 | + </Flex> | |
175 | + <Flex> | |
176 | + <span className="text-xs text-[#8C8C8C] pt-1"> | |
177 | + {formatDateTime(item.createTime)} | |
178 | + </span> | |
100 | 179 | </Flex> |
101 | - </List.Item> | |
102 | - )} | |
103 | - </VirtualList> | |
104 | - </List> | |
180 | + </Flex> | |
181 | + </List.Item> | |
182 | + )} | |
183 | + /> | |
105 | 184 | </Drawer> |
106 | 185 | </> |
107 | 186 | ); | ... | ... |