2019-11-05 17:23:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Link } from 'react-router';
|
|
|
|
|
|
|
|
import Icons from './Icons';
|
|
|
|
|
|
|
|
const Item = ({
|
|
|
|
isAdded, onSelect, name, url
|
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()}
|
|
|
|
>
|
|
|
|
View
|
|
|
|
</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,
|
|
|
|
};
|
|
|
|
|
2019-11-10 16:08:00 +00:00
|
|
|
const QuickAddList = ({
|
|
|
|
items, onAdd, onRemove
|
|
|
|
}) => {
|
|
|
|
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}
|
|
|
|
{...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,
|
2019-11-05 17:23:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default QuickAddList;
|