import PropTypes from 'prop-types'; import React from 'react'; import InlineSVG from 'react-inlinesvg'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as CollectionsActions from '../../actions/collections'; import getSortedCollections from '../../selectors/collections'; import exitUrl from '../../../../images/exit.svg'; import Loader from '../../../App/components/loader'; import { Searchbar } from '../Searchbar'; import Item from './Item'; const NoCollections = () => (

No collections

); const projectInCollection = (project, collection) => ( collection.items.find(item => item.project.id === project.id) != null ); const CollectionPopover = ({ loading, onClose, project, collections, addToCollection, removeFromCollection, getCollections, user }) => { const [didLoadData, setDidLoadData] = React.useState(null); const [searchTerm, setSearchTerm] = React.useState(''); const filteredCollections = searchTerm === '' ? collections : collections.filter(({ name }) => name.toUpperCase().includes(searchTerm.toUpperCase())); React.useEffect(() => { getCollections(user.username); }, [user]); React.useEffect(() => { if (didLoadData === true) { return; } if (loading && didLoadData === null) { setDidLoadData(false); } else if (!loading && didLoadData === false) { setDidLoadData(true); } }, [loading]); const handleAddToCollection = (collectionId) => { addToCollection(collectionId, project.id); }; const handleRemoveFromCollection = (collectionId) => { removeFromCollection(collectionId, project.id); }; let content = null; if (didLoadData && collections.length === 0) { content = ; } else if (didLoadData) { content = ( ); } else { content = ; } return (

Add to collection

setSearchTerm('')} />
{content}
); }; CollectionPopover.propTypes = { loading: PropTypes.bool.isRequired, onClose: PropTypes.func.isRequired, getCollections: PropTypes.func.isRequired, addToCollection: PropTypes.func.isRequired, removeFromCollection: PropTypes.func.isRequired, user: PropTypes.shape({ username: PropTypes.string.isRequired, }).isRequired, collections: PropTypes.arrayOf(PropTypes.shape({ name: PropTypes.string, })).isRequired, project: PropTypes.shape({ id: PropTypes.string, }).isRequired, }; function mapStateToProps(state, ownProps) { return { user: state.user, collections: getSortedCollections(state), loading: state.loading, }; } function mapDispatchToProps(dispatch) { return bindActionCreators( Object.assign({}, CollectionsActions), dispatch ); } export default connect(mapStateToProps, mapDispatchToProps)(CollectionPopover);