-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathnotice.ts
executable file
·60 lines (51 loc) · 1.64 KB
/
notice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {deleteNotice, queryNotices, updateNotices} from '@/services/user';
import auth from "@/utils/auth";
import {useState} from 'react';
export default () => {
const [notices, setNotices] = useState<any[]>([]);
const [ws, setWs] = useState(null);
const [noticeCount, setNoticeCount] = useState<number>(0);
const [unreadCount, setUnreadCount] = useState<number>(0);
const [totalCount, setTotalCount] = useState<number>(0);
const saveNotices = (payload: any[]) => {
setNotices(payload)
}
const clearNotices = (payload: string) => {
const count = notices.length;
const unreadCount = notices.filter((item) => !item.read).length || 0;
setNotices(notices.filter((item) => item.type !== payload))
setTotalCount(count)
setUnreadCount(unreadCount)
}
const fetchNotices = async (params: Record<string, string | number>) => {
const resp = await queryNotices(params)
saveNotices(resp)
return resp;
}
const deleteNotices = async (idList: number[]) => {
const resp = await deleteNotice(idList)
auth.response(resp);
}
const readNotices = async (data: any[]) => {
const broadcast = data?.filter(item => item.msg_type === 1).map(item => item.id) || []
const personal = data?.filter(item => item.msg_type === 2).map(item => item.id) || []
if (broadcast.length > 0 || personal.length > 0) {
const resp = await updateNotices({broadcast, personal})
auth.response(resp)
}
}
return {
clearNotices,
fetchNotices,
notices,
totalCount,
noticeCount,
ws,
setWs,
unreadCount,
deleteNotices,
setNoticeCount,
readNotices,
setTotalCount
}
}