-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathNotifications.js
More file actions
89 lines (82 loc) · 2.59 KB
/
Notifications.js
File metadata and controls
89 lines (82 loc) · 2.59 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useCallback, useContext } from 'react'
import { Button, FormGroup, Table } from 'react-bootstrap'
import EmptyBadge from './components/EmptyBadge'
import { http } from '../lib/leancloud'
import { useQuery, useQueryClient } from 'react-query'
import { Link } from 'react-router-dom'
import { TicketStatusLabel } from './components/TicketStatusLabel'
import moment from 'moment'
import classNames from 'classnames'
import css from './CustomerServiceTickets.css'
import { AppContext } from './context'
const renderLastAction = (action) => {
return (
{
newTicket: '新建工单',
reply: '新的回复',
changeAssignee: '转移给你',
ticketEvaluation: '新的评价',
changeStatus: '状态更新',
}[action] ?? ''
)
}
const Notification = ({ notification }) => {
const unread = notification.unreadCount > 0
return (
<tr className={classNames([css.notification, unread ? css.unread : css.read])}>
<td>{unread && <EmptyBadge />}</td>
<td>
<div className={css.heading}>
<Link to={'/tickets/' + notification.ticket.nid} className={css.title}>
{notification.ticket.title}
</Link>
</div>
<div className={css.meta}>
<span className={css.nid}>#{notification.ticket.nid}</span>
<span className={css.status}>
<TicketStatusLabel status={notification.ticket.status} />
</span>
</div>
</td>
<td>{renderLastAction(notification.latestAction)}</td>
<td className="text-muted">
{notification.latestActionAt && moment(notification.latestActionAt).fromNow()}
</td>
</tr>
)
}
export const Notifications = () => {
const { addNotification } = useContext(AppContext)
const queryClient = useQueryClient()
const { data: notifications, refetch } = useQuery({
queryKey: 'notifications',
queryFn: () => http.get('/api/2/notifications'),
})
const markAllReaded = useCallback(() => {
http
.post('/api/2/notifications/read-all')
.then(() => {
refetch()
queryClient.invalidateQueries('unread')
return
})
.catch(addNotification)
}, [addNotification, queryClient, refetch])
return (
<div>
<FormGroup>
<Button variant="light" onClick={markAllReaded}>
全部标记为已读
</Button>
</FormGroup>
<Table>
<tbody>
{notifications &&
notifications.map((notification) => (
<Notification notification={notification} key={notification.id} />
))}
</tbody>
</Table>
</div>
)
}