InAppNotificationDropdown.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { useState, useEffect, FC } from 'react';
  2. import {
  3. Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
  4. } from 'reactstrap';
  5. import loggerFactory from '~/utils/logger';
  6. import AppContainer from '../../client/services/AppContainer';
  7. import { withUnstatedContainers } from '../UnstatedUtils';
  8. import { IInAppNotification } from '../../interfaces/in-app-notification';
  9. import { InAppNotification } from './InAppNotification';
  10. import SocketIoContainer from '../../client/services/SocketIoContainer';
  11. const logger = loggerFactory('growi:InAppNotificationDropdown');
  12. type Props = {
  13. appContainer: AppContainer,
  14. socketIoContainer: SocketIoContainer,
  15. };
  16. const InAppNotificationDropdown: FC<Props> = (props: Props) => {
  17. const { appContainer } = 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. useEffect(() => {
  23. initializeSocket(props);
  24. fetchNotificationStatus();
  25. }, []);
  26. const initializeSocket = (props) => {
  27. const socket = props.socketIoContainer.getSocket();
  28. socket.on('notificationUpdated', (data: { userId: string, count: number }) => {
  29. setCount(data.count);
  30. // eslint-disable-next-line no-console
  31. console.log('socketData', data);
  32. });
  33. };
  34. const updateNotificationStatus = async() => {
  35. try {
  36. await appContainer.apiv3Post('/in-app-notification/read');
  37. setCount(0);
  38. }
  39. catch (err) {
  40. logger.error(err);
  41. }
  42. };
  43. const fetchNotificationStatus = async() => {
  44. try {
  45. const res = await appContainer.apiv3Get('/in-app-notification/status');
  46. const { count } = res.data;
  47. setCount(count);
  48. }
  49. catch (err) {
  50. logger.error(err);
  51. }
  52. };
  53. /**
  54. * TODO: Fetch notification list by GW-7473
  55. */
  56. const fetchNotificationList = async() => {
  57. const limit = 6;
  58. try {
  59. const paginationResult = await appContainer.apiv3Get('/in-app-notification/list', { limit });
  60. setNotifications(paginationResult.data.docs);
  61. setIsLoaded(true);
  62. }
  63. catch (err) {
  64. logger.error(err);
  65. }
  66. };
  67. const toggleDropdownHandler = () => {
  68. if (isOpen === false && count > 0) {
  69. updateNotificationStatus();
  70. }
  71. const newIsOpenState = !isOpen;
  72. setIsOpen(newIsOpenState);
  73. if (newIsOpenState === true) {
  74. fetchNotificationList();
  75. }
  76. };
  77. /**
  78. * TODO: Jump to the page by clicking on the notification by GW-7472
  79. */
  80. const notificationClickHandler = async(notification: Notification) => {
  81. try {
  82. // await this.props.crowi.apiPost('/notification.open', { id: notification._id });
  83. // jump to target page
  84. // window.location.href = notification.target.path;
  85. }
  86. catch (err) {
  87. logger.error(err);
  88. }
  89. };
  90. const RenderUnLoadedInAppNotification = (): JSX.Element => {
  91. return (
  92. <i className="fa fa-spinner"></i>
  93. );
  94. };
  95. const RenderEmptyInAppNotification = (): JSX.Element => {
  96. return (
  97. // TODO: apply i18n by #78569
  98. <>You had no notifications, yet.</>
  99. );
  100. };
  101. // TODO: improve renderInAppNotificationList by GW-7535
  102. // refer to https://github.com/crowi/crowi/blob/eecf2bc821098d2516b58104fe88fae81497d3ea/client/components/Notification/Notification.tsx
  103. const RenderInAppNotificationList = () => {
  104. console.log('notificationsHoge', notifications);
  105. if (notifications.length === 0) {
  106. return <RenderEmptyInAppNotification />;
  107. }
  108. const notificationList = notifications.map((notification: IInAppNotification) => {
  109. return (
  110. <div className="d-flex flex-row" key={notification._id}>
  111. <InAppNotification notification={notification} onClick={notificationClickHandler} />
  112. </div>
  113. );
  114. });
  115. return <>{notificationList}</>;
  116. };
  117. const InAppNotificationContents = (): JSX.Element => {
  118. if (!isLoaded) {
  119. return <RenderUnLoadedInAppNotification />;
  120. }
  121. return <RenderInAppNotificationList />;
  122. };
  123. const badge = count > 0 ? <span className="badge badge-pill badge-danger grw-notification-badge">{count}</span> : '';
  124. return (
  125. <Dropdown className="notification-wrapper" isOpen={isOpen} toggle={toggleDropdownHandler}>
  126. <DropdownToggle tag="a">
  127. <button type="button" className="nav-link border-0 bg-transparent waves-effect waves-light">
  128. <i className="icon-bell mr-2" /> {badge}
  129. </button>
  130. </DropdownToggle>
  131. <DropdownMenu className="px-2" right>
  132. <InAppNotificationContents />
  133. <DropdownItem divider />
  134. {/* TODO: Able to show all notifications by #79317 */}
  135. <a className="dropdown-item d-flex justify-content-center">See All</a>
  136. </DropdownMenu>
  137. </Dropdown>
  138. );
  139. };
  140. /**
  141. * Wrapper component for using unstated
  142. */
  143. const InAppNotificationDropdownWrapper = withUnstatedContainers(InAppNotificationDropdown, [AppContainer, SocketIoContainer]);
  144. export default InAppNotificationDropdownWrapper;