2019-11-05 18:23:57 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Link } from 'react-router';
|
2020-08-22 12:50:49 +02:00
|
|
|
import { withTranslation } from 'react-i18next';
|
2019-11-05 18:23:57 +01:00
|
|
|
|
|
|
|
import Icons from './Icons';
|
|
|
|
|
|
|
|
const Item = ({
|
2020-08-22 12:50:49 +02:00
|
|
|
isAdded, onSelect, name, url, t
|
2020-05-06 01:03:58 +02:00
|
|
|
}) => {
|
2020-08-26 14:06:37 +02:00
|
|
|
const buttonLabel = isAdded ? t('QuickAddList.ButtonRemoveARIA') : t('QuickAddList.ButtonAddToCollectionARIA');
|
2020-05-06 01:03:58 +02:00
|
|
|
return (
|
|
|
|
<li className="quick-add__item" onClick={onSelect}> { /* eslint-disable-line */ }
|
|
|
|
<button className="quick-add__item-toggle" onClick={onSelect} aria-label={buttonLabel}>
|
|
|
|
<Icons isAdded={isAdded} />
|
|
|
|
</button>
|
|
|
|
<span className="quick-add__item-name">{name}</span>
|
|
|
|
<Link
|
|
|
|
className="quick-add__item-view"
|
|
|
|
to={url}
|
|
|
|
target="_blank"
|
|
|
|
onClick={e => e.stopPropogation()}
|
|
|
|
>
|
2020-08-22 12:50:49 +02:00
|
|
|
{t('QuickAddList.View')}
|
2020-05-06 01:03:58 +02:00
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|
2019-11-05 18:23:57 +01:00
|
|
|
|
|
|
|
const ItemType = PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
isAdded: PropTypes.bool.isRequired,
|
|
|
|
});
|
|
|
|
|
|
|
|
Item.propTypes = {
|
2020-02-05 00:40:54 +01:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
isAdded: PropTypes.bool.isRequired,
|
2019-11-05 18:23:57 +01:00
|
|
|
onSelect: PropTypes.func.isRequired,
|
2020-08-22 12:50:49 +02:00
|
|
|
t: PropTypes.func.isRequired
|
2019-11-05 18:23:57 +01:00
|
|
|
};
|
|
|
|
|
2019-11-10 17:08:00 +01:00
|
|
|
const QuickAddList = ({
|
2020-08-22 12:50:49 +02:00
|
|
|
items, onAdd, onRemove, t
|
2019-11-10 17:08:00 +01:00
|
|
|
}) => {
|
|
|
|
const handleAction = (item) => {
|
|
|
|
if (item.isAdded) {
|
|
|
|
onRemove(item);
|
|
|
|
} else {
|
|
|
|
onAdd(item);
|
2019-11-05 18:23:57 +01:00
|
|
|
}
|
2019-11-10 17:08:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="quick-add">{items.map(item => (<Item
|
|
|
|
key={item.id}
|
2020-08-22 12:50:49 +02:00
|
|
|
t={t}
|
2019-11-10 17:08:00 +01:00
|
|
|
{...item}
|
|
|
|
onSelect={
|
2020-02-11 22:16:43 +01:00
|
|
|
(event) => {
|
2020-04-10 23:01:40 +02:00
|
|
|
event.stopPropagation();
|
|
|
|
event.currentTarget.blur();
|
2020-02-11 22:16:43 +01:00
|
|
|
handleAction(item);
|
|
|
|
}
|
2019-11-10 17:08:00 +01:00
|
|
|
}
|
|
|
|
/>))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
2019-11-05 18:23:57 +01:00
|
|
|
|
|
|
|
QuickAddList.propTypes = {
|
|
|
|
items: PropTypes.arrayOf(ItemType).isRequired,
|
2019-11-10 17:08:00 +01:00
|
|
|
onAdd: PropTypes.func.isRequired,
|
|
|
|
onRemove: PropTypes.func.isRequired,
|
2020-08-22 12:50:49 +02:00
|
|
|
t: PropTypes.func.isRequired
|
2019-11-05 18:23:57 +01:00
|
|
|
};
|
|
|
|
|
2020-08-22 12:50:49 +02:00
|
|
|
export default withTranslation()(QuickAddList);
|