InAppNotificationPage.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 [activePageOfAllNotificationCat, setActivePage] = useState(1);
  18. const offsetOfAllNotificationCat = (activePageOfAllNotificationCat - 1) * limit;
  19. const { data: allNotificationData } = useSWRxInAppNotifications(limit, offsetOfAllNotificationCat);
  20. const [activePageOfUnopenedNotificationCat, setActiveUnopenedNotificationPage] = useState(1);
  21. const offsetOfUnopenedNotificationCat = (activePageOfUnopenedNotificationCat - 1) * limit;
  22. const { t } = useTranslation();
  23. if (allNotificationData == null) {
  24. return (
  25. <div className="wiki">
  26. <div className="text-muted text-center">
  27. <i className="fa fa-2x fa-spinner fa-pulse mr-1"></i>
  28. </div>
  29. </div>
  30. );
  31. }
  32. const setAllNotificationPageNumber = (selectedPageNumber): void => {
  33. setActivePage(selectedPageNumber);
  34. };
  35. const setUnopenedPageNumber = (selectedPageNumber): void => {
  36. setActiveUnopenedNotificationPage(selectedPageNumber);
  37. };
  38. // commonize notification lists by 81953
  39. const AllInAppNotificationList = () => {
  40. return (
  41. <>
  42. <InAppNotificationList inAppNotificationData={allNotificationData} />
  43. <PaginationWrapper
  44. activePage={activePageOfAllNotificationCat}
  45. changePage={setAllNotificationPageNumber}
  46. totalItemsCount={allNotificationData.totalDocs}
  47. pagingLimit={allNotificationData.limit}
  48. align="center"
  49. size="sm"
  50. />
  51. </>
  52. );
  53. };
  54. // commonize notification lists by 81953
  55. const UnopenedInAppNotificationList = () => {
  56. const { data: unopendNotificationData } = useSWRxInAppNotifications(limit, offsetOfUnopenedNotificationCat, InAppNotificationStatuses.STATUS_UNOPENED);
  57. if (unopendNotificationData == null) {
  58. return (
  59. <div className="wiki">
  60. <div className="text-muted text-center">
  61. <i className="fa fa-2x fa-spinner fa-pulse mr-1"></i>
  62. </div>
  63. </div>
  64. );
  65. }
  66. return (
  67. <>
  68. <div className="mb-2 d-flex justify-content-end">
  69. <button
  70. type="button"
  71. className="btn btn-outline-primary"
  72. // TODO: set "UNOPENED" notification status "OPEND" by 81951
  73. // onClick={}
  74. >
  75. {t('in_app_notification.mark_all_as_read')}
  76. </button>
  77. </div>
  78. <InAppNotificationList inAppNotificationData={unopendNotificationData} />
  79. <PaginationWrapper
  80. activePage={activePageOfUnopenedNotificationCat}
  81. changePage={setUnopenedPageNumber}
  82. totalItemsCount={unopendNotificationData.totalDocs}
  83. pagingLimit={unopendNotificationData.limit}
  84. align="center"
  85. size="sm"
  86. />
  87. </>
  88. );
  89. };
  90. const navTabMapping = {
  91. user_infomation: {
  92. Icon: () => <></>,
  93. Content: AllInAppNotificationList,
  94. i18n: t('in_app_notification.all'),
  95. index: 0,
  96. },
  97. external_accounts: {
  98. Icon: () => <></>,
  99. Content: UnopenedInAppNotificationList,
  100. i18n: t('in_app_notification.unopend'),
  101. index: 1,
  102. },
  103. };
  104. return (
  105. <CustomNavAndContents navTabMapping={navTabMapping} />
  106. );
  107. };
  108. const InAppNotificationPage = withUnstatedContainers(InAppNotificationPageBody, [AppContainer]);
  109. export default InAppNotificationPage;
  110. InAppNotificationPageBody.propTypes = {
  111. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  112. };