| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import React, { Fragment } from 'react';
- import PropTypes from 'prop-types';
- import { withTranslation } from 'react-i18next';
- import * as toastr from 'toastr';
- import { createSubscribedElement } from '../UnstatedUtils';
- // import { toastSuccess, toastError } from '../../../util/apiNotification';
- import AppContainer from '../../services/AppContainer';
- import WebsocketContainer from '../../services/WebsocketContainer';
- import ProgressBar from './Common/ProgressBar';
- import SelectCollectionsModal from './ExportArchiveData/SelectCollectionsModal';
- import ArchiveFilesTable from './ExportArchiveData/ArchiveFilesTable';
- class ExportArchiveDataPage extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- collections: [],
- zipFileStats: [],
- progressList: [],
- isExportModalOpen: false,
- isExporting: false,
- isZipping: false,
- isExported: false,
- };
- this.onZipFileStatAdd = this.onZipFileStatAdd.bind(this);
- this.onZipFileStatRemove = this.onZipFileStatRemove.bind(this);
- this.openExportModal = this.openExportModal.bind(this);
- this.closeExportModal = this.closeExportModal.bind(this);
- this.exportingRequestedHandler = this.exportingRequestedHandler.bind(this);
- }
- async componentWillMount() {
- // TODO:: use apiv3.get
- // eslint-disable-next-line no-unused-vars
- const [{ collections }, { status }] = await Promise.all([
- this.props.appContainer.apiGet('/v3/mongo/collections', {}),
- this.props.appContainer.apiGet('/v3/export/status', {}),
- ]);
- // TODO: toastSuccess, toastError
- const { zipFileStats, isExporting, progressList } = status;
- this.setState({
- collections,
- zipFileStats,
- isExporting,
- progressList,
- });
- this.setupWebsocketEventHandler();
- }
- setupWebsocketEventHandler() {
- const socket = this.props.websocketContainer.getWebSocket();
- // websocket event
- socket.on('admin:onProgressForExport', ({ currentCount, totalCount, progressList }) => {
- this.setState({
- isExporting: true,
- progressList,
- });
- });
- // websocket event
- socket.on('admin:onStartZippingForExport', () => {
- this.setState({
- isZipping: true,
- });
- });
- // websocket event
- socket.on('admin:onTerminateForExport', ({ addedZipFileStat }) => {
- const zipFileStats = this.state.zipFileStats.concat([addedZipFileStat]);
- this.setState({
- isExporting: false,
- isZipping: false,
- isExported: true,
- zipFileStats,
- });
- // TODO: toastSuccess, toastError
- toastr.success(undefined, `New Archive Data '${addedZipFileStat.fileName}' is added`, {
- closeButton: true,
- progressBar: true,
- newestOnTop: false,
- showDuration: '100',
- hideDuration: '100',
- timeOut: '1200',
- extendedTimeOut: '150',
- });
- });
- }
- onZipFileStatAdd(newStat) {
- this.setState((prevState) => {
- return {
- zipFileStats: [...prevState.zipFileStats, newStat],
- };
- });
- }
- async onZipFileStatRemove(fileName) {
- try {
- await this.props.appContainer.apiDelete(`/v3/export/${fileName}`, {});
- this.setState((prevState) => {
- return {
- zipFileStats: prevState.zipFileStats.filter(stat => stat.fileName !== fileName),
- };
- });
- // TODO: toastSuccess, toastError
- toastr.success(undefined, `Deleted ${fileName}`, {
- closeButton: true,
- progressBar: true,
- newestOnTop: false,
- showDuration: '100',
- hideDuration: '100',
- timeOut: '1200',
- extendedTimeOut: '150',
- });
- }
- catch (err) {
- // TODO: toastSuccess, toastError
- toastr.error(err, 'Error', {
- closeButton: true,
- progressBar: true,
- newestOnTop: false,
- showDuration: '100',
- hideDuration: '100',
- timeOut: '3000',
- });
- }
- }
- openExportModal() {
- this.setState({ isExportModalOpen: true });
- }
- closeExportModal() {
- this.setState({ isExportModalOpen: false });
- }
- /**
- * event handler invoked when export process was requested successfully
- */
- exportingRequestedHandler() {
- }
- renderProgressBarsForCollections() {
- const cols = this.state.progressList.map((progressData) => {
- const { collectionName, currentCount, totalCount } = progressData;
- return (
- <div className="col-md-6" key={collectionName}>
- <ProgressBar
- header={collectionName}
- currentCount={currentCount}
- totalCount={totalCount}
- />
- </div>
- );
- });
- return <div className="row px-3">{cols}</div>;
- }
- renderProgressBarForZipping() {
- const { isZipping, isExported } = this.state;
- const showZippingBar = isZipping || isExported;
- if (!showZippingBar) {
- return <></>;
- }
- return (
- <div className="row px-3">
- <div className="col-md-12" key="progressBarForZipping">
- <ProgressBar
- header="Zip Files"
- currentCount={1}
- totalCount={1}
- isInProgress={isZipping}
- />
- </div>
- </div>
- );
- }
- render() {
- const { t } = this.props;
- const { isExporting, isExported, progressList } = this.state;
- const showExportingData = (isExported || isExporting) && (progressList != null);
- return (
- <Fragment>
- <h2>{t('Export Archive Data')}</h2>
- <button type="button" className="btn btn-default" disabled={isExporting} onClick={this.openExportModal}>
- {t('export_management.create_new_archive_data')}
- </button>
- { showExportingData && (
- <div className="mt-5">
- <h3>{t('export_management.exporting_collection_list')}</h3>
- { this.renderProgressBarsForCollections() }
- { this.renderProgressBarForZipping() }
- </div>
- ) }
- <div className="mt-5">
- <h3>{t('export_management.exported_data_list')}</h3>
- <ArchiveFilesTable
- zipFileStats={this.state.zipFileStats}
- onZipFileStatRemove={this.onZipFileStatRemove}
- />
- </div>
- <SelectCollectionsModal
- isOpen={this.state.isExportModalOpen}
- onExportingRequested={this.exportingRequestedHandler}
- onClose={this.closeExportModal}
- collections={this.state.collections}
- />
- </Fragment>
- );
- }
- }
- ExportArchiveDataPage.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
- };
- /**
- * Wrapper component for using unstated
- */
- const ExportArchiveDataPageWrapper = (props) => {
- return createSubscribedElement(ExportArchiveDataPage, props, [AppContainer, WebsocketContainer]);
- };
- export default withTranslation()(ExportArchiveDataPageWrapper);
|