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';
|
|
|
|
import actions from 'providers/Store/actions'
|
|
|
|
import { useStore } from 'providers/Store';
|
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 13:13:21 +01:00
|
|
|
const [store, storeDispatch] = useStore();
|
|
|
|
|
|
|
|
const {
|
|
|
|
activeRequestTabId
|
|
|
|
} = store;
|
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-13 13:13:21 +01:00
|
|
|
storeDispatch({
|
2021-12-03 20:37:38 +01:00
|
|
|
type: actions.SIDEBAR_COLLECTION_CLICK,
|
2022-03-13 13:13:21 +01:00
|
|
|
collectionUid: collection.uid
|
2021-12-03 20:37:38 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-13 18:31:16 +01:00
|
|
|
const hideAddFolderModal = () => setShowNewFolderModal(false);
|
2022-03-13 13:13:21 +01:00
|
|
|
const collectionItems = get(collection, 'current.items');
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
return (
|
|
|
|
<StyledWrapper className="flex flex-col">
|
2022-03-13 18:31:16 +01:00
|
|
|
{showNewFolderModal && (
|
|
|
|
<NewFolder
|
2022-03-13 15:29:10 +01:00
|
|
|
collectionUid={collection.uid}
|
|
|
|
handleCancel={hideAddFolderModal}
|
|
|
|
handleClose={hideAddFolderModal}
|
|
|
|
/>
|
|
|
|
)}
|
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 13:13:21 +01:00
|
|
|
<span className="ml-1">{collection.current.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 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 13:13:21 +01:00
|
|
|
{collectionItems && collectionItems.length ? collectionItems.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}
|
|
|
|
collectionId={collection.id}
|
|
|
|
/>
|
|
|
|
}) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</StyledWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Collection;
|