NotifyDropdown.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import consts from '@/consts'
  2. import { queryNoticeList, queryUnreadNotice, updateNotice } from '@/services/login'
  3. import { BellOutlined } from '@ant-design/icons'
  4. import { Badge, Tabs, List, Typography, Popover } from 'antd'
  5. import { useState, useCallback, useEffect } from 'react'
  6. import styles from './index.less'
  7. const DropdownContent = ({ noticeCount }) => {
  8. const [state, setState] = useState({
  9. noticesTotal: 0,
  10. notices: []
  11. })
  12. const getNotices = useCallback(async () => {
  13. const {
  14. code = -1,
  15. data: { message, total }
  16. } = await queryNoticeList()
  17. if (code === consts.RET_CODE.SUCCESS) {
  18. setState({ ...state, noticesTotal: total, notices: message })
  19. }
  20. }, [])
  21. useEffect(() => {
  22. getNotices()
  23. }, [])
  24. const toggleNotice = id => {
  25. const nNotices = state.notices.map(item => {
  26. if (item.id === id) {
  27. return { ...item, isRead: 1 }
  28. }
  29. return item
  30. })
  31. setState({ ...state, notices: nNotices })
  32. }
  33. const handleTitleClick = async item => {
  34. // 未读
  35. if (!item.isRead) {
  36. const { code = -1 } = await updateNotice({ id: item.id })
  37. if (code === consts.RET_CODE.SUCCESS) {
  38. toggleNotice(item.id)
  39. }
  40. }
  41. }
  42. return (
  43. <Tabs>
  44. <Tabs.TabPane
  45. key="1"
  46. tab={
  47. <div>
  48. 通知
  49. {noticeCount ? <span> ({noticeCount}) </span> : null}
  50. </div>
  51. }
  52. >
  53. <List
  54. bordered
  55. dataSource={state.notices}
  56. renderItem={item => (
  57. <List.Item>
  58. <List.Item.Meta
  59. description={item.createTime}
  60. title={
  61. <Typography.Paragraph
  62. onClick={() => handleTitleClick(item)}
  63. ellipsis={{ rows: 1, tooltip: true }}
  64. delete={!!item.isRead}
  65. style={{
  66. cursor: !item.isRead ? 'pointer' : '',
  67. width: '100%',
  68. marginBottom: 0
  69. }}
  70. >
  71. {item.content}
  72. </Typography.Paragraph>
  73. }
  74. />
  75. </List.Item>
  76. )}
  77. />
  78. </Tabs.TabPane>
  79. <Tabs.TabPane tab="消息" key="2" />
  80. <Tabs.TabPane tab="代办" key="3" />
  81. </Tabs>
  82. )
  83. }
  84. const NotifyDropdown = () => {
  85. const [count, setCount] = useState(0)
  86. useEffect(() => {
  87. async function getCount() {
  88. const { code = -1, data = 0 } = await queryUnreadNotice()
  89. if (code === consts.RET_CODE.SUCCESS) {
  90. setCount(data)
  91. }
  92. }
  93. getCount()
  94. }, [])
  95. return (
  96. <Popover
  97. arrow
  98. trigger="click"
  99. overlayClassName="!min-w-360px"
  100. content={<DropdownContent noticeCount={count} />}
  101. >
  102. <span className={`${styles.action} ${styles.notice}`}>
  103. <Badge dot count={count}>
  104. <BellOutlined />
  105. </Badge>
  106. </span>
  107. </Popover>
  108. )
  109. }
  110. export default NotifyDropdown