2019-11-05 17:23:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Link } from 'react-router';
|
2020-08-22 10:50:49 +00:00
|
|
|
import { withTranslation } from 'react-i18next';
|
2019-11-05 17:23:57 +00:00
|
|
|
|
|
|
|
import Icons from './Icons';
|
|
|
|
|
|
|
|
const Item = ({
|
2020-08-22 10:50:49 +00:00
|
|
|
isAdded, onSelect, name, url, t
|
2020-05-05 23:03:58 +00:00
|
|
|
}) => {
|
|
|
|
const buttonLabel = isAdded ? 'Remove from collection' : 'Add to collection';
|
|
|
|
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 10:50:49 +00:00
|
|
|
{t('QuickAddList.View')}
|
2020-05-05 23:03:58 +00:00
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|
2019-11-05 17:23:57 +00:00
|
|
|
|
|
|
|
const ItemType = PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
isAdded: PropTypes.bool.isRequired,
|
|
|
|
});
|
|
|
|
|
|
|
|
Item.propTypes = {
|
2020-02-04 23:40:54 +00:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
isAdded: PropTypes.bool.isRequired,
|
2019-11-05 17:23:57 +00:00
|
|
|
onSelect: PropTypes.func.isRequired,
|
2020-08-22 10:50:49 +00:00
|
|
|
t: PropTypes.func.isRequired
|
2019-11-05 17:23:57 +00:00
|
|
|
};
|
|
|
|
|
2019-11-10 16:08:00 +00:00
|
|
|
const QuickAddList = ({
|
2020-08-22 10:50:49 +00:00
|
|
|
items, onAdd, onRemove, t
|
2019-11-10 16:08:00 +00:00
|
|
|
}) => {
|
|
|
|
const handleAction = (item) => {
|
|
|
|
if (item.isAdded) {
|
|
|
|
onRemove(item);
|
|
|
|
} else {
|
|
|
|
onAdd(item);
|
2019-11-05 17:23:57 +00:00
|
|
|
}
|
2019-11-10 16:08:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="quick-add">{items.map(item => (<Item
|
|
|
|
key={item.id}
|
2020-08-22 10:50:49 +00:00
|
|
|
t={t}
|
2019-11-10 16:08:00 +00:00
|
|
|
{...item}
|
|
|
|
onSelect={
|
2020-02-11 21:16:43 +00:00
|
|
|
(event) => {
|
2020-04-10 21:01:40 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.currentTarget.blur();
|
2020-02-11 21:16:43 +00:00
|
|
|
handleAction(item);
|
|
|
|
}
|
2019-11-10 16:08:00 +00:00
|
|
|
}
|
|
|
|
/>))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
2019-11-05 17:23:57 +00:00
|
|
|
|
|
|
|
QuickAddList.propTypes = {
|
|
|
|
items: PropTypes.arrayOf(ItemType).isRequired,
|
2019-11-10 16:08:00 +00:00
|
|
|
onAdd: PropTypes.func.isRequired,
|
|
|
|
onRemove: PropTypes.func.isRequired,
|
2020-08-22 10:50:49 +00:00
|
|
|
t: PropTypes.func.isRequired
|
2019-11-05 17:23:57 +00:00
|
|
|
};
|
|
|
|
|
2020-08-22 10:50:49 +00:00
|
|
|
export default withTranslation()(QuickAddList);
|