index.tsx 5.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as React from 'react';
import {translate} from 'react-i18next';
import {withStyles} from '@material-ui/core/styles';

// UI
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContentText from '@material-ui/core/DialogContentText';
import Article from 'model/cms/Article';
import Loading from 'ui/common/Loading';

/*tslint:disable*/
const styles = (theme) => ({
  root: {
    backgroundColor: 'white',
    margin: '0px 8px',
    '& > p': {
      fontFamily: 'Roboto-Regular',
      fontSize: '18px',
      color: '#4d4c46',
      lineHeight: '30px',
      marginTop: '20px',
      marginBottom: 0,
    },
    '& > h3': {
      fontSize: '24px',
      marginTop: '28px',
      paddingBottom: '6px',
    },
  },
  permButton: {
    [theme.breakpoints.down('sm')]: {
      width: '100%',
      margin: '8px 0',
    },
  },
  downloadBlock: {
    width: 'auto',
    display: 'flex' as 'flex',
    justifyContent: 'flex-start' as 'flex-start',
    paddingLeft: '24px',
    paddingBottom: '24px',
  },
  title: {
    backgroundColor: '#88ba42',
    '& > h6': {
      color: 'white',
      paddingLeft: '8px',
      marginBottom: 0,
      fontSize: '1.714rem',
      fontWeight: 'bold' as 'bold',
    }
  },
  alertDescription: {
    color: '#4d4c46',
  }

});
/*tslint:enable*/

interface IDownloadProps extends React.ClassAttributes<any> {
  classes: any;
  variant?: 'text' | 'outlined' | 'contained' | 'contained' | 'fab' | 'extendedFab';
  dataType: 'dwca' | 'pdci' | 'mcpd';
  from: 'acn' | 'wiews';
  buttonTitle: string;
  param: string;
  t: any;
  article: Article;
  accessionNumber?: number;
}

const WIEWS_DOWNLOAD_URL: string = `/proxy/api/v1/wiews/download`;
const ACN_DOWNLOAD_URL: string = `/proxy/api/v1/acn/download`;
const DOWNLOAD_LIMIT: number = 200000;

class Download extends React.Component<IDownloadProps, any> {

  public state = {
    open: false,
  };

  private show = () => {
    this.setState({open: true});
  }

  private hide = () => {
    this.setState({open: false});
  }

  private asyncHide = () => setTimeout(() => this.hide());

  public render() {
    const {variant = 'text', t, buttonTitle, from, param, dataType, classes, article, accessionNumber } = this.props;

    let downloadUrl: string;
    switch (from) {
      case 'wiews': downloadUrl = WIEWS_DOWNLOAD_URL; break;
      case 'acn': downloadUrl = ACN_DOWNLOAD_URL; break;
      default: break;
    }

    return (
      <div style={ { margin: '0 4px' } }>
        <Button onClick={ this.show } className={ this.props.classes.permButton } variant={ variant }>{ buttonTitle }</Button>
        { this.state.open &&
          <div>
            { accessionNumber && accessionNumber >= DOWNLOAD_LIMIT ?
                <Dialog
                  open={ this.state.open }
                  onClose={ this.hide }
                >
                  <DialogTitle id="alert-dialog-title">
                    <p>{ t('common.downloadDialog.refusing') }</p>
                  </DialogTitle>
                  <DialogContent>
                    <DialogContentText className={ classes.alertDescription }>
                      { t(`common.downloadDialog.warningMessage`, {value: DOWNLOAD_LIMIT}) }
                    </DialogContentText>
                  </DialogContent>
                  <DialogActions>
                    <Button onClick={ this.hide } autoFocus>{ t('common:action.OK') }</Button>
                  </DialogActions>
                </Dialog>
                :
                <Dialog
                  open={ this.state.open }
                  onClose={ this.hide }
                  maxWidth="lg"
                  fullWidth
                >
                  <DialogTitle className={ classes.title }>{ t('common.downloadDialog.title') }</DialogTitle>
                  <DialogContent>
                    {
                      article && <div className={ classes.root } dangerouslySetInnerHTML={ {__html: article.body} }/>
                        ||  <Loading />
                    }
                  </DialogContent>
                  <DialogActions className={ classes.downloadBlock }>
                    <form method="post" action={ `${downloadUrl}` } >
                      <input type="hidden" name={ `${dataType}` } value={ `${dataType}` } />
                      { from === 'acn' && <input type="hidden" name="f" value={ `${param}` } /> }
                      { from === 'wiews' && <input type="hidden" name="wiewsCode" value={ `${param}` } /> }
                      <Button variant="text" className={ `back-green` } onClick={ this.asyncHide } type="submit" autoFocus>
                        <span className="white">{ t('common.downloadDialog.button') }</span>
                      </Button>
                    </form>
                  </DialogActions>
                </Dialog>
              }
          </div>
        }
      </div>
    );
  }
}

export default translate()(withStyles(styles)(Download));