PrintReportDialog.tsx 3.23 KB
Newer Older
Maksym Tishchenko's avatar
Maksym Tishchenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import * as React from 'react';
import { connect } from 'react-redux';
import { WithTranslation, withTranslation } from 'react-i18next';
import { bindActionCreators } from 'redux';

import { withWidth, WithWidth } from '@material-ui/core';
import PrintReportForm from 'common/PrintReportForm';
import { showSnackbar } from "@gringlobal-ce/client/action/snackbar";
import { ReportService } from "@gringlobal-ce/client/service";
import RepositoryFile from "@gringlobal-ce/client/model/repository/RepositoryFile";
import { printPdfReport } from "@gringlobal-ce/client/utilities";
import { closeDialog } from "@gringlobal-ce/client/action/dialog";

interface IPrintReportDialogProps extends React.ClassAttributes<any>, WithTranslation, WithWidth {
  showSnackbar: (message: string) => void;
  selectedItemIds: number[]
  dialogKey: string;
  dialogKeyOpened: string;
  closeDialog: () => void;
  modelName: string;
Matija Obreza's avatar
Matija Obreza committed
21
  endpoint: string;
Maksym Tishchenko's avatar
Maksym Tishchenko committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
}

class PrintReportDialog extends React.Component<IPrintReportDialogProps, any> {
  public state = {
    error: null,
    reports: null
  }

  public componentDidUpdate(prevProps: Readonly<IPrintReportDialogProps>) {
    const { dialogKey, dialogKeyOpened } = this.props;

    if (!prevProps.dialogKeyOpened && dialogKeyOpened === dialogKey) {
      this.checkMultiple()
    }
  }

  private checkMultiple = () => {
Matija Obreza's avatar
Matija Obreza committed
39
    const { endpoint, modelName, t } = this.props;
Maksym Tishchenko's avatar
Maksym Tishchenko committed
40

Matija Obreza's avatar
Matija Obreza committed
41
42
    ReportService.getReportList(endpoint).then((reports) => {
      if (reports.length > 0) {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
43
        this.setState({ reports })
Matija Obreza's avatar
Matija Obreza committed
44
45
46
47
48
49
        if (reports.length === 1) {
          // this.handlePrintReport({ report: reports[0] })
          // return;
        }
      } else {
        this.setState({ reports, error: t('public.c.generateReport.noReportsFound', { path: `/reports/${modelName}` }) })
Maksym Tishchenko's avatar
Maksym Tishchenko committed
50
51
52
53
54
55
56
      }
    }).catch((e) => {
      this.setState({ error: e.data && e.data.error || e.toString() });
    });
  }

  public handlePrintReport = ({ report }: { report: RepositoryFile }) => {
Matija Obreza's avatar
Matija Obreza committed
57
    const { endpoint, showSnackbar, closeDialog, selectedItemIds, t } = this.props;
Maksym Tishchenko's avatar
Maksym Tishchenko committed
58

Matija Obreza's avatar
Matija Obreza committed
59
    printPdfReport(endpoint, report.originalFilename, selectedItemIds).then((pdfReport) => {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
60
61
62
63
64
65
66
67
68
69
70
      showSnackbar(t('public.c.generateReport.reportReady'));
      closeDialog()
    })

  }

  private resetError = () => {
    this.setState({ error: null })
  }

  public render() {
Matija Obreza's avatar
Matija Obreza committed
71
    const { t, width, endpoint, modelName, dialogKeyOpened, closeDialog } = this.props;
Maksym Tishchenko's avatar
Maksym Tishchenko committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    const { error, reports } = this.state;

    return (
      <PrintReportForm
        isOpen={ !!dialogKeyOpened }
        onClose={ closeDialog }
        onSubmit={ this.handlePrintReport }
        formId="print-report-form"
        title={ t('common:action.generatePdf') }
        submitButtonLabel={ t('common:action.generatePdf') }
        size="md"
        fullScreen={ width === 'xs' }
        error={ error }
        resetError={ this.resetError }
        reportsLoaded={ reports }
Matija Obreza's avatar
Matija Obreza committed
87
        endpoint={ endpoint }
Maksym Tishchenko's avatar
Maksym Tishchenko committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        modelName={ modelName }
      />
    );
  }
}

const mapStateToProps = (state) => ({
  dialogKeyOpened: state.dialog.key,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  showSnackbar,
  closeDialog
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(withTranslation()(withWidth()(PrintReportDialog)));