Commit 27f1a630 authored by Matija Obreza's avatar Matija Obreza
Browse files

Refactored DownloadDialog

- accepts downloadUrl + postParams object
parent 79d96428
......@@ -24,7 +24,7 @@ import AccessionCard from 'accessions/ui/c/AccessionCard';
import Tabs, { Tab } from 'ui/common/Tabs';
import AccessionFilters from './c/Filters';
import ButtonBar from 'ui/common/buttons/ButtonBar';
import Download from 'ui/common/download-acce-dialog';
import DownloadDialog, { DOWNLOAD_LIMIT } from 'ui/common/download-dialog';
class BrowsePage extends BrowsePageTemplate<Accession> {
protected static needs = [
......@@ -79,18 +79,16 @@ class BrowsePage extends BrowsePageTemplate<Accession> {
actions={
<ButtonBar>
{ this.state.authenticated &&
<Download dataType="mcpd"
<DownloadDialog downloadUrl="/proxy/api/v1/acn/download"
postParams={ { mcpd: 'mcpd', f: paged && paged.filterCode || '' } }
article={ this.state.downloadArticle }
param={ paged && paged.filterCode || '' }
from="acn"
accessionNumber={ paged && paged.totalElements }
disabled={ paged && paged.totalElements > DOWNLOAD_LIMIT }
buttonTitle={ `${t('common:action.download')} ${t('accessions.public.p.display.MCPD')}` } />
}
<Download dataType="dwca"
<DownloadDialog downloadUrl="/proxy/api/v1/acn/download"
article={ this.state.downloadArticle }
param={ paged && paged.filterCode || '' }
from="acn"
accessionNumber={ paged && paged.totalElements }
postParams={ { dwca: 'dwca', f: paged && paged.filterCode || '' } }
disabled={ paged && paged.totalElements > DOWNLOAD_LIMIT }
buttonTitle={ `${t('common:action.download')} ${t('accessions.public.p.display.zip')}` } />
</ButtonBar>
}
......
......@@ -32,7 +32,7 @@ import Button from '@material-ui/core/Button';
import Permissions from 'ui/common/permission/Permissions';
import FaoInstitute from 'model/genesys/FaoInstitute';
import ButtonBar from 'ui/common/buttons/ButtonBar';
import Download from 'ui/common/download-acce-dialog';
import DownloadDialog from 'ui/common/download-dialog';
import {loadArticlePromise} from 'cms/actions/public';
import Article from 'model/cms/Article';
......@@ -163,23 +163,20 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
<Button onClick={ this.applyInstituteCodeFilter }>{ t('institutes.public.p.display.browseAccessions') }</Button>
<Button onClick={ this.applyFilterForOverview }>{ t('accessions.tab.overview') }</Button>
{ this.state.authenticated &&
<Download dataType="mcpd"
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
article={ this.state.downloadArticle }
param={ code }
from="wiews"
postParams={ { mcpd: 'mcpd' } }
buttonTitle={ `${t('common:action.download')} ${t('institutes.public.p.display.MCPD')}` } />
}
{ this.state.authenticated &&
<Download dataType="pdci"
param={ code }
from="wiews"
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
postParams={ { pdci: 'pdci' } }
article={ this.state.downloadArticle }
buttonTitle={ `${t('common:action.download')} ${t('institutes.public.p.display.PDCI_short')}` } />
}
<Download dataType="dwca"
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
postParams={ { dwca: 'dwca' } }
article={ this.state.downloadArticle }
param={ code }
from="wiews"
buttonTitle={ `${t('common:action.download')} ${t('institutes.public.p.display.zip')}` } />
{ institute.details._permissions.manage && <Link to={ `/dashboard/wiews/${institute.details.code}/edit` }><Button>{ t('common:action.edit') }</Button></Link> }
{ institute.details._permissions.manage && <Permissions clazz={ FaoInstitute.clazz } id={ institute.details.id }/> }
......
......@@ -63,21 +63,19 @@ const styles = (theme) => ({
interface IDownloadProps extends React.ClassAttributes<any> {
classes: any;
t: any;
variant?: 'text' | 'outlined' | 'contained' | 'contained' | 'fab' | 'extendedFab';
dataType: 'dwca' | 'pdci' | 'mcpd';
from: 'acn' | 'wiews';
downloadUrl: string;
buttonTitle: string;
param: string;
t: any;
postParams: object;
article: Article;
accessionNumber?: number;
disabled?: boolean;
}
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;
export const DOWNLOAD_LIMIT: number = 200000;
class Download extends React.Component<IDownloadProps, any> {
class DownloadDialog extends React.Component<IDownloadProps, any> {
public state = {
open: false,
......@@ -94,21 +92,16 @@ class Download extends React.Component<IDownloadProps, any> {
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;
}
const {variant = 'text', t, buttonTitle, downloadUrl, postParams, classes, article } = this.props;
let { disabled } = this.props;
disabled = !!disabled || !downloadUrl;
return (
<div style={ { margin: '0 4px' } }>
<Button onClick={ this.show } className={ this.props.classes.permButton } variant={ variant }>{ buttonTitle }</Button>
<Button onClick={ this.show } disabled={ disabled } className={ this.props.classes.permButton } variant={ variant }>{ buttonTitle }</Button>
{ this.state.open &&
<div>
{ accessionNumber && accessionNumber >= DOWNLOAD_LIMIT ?
{ !!disabled ?
<Dialog
open={ this.state.open }
onClose={ this.hide }
......@@ -141,9 +134,9 @@ class Download extends React.Component<IDownloadProps, any> {
</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}` } /> }
{ postParams && Object.keys(postParams).map((key) => (
<input key={ key } type="hidden" name={ key } value={ postParams[key] } />
)) }
<Button variant="text" className={ `back-green` } onClick={ this.asyncHide } type="submit" autoFocus>
<span className="white">{ t('common.downloadDialog.button') }</span>
</Button>
......@@ -158,4 +151,4 @@ class Download extends React.Component<IDownloadProps, any> {
}
}
export default translate()(withStyles(styles)(Download));
export default translate()(withStyles(styles)(DownloadDialog));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment