InAppNotificationDropdown.tsx 3.0 KB

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