ExportArchiveDataPage.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import * as toastr from 'toastr';
  5. // import { toastSuccess, toastError } from '~/client/util/apiNotification';
  6. import { apiDelete, apiGet } from '~/client/util/apiv1-client';
  7. import { useAdminSocket } from '~/stores/socket-io';
  8. import LabeledProgressBar from './Common/LabeledProgressBar';
  9. import ArchiveFilesTable from './ExportArchiveData/ArchiveFilesTable';
  10. import SelectCollectionsModal from './ExportArchiveData/SelectCollectionsModal';
  11. const IGNORED_COLLECTION_NAMES = [
  12. 'sessions', 'rlflx', 'activities',
  13. ];
  14. class ExportArchiveDataPage extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. collections: [],
  19. zipFileStats: [],
  20. progressList: [],
  21. isExportModalOpen: false,
  22. isExporting: false,
  23. isZipping: false,
  24. isExported: false,
  25. };
  26. this.onZipFileStatAdd = this.onZipFileStatAdd.bind(this);
  27. this.onZipFileStatRemove = this.onZipFileStatRemove.bind(this);
  28. this.openExportModal = this.openExportModal.bind(this);
  29. this.closeExportModal = this.closeExportModal.bind(this);
  30. this.exportingRequestedHandler = this.exportingRequestedHandler.bind(this);
  31. }
  32. async UNSAFE_componentWillMount() {
  33. // TODO:: use apiv3.get
  34. // eslint-disable-next-line no-unused-vars
  35. const [{ collections }, { status }] = await Promise.all([
  36. apiGet('/v3/mongo/collections', {}),
  37. apiGet('/v3/export/status', {}),
  38. ]);
  39. // TODO: toastSuccess, toastError
  40. // filter only not ignored collection names
  41. const filteredCollections = collections.filter((collectionName) => {
  42. return !IGNORED_COLLECTION_NAMES.includes(collectionName);
  43. });
  44. const { zipFileStats, isExporting, progressList } = status;
  45. this.setState({
  46. collections: filteredCollections,
  47. zipFileStats,
  48. isExporting,
  49. progressList,
  50. });
  51. this.setupWebsocketEventHandler();
  52. }
  53. setupWebsocketEventHandler() {
  54. const { socket } = this.props;
  55. if (socket != null) {
  56. // websocket event
  57. socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
  58. this.setState({
  59. isExporting: true,
  60. progressList,
  61. });
  62. });
  63. // websocket event
  64. socket.on('admin:onStartZippingForExport', () => {
  65. this.setState({
  66. isZipping: true,
  67. });
  68. });
  69. // websocket event
  70. socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
  71. const zipFileStats = this.state.zipFileStats.concat([addedZipFileStat]);
  72. this.setState({
  73. isExporting: false,
  74. isZipping: false,
  75. isExported: true,
  76. zipFileStats,
  77. });
  78. // TODO: toastSuccess, toastError
  79. toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
  80. closeButton: true,
  81. progressBar: true,
  82. newestOnTop: false,
  83. showDuration: '100',
  84. hideDuration: '100',
  85. timeOut: '1200',
  86. extendedTimeOut: '150',
  87. });
  88. });
  89. }
  90. }
  91. onZipFileStatAdd(newStat) {
  92. this.setState((prevState) => {
  93. return {
  94. zipFileStats: [...prevState.zipFileStats, newStat],
  95. };
  96. });
  97. }
  98. async onZipFileStatRemove(fileName) {
  99. try {
  100. await apiDelete(`/v3/export/${fileName}`, {});
  101. this.setState((prevState) => {
  102. return {
  103. zipFileStats: prevState.zipFileStats.filter(stat => stat.fileName !== fileName),
  104. };
  105. });
  106. // TODO: toastSuccess, toastError
  107. toastr.success(undefined, `Deleted ${fileName}`, {
  108. closeButton: true,
  109. progressBar: true,
  110. newestOnTop: false,
  111. showDuration: '100',
  112. hideDuration: '100',
  113. timeOut: '1200',
  114. extendedTimeOut: '150',
  115. });
  116. }
  117. catch (err) {
  118. // TODO: toastSuccess, toastError
  119. toastr.error(err, 'Error', {
  120. closeButton: true,
  121. progressBar: true,
  122. newestOnTop: false,
  123. showDuration: '100',
  124. hideDuration: '100',
  125. timeOut: '3000',
  126. });
  127. }
  128. }
  129. openExportModal() {
  130. this.setState({ isExportModalOpen: true });
  131. }
  132. closeExportModal() {
  133. this.setState({ isExportModalOpen: false });
  134. }
  135. /**
  136. * event handler invoked when export process was requested successfully
  137. */
  138. exportingRequestedHandler() {
  139. }
  140. renderProgressBarsForCollections() {
  141. const cols = this.state.progressList.map((progressData) => {
  142. const { collectionName, currentCount, totalCount } = progressData;
  143. return (
  144. <div className="col-md-6" key={collectionName}>
  145. <LabeledProgressBar
  146. header={collectionName}
  147. currentCount={currentCount}
  148. totalCount={totalCount}
  149. />
  150. </div>
  151. );
  152. });
  153. return <div className="row px-3">{cols}</div>;
  154. }
  155. renderProgressBarForZipping() {
  156. const { isZipping, isExported } = this.state;
  157. const showZippingBar = isZipping || isExported;
  158. if (!showZippingBar) {
  159. return <></>;
  160. }
  161. return (
  162. <div className="row px-3">
  163. <div className="col-md-12" key="progressBarForZipping">
  164. <LabeledProgressBar
  165. header="Zip Files"
  166. currentCount={1}
  167. totalCount={1}
  168. isInProgress={isZipping}
  169. />
  170. </div>
  171. </div>
  172. );
  173. }
  174. render() {
  175. const { t } = this.props;
  176. const { isExporting, isExported, progressList } = this.state;
  177. const showExportingData = (isExported || isExporting) && (progressList != null);
  178. return (
  179. <div data-testid="admin-export-archive-data">
  180. <h2>{t('Export Archive Data')}</h2>
  181. <button type="button" className="btn btn-outline-secondary" disabled={isExporting} onClick={this.openExportModal}>
  182. {t('admin:export_management.create_new_archive_data')}
  183. </button>
  184. { showExportingData && (
  185. <div className="mt-5">
  186. <h3>{t('admin:export_management.exporting_collection_list')}</h3>
  187. { this.renderProgressBarsForCollections() }
  188. { this.renderProgressBarForZipping() }
  189. </div>
  190. ) }
  191. <div className="mt-5">
  192. <h3>{t('admin:export_management.exported_data_list')}</h3>
  193. <ArchiveFilesTable
  194. zipFileStats={this.state.zipFileStats}
  195. onZipFileStatRemove={this.onZipFileStatRemove}
  196. />
  197. </div>
  198. <SelectCollectionsModal
  199. isOpen={this.state.isExportModalOpen}
  200. onExportingRequested={this.exportingRequestedHandler}
  201. onClose={this.closeExportModal}
  202. collections={this.state.collections}
  203. />
  204. </div>
  205. );
  206. }
  207. }
  208. ExportArchiveDataPage.propTypes = {
  209. t: PropTypes.func.isRequired, // i18next
  210. socket: PropTypes.object,
  211. };
  212. const ExportArchiveDataPageWrapperFC = (props) => {
  213. const { t } = useTranslation();
  214. const { data: socket } = useAdminSocket();
  215. return <ExportArchiveDataPage t={t} socket={socket} {...props} />;
  216. };
  217. export default ExportArchiveDataPageWrapperFC;