2022-03-13 15:29:10 +01:00
|
|
|
import React, { useState, forwardRef, useRef } from 'react';
|
2022-01-01 10:07:34 +01:00
|
|
|
import get from 'lodash/get';
|
2021-12-03 20:37:38 +01:00
|
|
|
import classnames from 'classnames';
|
2022-03-13 13:13:21 +01:00
|
|
|
import { IconChevronRight, IconDots } from '@tabler/icons';
|
|
|
|
import Dropdown from 'components/Dropdown';
|
2022-03-17 22:29:10 +01:00
|
|
|
import { collectionClicked } from 'providers/ReduxStore/slices/collections';
|
|
|
|
import { useDispatch } from 'react-redux';
|
2022-03-13 22:13:36 +01:00
|
|
|
import NewRequest from 'components/Sidebar/NewRequest';
|
2022-03-13 18:31:16 +01:00
|
|
|
import NewFolder from 'components/Sidebar/NewFolder';
|
2022-03-13 13:13:21 +01:00
|
|
|
import CollectionItem from './CollectionItem';
|
2021-12-03 20:37:38 +01:00
|
|
|
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
|
|
|
|
2022-03-13 13:13:21 +01:00
|
|
|
const Collection = ({collection}) => {
|
2022-03-13 18:31:16 +01:00
|
|
|
const [showNewFolderModal, setShowNewFolderModal] = useState(false);
|
2022-03-13 22:13:36 +01:00
|
|
|
const [showNewRequestModal, setShowNewRequestModal] = useState(false);
|
2022-03-17 22:29:10 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-03-13 13:13:21 +01:00
|
|
|
|
2022-01-01 10:07:34 +01:00
|
|
|
const menuDropdownTippyRef = useRef();
|
|
|
|
const onMenuDropdownCreate = (ref) => menuDropdownTippyRef.current = ref;
|
|
|
|
const MenuIcon = forwardRef((props, ref) => {
|
|
|
|
return (
|
|
|
|
<div ref={ref}>
|
|
|
|
<IconDots size={22}/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
2021-12-03 20:37:38 +01:00
|
|
|
|
|
|
|
const iconClassName = classnames({
|
|
|
|
'rotate-90': collection.collapsed
|
|
|
|
});
|
|
|
|
|
2022-01-01 10:07:34 +01:00
|
|
|
const handleClick = (event) => {
|
2022-03-13 15:59:47 +01:00
|
|
|
let tippyEl = get(menuDropdownTippyRef, 'current.reference');
|
|
|
|
if(tippyEl && tippyEl.contains && tippyEl.contains(event.target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event && event.target && event.target.className === 'dropdown-item') {
|
2022-01-01 10:07:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:29:10 +01:00
|
|
|
dispatch(collectionClicked(collection.uid));
|
2021-12-03 20:37:38 +01:00
|
|
|
};
|
|
|
|
|
2022-03-13 22:13:36 +01:00
|
|
|
const hideNewFolderModal = () => setShowNewFolderModal(false);
|
|
|
|
const hideNewRequestModal = () => setShowNewRequestModal(false);
|
2022-03-13 13:13:21 +01:00
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
return (
|
|
|
|
<StyledWrapper className="flex flex-col">
|
2022-03-13 22:13:36 +01:00
|
|
|
{showNewRequestModal && (
|
|
|
|
<NewRequest
|
|
|
|
collectionUid={collection.uid}
|
|
|
|
handleCancel={hideNewRequestModal}
|
|
|
|
handleClose={hideNewRequestModal}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-13 18:31:16 +01:00
|
|
|
{showNewFolderModal && (
|
|
|
|
<NewFolder
|
2022-03-13 15:29:10 +01:00
|
|
|
collectionUid={collection.uid}
|
2022-03-13 22:13:36 +01:00
|
|
|
handleCancel={hideNewFolderModal}
|
|
|
|
handleClose={hideNewFolderModal}
|
2022-03-13 15:29:10 +01:00
|
|
|
/>
|
|
|
|
)}
|
2021-12-03 20:37:38 +01:00
|
|
|
<div className="flex py-1 collection-name items-center" onClick={handleClick}>
|
|
|
|
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{width:16, color: 'rgb(160 160 160)'}}/>
|
2022-03-13 22:13:36 +01:00
|
|
|
<span className="ml-1">{collection.name}</span>
|
2022-01-01 10:07:34 +01:00
|
|
|
<div className="collection-actions">
|
|
|
|
<Dropdown onCreate={onMenuDropdownCreate} icon={<MenuIcon />} placement='bottom-start'>
|
2022-03-13 15:59:47 +01:00
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
menuDropdownTippyRef.current.hide();
|
2022-03-13 22:13:36 +01:00
|
|
|
setShowNewRequestModal(true)
|
2022-03-13 15:59:47 +01:00
|
|
|
}}>
|
2022-03-13 18:31:16 +01:00
|
|
|
New Request
|
2022-01-01 10:07:34 +01:00
|
|
|
</div>
|
2022-03-13 15:59:47 +01:00
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
menuDropdownTippyRef.current.hide();
|
2022-03-13 18:31:16 +01:00
|
|
|
setShowNewFolderModal(true)
|
2022-03-13 15:59:47 +01:00
|
|
|
}}>
|
2022-03-13 18:31:16 +01:00
|
|
|
New Folder
|
2022-01-01 10:07:34 +01:00
|
|
|
</div>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
2021-12-03 20:37:38 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
{collection.collapsed ? (
|
|
|
|
<div>
|
2022-03-13 22:13:36 +01:00
|
|
|
{collection.items && collection.items.length ? collection.items.map((i) => {
|
2021-12-03 20:37:38 +01:00
|
|
|
return <CollectionItem
|
2022-03-13 13:13:21 +01:00
|
|
|
key={i.uid}
|
2021-12-03 20:37:38 +01:00
|
|
|
item={i}
|
2022-03-13 22:13:36 +01:00
|
|
|
collectionUid={collection.uid}
|
2021-12-03 20:37:38 +01:00
|
|
|
/>
|
|
|
|
}) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</StyledWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Collection;
|