ExportArchiveDataPage.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import * as toastr from 'toastr';
  4. import { apiDelete } from '~/client/util/apiv1-client';
  5. import { apiv3Get } from '~/client/util/apiv3-client';
  6. import { useAdminSocket } from '~/stores/socket-io';
  7. import LabeledProgressBar from './Common/LabeledProgressBar';
  8. import ArchiveFilesTable from './ExportArchiveData/ArchiveFilesTable';
  9. import SelectCollectionsModal from './ExportArchiveData/SelectCollectionsModal';
  10. const IGNORED_COLLECTION_NAMES = [
  11. 'sessions', 'rlflx', 'activities',
  12. ];
  13. const ExportArchiveDataPage = (): JSX.Element => {
  14. const { data: socket } = useAdminSocket();
  15. const { t } = useTranslation('admin');
  16. const [collections, setCollections] = useState<any[]>([]);
  17. const [zipFileStats, setZipFileStats] = useState<any[]>([]);
  18. const [progressList, setProgressList] = useState<any[]>([]);
  19. const [isExportModalOpen, setExportModalOpen] = useState(false);
  20. const [isExporting, setExporting] = useState(false);
  21. const [isZipping, setZipping] = useState(false);
  22. const [isExported, setExported] = useState(false);
  23. const fetchData = useCallback(async() => {
  24. const [{ data: collectionsData }, { data: statusData }] = await Promise.all([
  25. apiv3Get<{collections: any[]}>('/mongo/collections', {}),
  26. apiv3Get<{status: { zipFileStats: any[], isExporting: boolean, progressList: any[] }}>('/export/status', {}),
  27. ]);
  28. // TODO: toastSuccess, toastError
  29. // filter only not ignored collection names
  30. const filteredCollections = collectionsData.collections.filter((collectionName) => {
  31. return !IGNORED_COLLECTION_NAMES.includes(collectionName);
  32. });
  33. const { zipFileStats, isExporting, progressList } = statusData.status;
  34. setCollections(filteredCollections);
  35. setZipFileStats(zipFileStats);
  36. setExporting(isExporting);
  37. setProgressList(progressList);
  38. }, []);
  39. const setupWebsocketEventHandler = useCallback(() => {
  40. if (socket != null) {
  41. // websocket event
  42. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  43. setExporting(true);
  44. setProgressList(progressList);
  45. });
  46. // websocket event
  47. socket.on('admin:onStartZippingForExport', () => {
  48. setZipping(true);
  49. });
  50. // websocket event
  51. socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
  52. setExporting(false);
  53. setZipping(false);
  54. setExported(true);
  55. setZipFileStats(prev => prev.concat([addedZipFileStat]));
  56. // TODO: toastSuccess, toastError
  57. toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
  58. closeButton: true,
  59. progressBar: true,
  60. newestOnTop: false,
  61. showDuration: '100',
  62. hideDuration: '100',
  63. timeOut: '1200',
  64. extendedTimeOut: '150',
  65. });
  66. });
  67. }
  68. }, [socket]);
  69. const onZipFileStatRemove = useCallback(async(fileName) => {
  70. try {
  71. await apiDelete(`/v3/export/${fileName}`, {});
  72. setZipFileStats(prev => prev.filter(stat => stat.fileName !== fileName));
  73. // TODO: toastSuccess, toastError
  74. toastr.success(undefined, `Deleted ${fileName}`, {
  75. closeButton: true,
  76. progressBar: true,
  77. newestOnTop: false,
  78. showDuration: '100',
  79. hideDuration: '100',
  80. timeOut: '1200',
  81. extendedTimeOut: '150',
  82. });
  83. }
  84. catch (err) {
  85. // TODO: toastSuccess, toastError
  86. toastr.error(err, 'Error', {
  87. closeButton: true,
  88. progressBar: true,
  89. newestOnTop: false,
  90. showDuration: '100',
  91. hideDuration: '100',
  92. timeOut: '3000',
  93. });
  94. }
  95. }, []);
  96. const exportingRequestedHandler = useCallback(() => {}, []);
  97. const renderProgressBarsForCollections = useCallback(() => {
  98. const cols = progressList.map((progressData) => {
  99. const { collectionName, currentCount, totalCount } = progressData;
  100. return (
  101. <div className="col-md-6" key={collectionName}>
  102. <LabeledProgressBar
  103. header={collectionName}
  104. currentCount={currentCount}
  105. totalCount={totalCount}
  106. />
  107. </div>
  108. );
  109. });
  110. return <div className="row px-3">{cols}</div>;
  111. }, [progressList]);
  112. const renderProgressBarForZipping = useCallback(() => {
  113. const showZippingBar = isZipping || isExported;
  114. if (!showZippingBar) {
  115. return <></>;
  116. }
  117. return (
  118. <div className="row px-3">
  119. <div className="col-md-12" key="progressBarForZipping">
  120. <LabeledProgressBar
  121. header="Zip Files"
  122. currentCount={1}
  123. totalCount={1}
  124. isInProgress={isZipping}
  125. />
  126. </div>
  127. </div>
  128. );
  129. }, [isExported, isZipping]);
  130. useEffect(() => {
  131. fetchData();
  132. setupWebsocketEventHandler();
  133. }, [fetchData, setupWebsocketEventHandler]);
  134. const showExportingData = (isExported || isExporting) && (progressList != null);
  135. return (
  136. <div data-testid="admin-export-archive-data">
  137. <h2>{t('export_management.export_archive_data')}</h2>
  138. <button type="button" className="btn btn-outline-secondary" disabled={isExporting} onClick={() => setExportModalOpen(true)}>
  139. {t('export_management.create_new_archive_data')}
  140. </button>
  141. { showExportingData && (
  142. <div className="mt-5">
  143. <h3>{t('export_management.exporting_collection_list')}</h3>
  144. { renderProgressBarsForCollections() }
  145. { renderProgressBarForZipping() }
  146. </div>
  147. ) }
  148. <div className="mt-5">
  149. <h3>{t('export_management.exported_data_list')}</h3>
  150. <ArchiveFilesTable
  151. zipFileStats={zipFileStats}
  152. onZipFileStatRemove={onZipFileStatRemove}
  153. />
  154. </div>
  155. <SelectCollectionsModal
  156. isOpen={isExportModalOpen}
  157. onExportingRequested={exportingRequestedHandler}
  158. onClose={() => setExportModalOpen(false)}
  159. collections={collections}
  160. />
  161. </div>
  162. );
  163. };
  164. export default ExportArchiveDataPage;