InAppNotificationDropdown.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useState } from 'react';
  2. import { Dropdown, DropdownToggle, DropdownMenu } from 'reactstrap';
  3. // import DropdownMenu from './InAppNotificationDropdown/DropdownMenu';
  4. // import Icon from './Common/Icon'
  5. // import Crowi from 'client/util/Crowi'
  6. // import { Notification } from 'client/types/crowi'
  7. interface Props {
  8. // crowi: Crowi
  9. me: string
  10. }
  11. interface State {
  12. count: number
  13. loaded: boolean
  14. notifications: Notification[]
  15. isOpen: boolean
  16. }
  17. const InAppNotificationDropdown = (props: Props) => {
  18. const [count, setCount] = useState(0);
  19. const [isLoaded, setIsLoaded] = useState(false);
  20. const [notifications, setNotifications] = useState([]);
  21. const [isOpen, setIsOpen] = useState(false);
  22. // componentDidMount() {
  23. // this.initializeSocket();
  24. // this.fetchList();
  25. // this.fetchStatus();
  26. // }
  27. // initializeSocket() {
  28. // this.props.crowi.getWebSocket().on('notification updated', (data: { user: string }) => {
  29. // if (this.props.me === data.user) {
  30. // this.fetchList();
  31. // this.fetchStatus();
  32. // }
  33. // });
  34. // }
  35. // async fetchStatus() {
  36. // try {
  37. // const { count = null } = await this.props.crowi.apiGet('/notification.status');
  38. // if (count !== null && count !== this.state.count) {
  39. // this.setState({ count });
  40. // }
  41. // }
  42. // catch (err) {
  43. // // TODO: error handling
  44. // }
  45. // }
  46. const updateStatus = () => {
  47. try {
  48. // await this.props.crowi.apiPost('/notification.read');
  49. setCount(0);
  50. }
  51. catch (err) {
  52. // TODO: error handling
  53. }
  54. };
  55. // async fetchList() {
  56. // const limit = 6;
  57. // try {
  58. // const { notifications } = await this.props.crowi.apiGet('/notification.list', { limit });
  59. // this.setState({ loaded: true, notifications });
  60. // }
  61. // catch (err) {
  62. // // TODO: error handling
  63. // }
  64. // }
  65. const onToggleDropdown = () => {
  66. if (isOpen != null && count > 0) {
  67. updateStatus();
  68. }
  69. setIsOpen(!isOpen);
  70. };
  71. // async handleNotificationOnClick(notification: Notification) {
  72. // try {
  73. // await this.props.crowi.apiPost('/notification.open', { id: notification._id });
  74. // // jump to target page
  75. // window.location.href = notification.target.path;
  76. // }
  77. // catch (err) {
  78. // // TODO: error handling
  79. // }
  80. // }
  81. const badge = count > 0 ? <span className="badge badge-pill badge-danger notification-badge">{count}</span> : '';
  82. return (
  83. <Dropdown className="notification-wrapper" isOpen={isOpen} toggle={onToggleDropdown}>
  84. <DropdownToggle tag="a" className="nav-link">
  85. <i className="icon-bell mr-2"></i>
  86. {badge}
  87. </DropdownToggle>
  88. <DropdownMenu>hoge</DropdownMenu>
  89. </Dropdown>
  90. );
  91. };
  92. export default InAppNotificationDropdown;