Commit 3a239c02 authored by Oleksii Savran's avatar Oleksii Savran Committed by Matija Obreza
Browse files

Bug: Extra CMS request for Download dialog article

parent cb2189f8
......@@ -6,7 +6,6 @@ import { parse } from 'query-string';
// Actions
import { applyFilters, loadMoreAccessions, updateRoute } from 'accessions/actions/public';
import {loadArticlePromise} from 'cms/actions/public';
// Models
import Page from 'model/Page';
......@@ -39,25 +38,20 @@ class BrowsePage extends BrowsePageTemplate<Accession> {
}
public state = {
downloadArticle: null,
authenticated: false,
};
public componentWillMount() {
super.componentWillMount();
const { userRoles, loadArticlePromise, lang } = this.props;
const { userRoles } = this.props;
const authenticated: boolean = userRoles.findIndex((role) => role === 'ROLE_USER') !== -1;
this.setState({authenticated});
const slug: string = authenticated ? 'download-authenticated' : 'download-anonymous';
if (!this.state.downloadArticle || (this.state.downloadArticle.slug !== slug)) {
loadArticlePromise(lang, slug).then((data) => this.setState({downloadArticle: data}));
}
}
public render() {
const { paged, loadMoreData, filterCode, currentTab, t} = this.props;
const slug: string = this.state.authenticated ? 'download-authenticated' : 'download-anonymous';
const renderAccession = (s: Accession, index: number) => {
return <AccessionCard key={ s.uuid } index={ index } accession={ s } />;
};
......@@ -81,14 +75,14 @@ class BrowsePage extends BrowsePageTemplate<Accession> {
{ this.state.authenticated &&
<DownloadDialog downloadUrl="/proxy/api/v1/acn/download"
postParams={ { mcpd: 'mcpd', f: paged && paged.filterCode || '' } }
article={ this.state.downloadArticle }
disabled={ paged && paged.totalElements > DOWNLOAD_LIMIT }
slug={ slug }
buttonTitle={ `${t('common:action.download')} ${t('accessions.public.p.display.MCPD')}` } />
}
<DownloadDialog downloadUrl="/proxy/api/v1/acn/download"
article={ this.state.downloadArticle }
postParams={ { dwca: 'dwca', f: paged && paged.filterCode || '' } }
disabled={ paged && paged.totalElements > DOWNLOAD_LIMIT }
slug={ slug }
buttonTitle={ `${t('common:action.download')} ${t('accessions.public.p.display.zip')}` } />
</ButtonBar>
}
......@@ -124,14 +118,12 @@ const mapStateToProps = (state, ownProps) => ({
filterCode: ownProps.match.params.filterCode,
currentTab: ownProps.match.params.tab || 'data', // current tab, or ownProps.location.pathname
userRoles: state.login.authorities,
lang: state.applicationConfig.lang,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
applyFilters,
loadMoreData: loadMoreAccessions,
updateRoute,
loadArticlePromise,
}, dispatch);
......
......@@ -33,8 +33,6 @@ import Permissions from 'ui/common/permission/Permissions';
import FaoInstitute from 'model/genesys/FaoInstitute';
import ButtonBar from 'ui/common/buttons/ButtonBar';
import DownloadDialog from 'ui/common/download-dialog';
import {loadArticlePromise} from 'cms/actions/public';
import Article from 'model/cms/Article';
interface IDisplayPageProps extends React.ClassAttributes<any> {
t: any;
......@@ -47,8 +45,6 @@ interface IDisplayPageProps extends React.ClassAttributes<any> {
applyFilters: any;
applyOverviewFilters: any;
userRoles: string[];
loadArticlePromise: (locale: string, slug: string) => Promise<Article>;
lang: string;
}
const mobile = ['sm', 'xs'] as Breakpoint[];
......@@ -64,7 +60,6 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
}
public state = {
downloadArticle: null,
authenticated: false,
};
......@@ -98,7 +93,7 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
}
public componentWillMount() {
const { institute, code, loadInstitute, lang, userRoles, loadArticlePromise } = this.props;
const { institute, code, loadInstitute, userRoles } = this.props;
if (code && (! institute || code !== institute.details.code)) {
console.log(`Reloading institute data for code=${code}`, institute);
loadInstitute(code);
......@@ -106,14 +101,11 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
const authenticated: boolean = userRoles.findIndex((role) => role === 'ROLE_USER') !== -1;
this.setState({authenticated});
const slug: string = authenticated ? 'download-authenticated' : 'download-anonymous';
if (!this.state.downloadArticle || (this.state.downloadArticle.slug !== slug)) {
loadArticlePromise(lang, slug).then((data) => this.setState({downloadArticle: data}));
}
}
public render() {
const { error, institute, code, width, t } = this.props;
const slug: string = this.state.authenticated ? 'download-authenticated' : 'download-anonymous';
const stillLoading: boolean = ! error && (! institute || (code && institute && institute.details.code !== code));
const isMobile = mobile.indexOf(width) !== -1;
......@@ -164,19 +156,19 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
<Button onClick={ this.applyFilterForOverview }>{ t('accessions.tab.overview') }</Button>
{ this.state.authenticated &&
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
article={ this.state.downloadArticle }
slug={ slug }
postParams={ { mcpd: 'mcpd' } }
buttonTitle={ `${t('common:action.download')} ${t('institutes.public.p.display.MCPD')}` } />
}
{ this.state.authenticated &&
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
slug={ slug }
postParams={ { pdci: 'pdci' } }
article={ this.state.downloadArticle }
buttonTitle={ `${t('common:action.download')} ${t('institutes.public.p.display.PDCI_short')}` } />
}
<DownloadDialog downloadUrl={ `/proxy/api/v1/wiews/${code}/download` }
slug={ slug }
postParams={ { dwca: 'dwca' } }
article={ this.state.downloadArticle }
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 }/> }
......@@ -306,14 +298,12 @@ const mapStateToProps = (state, ownProps) => ({
error: state.institutes.public.instituteError,
code: ownProps.match.params.wiewsCode,
userRoles: state.login.authorities,
lang: state.applicationConfig.lang,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loadInstitute,
applyFilters,
applyOverviewFilters,
loadArticlePromise,
}, dispatch);
......
import * as React from 'react';
import {translate} from 'react-i18next';
import {withStyles} from '@material-ui/core/styles';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
// Actions
import {loadArticle} from 'cms/actions/public';
// UI
import Button from '@material-ui/core/Button';
......@@ -70,7 +75,10 @@ interface IDownloadProps extends React.ClassAttributes<any> {
buttonTitle: string;
postParams: object;
article: Article;
slug: string;
lang: string;
disabled?: boolean;
loadArticle: (lang: string, slug: string) => void;
}
export const DOWNLOAD_LIMIT: number = 200000;
......@@ -82,6 +90,10 @@ class DownloadDialog extends React.Component<IDownloadProps, any> {
};
private show = () => {
const { loadArticle, slug, lang, article } = this.props;
if (!article || (article.slug !== slug)) {
loadArticle(lang, slug);
}
this.setState({open: true});
}
......@@ -151,4 +163,15 @@ class DownloadDialog extends React.Component<IDownloadProps, any> {
}
}
export default translate()(withStyles(styles)(DownloadDialog));
const mapStateToProps = (state, ownProps) => ({
lang: state.applicationConfig.lang,
article: state.cms.public.articles[ownProps.slug],
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loadArticle,
}, dispatch);
export default translate()(connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(DownloadDialog)));
Supports Markdown
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