import React from 'react'; import PropTypes from 'prop-types'; import _ from 'lodash'; class ImportStatus extends React.Component { constructor(props) { super(props); this.state = { jobs: props.jobs }; this.pollingInterval = 5000; this.chunkSize = 50; this.links = _.isEmpty(props.links) ? [`${props.type}/:id`] : props.links; if (_.isEmpty(props.path)) { const k = `lcms_engine_import_status_admin_${this.props.type}_path`; this.path = Routes[k].call(); } else { this.path = props.path; } this.withPdf = props.with_pdf || false; } componentDidMount() { this.intervalFn = setInterval(this.poll.bind(this), this.pollingInterval); } componentWillUnmount() { clearInterval(this.intervalFn); } poll() { const pendingJobs = _.compact(_.map(this.state.jobs, (job, jid) => (job.status !== 'done' ? jid : null))); if (pendingJobs.length > 0) { _.each(_.chunk(pendingJobs, this.chunkSize), jids => this.updateChunkStatus(jids)); } else { clearInterval(this.intervalFn); } } updateChunkStatus(jids) { $.getJSON(this.path, { jids: jids, type: this.props.type, _: Date.now(), // prevent cached response }) .done(res => { let updatedJobs = {}; _.each(res, (val, jid) => { updatedJobs[jid] = _.extend(this.state.jobs[jid], { status: val.status }, val.result); }); this.setState({ jobs: _.extend(this.state.jobs, updatedJobs) }); }) .fail(res => { console.warn('check content export status', res); // eslint-disable-line no-console }); } resourceButton(job) { if (this.withPdf) { return ( ); } let linkWithParams = function (route, params = {}) { let path = route; _.each(params, (v, k) => { path = _.replace(path, `:${k}`, v); }); return path; }; return _.map(this.links, (link, idx) => ( )); } spinner() { return ( ); } render() { const waitingCount = _.filter(this.state.jobs, job => job.status !== 'done').length; const importedCount = _.filter(this.state.jobs, job => job.status === 'done' && job.ok).length; const failedCount = _.filter(this.state.jobs, job => job.status === 'done' && !job.ok).length; const results = _.map(this.state.jobs, (job, key) => { let status; if (job.status === 'done') { status = job.ok ? 'ok' : 'err'; } else { status = job.status; } return (
• {waitingCount} Files(s) Processing {`✓ ${importedCount} File(s) ${ this.withPdf ? 'Generated' : 'Imported' }`} x {failedCount} File(s) Failed