PrintReportForm.tsx 3.69 KB
Newer Older
Maksym Tishchenko's avatar
Maksym Tishchenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import * as React from 'react';
import { connect } from 'react-redux';
import { Form, Field, FormProps, FormRenderProps } from 'react-final-form';
import { withTranslation, WithTranslation } from 'react-i18next';
// UI
import { Grid } from '@material-ui/core';
import withDialog from "ui/common/withDialog";
import RadioSelection from "@gringlobal-ce/client/ui/common/form/RadioSelection";
import { ReportService } from "@gringlobal-ce/client/service";
import { bindActionCreators } from "redux";
import { showSnackbar } from "@gringlobal-ce/client/action/snackbar";
import RepositoryFile from "@gringlobal-ce/client/model/repository/RepositoryFile";
import Loading from "@gringlobal-ce/client/ui/common/Loading";

Matija Obreza's avatar
Matija Obreza committed
15
16
const PrintReportForm = ({ t, onSubmit, initialValues, endpoint, reportsLoaded, showSnackbar, modelName, error }:
                        { showSnackbar: (msg) => void, onSubmit: (values) => void, endpoint: string, modelName: string, reportsLoaded: RepositoryFile[] } & FormProps & WithTranslation) => {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
17
18
19
20
21
22
23
24
  const [ selectedReportId, setSelectedReportId ] = React.useState(null)
  const [ internalError, setInternalError ] = React.useState(null)
  const [ reports, setReports ] = React.useState(reportsLoaded)

  React.useEffect(() => {
    if (reportsLoaded && !reports) {
      setReports(reportsLoaded)
    }
Matija Obreza's avatar
Matija Obreza committed
25
26
    if (!reports && !reportsLoaded && endpoint) {
      ReportService.getReportList(endpoint).then((data) => {
Maksym Tishchenko's avatar
Maksym Tishchenko committed
27
        if (data.length === 0) {
28
          setInternalError(t('public.c.generateReport.noReportsFound', { path: `/reports/${modelName}` }))
Maksym Tishchenko's avatar
Maksym Tishchenko committed
29
30
31
32
33
34
35
        }
        setReports(data)
      }).catch((e) => {
        console.log('Error getting reports', e)
        showSnackbar(t('common:label.errorHappened'));
      });
    }
Matija Obreza's avatar
Matija Obreza committed
36
  }, [reports, endpoint, reportsLoaded])
Maksym Tishchenko's avatar
Maksym Tishchenko committed
37
38

  const handleSubmit = () => {
39
40
41
42
    if (!reports || reports?.length === 0) {
      setInternalError(t('public.c.generateReport.noReportsFound', { path: `/reports/${modelName}` }))
      return
    }
Maksym Tishchenko's avatar
Maksym Tishchenko committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    if (reports?.length > 1 && !selectedReportId) {
      setInternalError(t('public.c.generateReport.noReportSelected'))
      return
    }
    const selectedReport = reports?.length === 1 ? reports[0] : reports.find((report) => +report.id === +selectedReportId)
    onSubmit({ report: selectedReport })
  }


  const renderLabel = (option) => {
    const selectedReport = reports?.find((report) => +report.id === +option)
    return `${ selectedReport.title ?? '' } ${ selectedReport.originalFilename }`.trim()
  }

  const selectTemplate = (e, val) => {
    setSelectedReportId(val)
    setInternalError(null)
  }

  return (
    <Form
      initialValues={ initialValues }
      onSubmit={ handleSubmit }
    >
      { (props: FormRenderProps) => (
        <form onSubmit={ props.handleSubmit } id="print-report-form">
          <Grid container spacing={ 4 }>
            { reports === null && <Loading/> }
            { reports?.length > 1 &&
              <Grid item xs={ 12 }>
                <Field
                  name="report"
                  component={ RadioSelection }
                  label={ t(`public.c.generateReport.selectReportTemplate`) }
                  onChange={ selectTemplate }
                  options={ reports?.map((report) => `${ report.id }`) }
                  renderOptionLabel={ renderLabel }
                />
              </Grid>
            }
            { (internalError || error) && <Grid item xs={ 12 } sm={ 12 } style={ { color: 'red' } }>{ internalError || error }</Grid> }
          </Grid>
        </form>
      ) }
    </Form>
  )
}

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

export default connect(null, mapDispatchToProps)(withDialog(withTranslation()(PrintReportForm)));