Skip to content

Commit

Permalink
Merge pull request #779 from Inist-CNRS/improve-redirect-format
Browse files Browse the repository at this point in the history
try to improve redirect format
  • Loading branch information
ThieryMichel committed Nov 15, 2018
2 parents 86045f8 + 10ffede commit f015f87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/app/js/formats/redirect/RedirectView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Helmet } from 'react-helmet';
import InvalidFormat from '../InvalidFormat';
import { field as fieldPropTypes } from '../../propTypes';
import { isURL } from '../../../../common/uris.js';

Expand All @@ -10,19 +12,26 @@ class RedirectView extends Component {

componentDidMount() {
const { field, resource } = this.props;
const value = resource[field.name];
const values = Array.isArray(value) ? value : [value];
const URLs = values.filter(v => isURL(v));
const url = URLs.pop();

if (url) {
const url = resource[field.name];
if (isURL(url)) {
window.location.href = url;
}
}

render() {
const { field, resource, className } = this.props;
return <span className={className}>{resource[field.name]}</span>;
const url = resource[field.name];
if (!isURL(url)) {
return <InvalidFormat format={field.format} value={url} />;
}
return (
<div className={className}>
<Helmet>
<link rel="canonical" href={url} />
</Helmet>
<a href={url}>{url}</a>
</div>
);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/app/js/formats/redirect/RedirectView.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import React from 'react';
import { shallow } from 'enzyme';

import InvalidFormat from '../InvalidFormat';
import RedirectView from './RedirectView';

describe('<RedirectView />', () => {
it('should render', () => {
it('should render if url', () => {
const resource = { foo: 'http://example.com' };
const field = { name: 'foo' };
const wrapper = shallow(
<RedirectView resource={resource} field={field} />,
);
expect(wrapper.find('a').text()).toEqual('http://example.com');
});
it('should render an InvalidFormat for an invalid value', () => {
const resource = { foo: 'Run you fools!' };
const field = { name: 'foo' };
const wrapper = shallow(
<RedirectView resource={resource} field={field} />,
);
expect(wrapper.find('span').text()).toEqual('Run you fools!');
expect(wrapper.find(InvalidFormat).length).toEqual(1);
});
});

0 comments on commit f015f87

Please sign in to comment.