InAppNotificationPage.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { FC, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import AppContainer from '~/client/services/AppContainer';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import InAppNotificationList from './InAppNotificationList';
  7. import { useSWRxInAppNotifications } from '../../stores/in-app-notification';
  8. import PaginationWrapper from '../PaginationWrapper';
  9. import CustomNavAndContents from '../CustomNavigation/CustomNavAndContents';
  10. import { InAppNotificationStatuses } from '~/interfaces/in-app-notification';
  11. type Props = {
  12. appContainer: AppContainer
  13. }
  14. const InAppNotificationPageBody: FC<Props> = (props) => {
  15. const { appContainer } = props;
  16. const limit = appContainer.config.pageLimitationXL;
  17. const [activePage, setActivePage] = useState(1);
  18. const offset = (activePage - 1) * limit;
  19. const { data: inAppNotificationData } = useSWRxInAppNotifications(limit, offset);
  20. const [activeUnopenedNotificationPage, setActiveUnopenedPage] = useState(1);
  21. const UnopenedNotificationOffset = (activeUnopenedNotificationPage - 1) * limit;
  22. const { data: unopendNotificationData } = useSWRxInAppNotifications(limit, UnopenedNotificationOffset, InAppNotificationStatuses.STATUS_UNOPENED);
  23. const { t } = useTranslation();
  24. if (inAppNotificationData == null) {
  25. return (
  26. <div className="wiki">
  27. <div className="text-muted text-center">
  28. <i className="fa fa-2x fa-spinner fa-pulse mr-1"></i>
  29. </div>
  30. </div>
  31. );
  32. }
  33. const setPageNumber = (selectedPageNumber): void => {
  34. setActivePage(selectedPageNumber);
  35. };
  36. const setUnopenedPageNumber = (selectedPageNumber): void => {
  37. setActiveUnopenedPage(selectedPageNumber);
  38. };
  39. // commonize notification lists by 81953
  40. const AllInAppNotificationList = () => {
  41. return (
  42. <>
  43. <InAppNotificationList inAppNotificationData={inAppNotificationData} />
  44. <PaginationWrapper
  45. activePage={activePage}
  46. changePage={setPageNumber}
  47. totalItemsCount={inAppNotificationData.totalDocs}
  48. pagingLimit={inAppNotificationData.limit}
  49. align="center"
  50. size="sm"
  51. />
  52. </>
  53. );
  54. };
  55. // commonize notification lists by 81953
  56. const UnopenedInAppNotificationList = () => {
  57. return (
  58. <>
  59. <div className="mb-2 d-flex justify-content-end">
  60. <button
  61. type="button"
  62. className="btn btn-outline-primary"
  63. // TODO: set "UNOPENED" notification status "OPEND" by 81951
  64. // onClick={}
  65. >
  66. {t('in_app_notification.mark_all_as_read')}
  67. </button>
  68. </div>
  69. <InAppNotificationList inAppNotificationData={unopendNotificationData} />
  70. <PaginationWrapper
  71. activePage={activeUnopenedNotificationPage}
  72. changePage={setUnopenedPageNumber}
  73. totalItemsCount={inAppNotificationData.totalDocs}
  74. pagingLimit={inAppNotificationData.limit}
  75. align="center"
  76. size="sm"
  77. />
  78. </>
  79. );
  80. };
  81. const navTabMapping = {
  82. user_infomation: {
  83. Icon: () => <></>,
  84. Content: AllInAppNotificationList,
  85. i18n: t('in_app_notification.all'),
  86. index: 0,
  87. },
  88. external_accounts: {
  89. Icon: () => <></>,
  90. Content: UnopenedInAppNotificationList,
  91. i18n: t('in_app_notification.unopend'),
  92. index: 1,
  93. },
  94. };
  95. return (
  96. <CustomNavAndContents navTabMapping={navTabMapping} />
  97. );
  98. };
  99. const InAppNotificationPage = withUnstatedContainers(InAppNotificationPageBody, [AppContainer]);
  100. export default InAppNotificationPage;
  101. InAppNotificationPageBody.propTypes = {
  102. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  103. };