99 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-08-24 15:09:42 +02:00
/**
2017-03-18 17:34:00 +01:00
*
* LeftMenuLink
*
*/
2016-08-24 15:09:42 +02:00
import React from 'react';
import { startsWith, upperFirst } from 'lodash';
import PropTypes from 'prop-types';
2017-08-17 15:39:09 +02:00
import { FormattedMessage } from 'react-intl';
2017-08-21 15:12:53 +02:00
import { Link } from 'react-router-dom';
2017-08-17 15:39:09 +02:00
import en from '../../translations/en.json';
2017-08-17 15:39:09 +02:00
import styles from './styles.scss';
2016-08-24 15:09:42 +02:00
2018-05-02 11:01:27 +02:00
class LeftMenuLink extends React.Component {
// eslint-disable-line react/prefer-stateless-function
2016-08-24 15:09:42 +02:00
render() {
2017-07-21 18:15:43 +02:00
// We need to create our own active url checker,
// because of the two levels router.
2018-05-02 11:01:27 +02:00
const isLinkActive = startsWith(
2018-08-08 16:30:04 +02:00
window.location.pathname.replace('/admin', '').concat('/'),
this.props.destination.concat('/'),
2018-05-02 11:01:27 +02:00
);
2018-08-08 16:30:04 +02:00
2018-05-02 11:01:27 +02:00
const plugin =
this.props.source !== 'content-manager' && this.props.source !== '' ? (
<div className={styles.plugin}>
<span>{upperFirst(this.props.source.split('-').join(' '))}</span>
</div>
) : (
''
);
2017-07-21 18:15:43 +02:00
// Check if messageId exists in en locale to prevent warning messages
const content = en[this.props.label] ? (
<FormattedMessage
id={this.props.label}
2018-05-02 11:01:27 +02:00
defaultMessage="{label}"
values={{
label: `${this.props.label}`,
}}
className={styles.linkLabel}
/>
) : (
<span className={styles.linkLabel}>{this.props.label}</span>
);
2019-01-10 16:11:58 +01:00
// Icon.
const icon = <i className={`${styles.linkIcon} fa-${this.props.icon} fa`} />;
// Create external or internal link.
const link = this.props.destination.includes('http')
? (
<a
className={`${styles.link} ${isLinkActive ? styles.linkActive : ''}`}
href={this.props.destination}
target="_blank"
>
{icon}
{content}
</a>
)
: (
2017-11-27 17:27:16 +01:00
<Link
className={`${styles.link} ${isLinkActive ? styles.linkActive : ''}`}
to={{
pathname: this.props.destination,
search: this.props.source ? `?source=${this.props.source}` : '',
2017-11-27 17:27:16 +01:00
}}
>
2019-01-10 16:11:58 +01:00
{icon}
{content}
2016-08-25 10:47:15 +02:00
</Link>
2019-01-10 16:11:58 +01:00
);
return (
<li className={styles.item}>
{link}
{plugin}
2016-08-24 15:09:42 +02:00
</li>
);
}
}
LeftMenuLink.propTypes = {
destination: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
2017-11-27 17:27:16 +01:00
source: PropTypes.string,
};
LeftMenuLink.defaultProps = {
source: '',
2016-08-24 15:09:42 +02:00
};
export default LeftMenuLink;