mailtrain/client/src/reports/List.js
2018-12-28 05:33:07 +01:00

208 lines
No EOL
7.1 KiB
JavaScript

'use strict';
import React, {Component} from 'react';
import {withTranslation} from '../lib/i18n';
import {
NavButton,
requiresAuthenticatedUser,
Title,
Toolbar,
withPageHelpers
} from '../lib/page';
import {Table} from '../lib/table';
import {
withAsyncErrorHandler,
withErrorHandling
} from '../lib/error-handling';
import moment
from 'moment';
import axios
from '../lib/axios';
import {ReportState} from '../../../shared/reports';
import {Icon} from "../lib/bootstrap-components";
import {checkPermissions} from "../lib/permissions";
import {getUrl} from "../lib/urls";
import {
tableAddDeleteButton,
tableRestActionDialogInit,
tableRestActionDialogRender
} from "../lib/modals";
import {withComponentMixins} from "../lib/decorator-helpers";
@withComponentMixins([
withTranslation,
withErrorHandling,
withPageHelpers,
requiresAuthenticatedUser
])
export default class List extends Component {
constructor(props) {
super(props);
this.state = {};
tableRestActionDialogInit(this);
}
@withAsyncErrorHandler
async fetchPermissions() {
const result = await checkPermissions({
createReport: {
entityTypeId: 'namespace',
requiredOperations: ['createReport']
},
executeReportTemplate: {
entityTypeId: 'reportTemplate',
requiredOperations: ['execute']
},
createReportTemplate: {
entityTypeId: 'namespace',
requiredOperations: ['createReportTemplate']
},
viewReportTemplate: {
entityTypeId: 'reportTemplate',
requiredOperations: ['view']
}
});
this.setState({
createPermitted: result.data.createReport && result.data.executeReportTemplate,
templatesPermitted: result.data.createReportTemplate || result.data.viewReportTemplate
});
}
componentDidMount() {
// noinspection JSIgnoredPromiseFromCall
this.fetchPermissions();
}
@withAsyncErrorHandler
async stop(table, id) {
await axios.post(getUrl(`rest/report-stop/${id}`));
table.refresh();
}
@withAsyncErrorHandler
async start(table, id) {
await axios.post(getUrl(`rest/report-start/${id}`));
table.refresh();
}
render() {
const t = this.props.t;
const columns = [
{ data: 1, title: t('name') },
{ data: 2, title: t('template') },
{ data: 3, title: t('description') },
{ data: 4, title: t('created'), render: data => data ? moment(data).fromNow() : '' },
{ data: 5, title: t('namespace') },
{
actions: data => {
const actions = [];
const perms = data[8];
const permsReportTemplate = data[9];
let viewContent, startStop, refreshTimeout;
const state = data[6];
const id = data[0];
const mimeType = data[7];
if (state === ReportState.PROCESSING || state === ReportState.SCHEDULED) {
viewContent = {
label: <Icon icon="hourglass" title={t('processing-1')}/>,
};
startStop = {
label: <Icon icon="stop" title={t('stop')}/>,
action: (table) => this.stop(table, id)
};
refreshTimeout = 1000;
} else if (state === ReportState.FINISHED) {
if (mimeType === 'text/html') {
viewContent = {
label: <Icon icon="eye" title={t('view')}/>,
link: `/reports/${id}/view`
};
} else if (mimeType === 'text/csv') {
viewContent = {
label: <Icon icon="file-download" title={t('download')}/>,
href: getUrl(`rpts/${id}/download`)
};
}
startStop = {
label: <Icon icon="redo" title={t('refreshReport')}/>,
action: (table) => this.start(table, id)
};
} else if (state === ReportState.FAILED) {
viewContent = {
label: <Icon icon="thumbs-down" title={t('reportGenerationFailed')}/>,
};
startStop = {
label: <Icon icon="redo" title={t('regenerateReport')}/>,
action: (table) => this.start(table, id)
};
}
if (perms.includes('viewContent')) {
actions.push(viewContent);
}
if (perms.includes('viewOutput')) {
actions.push(
{
label: <Icon icon="tv" title={t('viewConsoleOutput')}/>,
link: `/reports/${id}/output`
}
);
}
if (perms.includes('execute') && permsReportTemplate.includes('execute')) {
actions.push(startStop);
}
if (perms.includes('edit') && permsReportTemplate.includes('execute')) {
actions.push({
label: <Icon icon="edit" title={t('edit')}/>,
link: `/reports/${id}/edit`
});
}
if (perms.includes('share')) {
actions.push({
label: <Icon icon="share" title={t('share')}/>,
link: `/reports/${id}/share`
});
}
tableAddDeleteButton(actions, this, perms, `rest/reports/${data[0]}`, data[1], t('deletingReport'), t('reportDeleted'));
return { refreshTimeout, actions };
}
}
];
return (
<div>
{tableRestActionDialogRender(this)}
<Toolbar>
{this.state.createPermitted &&
<NavButton linkTo="/reports/create" className="btn-primary" icon="plus" label={t('createReport')}/>
}
{this.state.templatesPermitted &&
<NavButton linkTo="/reports/templates" className="btn-primary" label={t('reportTemplates')}/>
}
</Toolbar>
<Title>{t('reports')}</Title>
<Table ref={node => this.table = node} withHeader dataUrl="rest/reports-table" columns={columns} />
</div>
);
}
}