bruno/renderer/components/Sidebar/Collections/Collection/index.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

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 { collectionClicked } from 'providers/ReduxStore/slices/collections';
import { useDispatch } from 'react-redux';
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);
const [showNewRequestModal, setShowNewRequestModal] = useState(false);
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) => {
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;
}
dispatch(collectionClicked(collection.uid));
2021-12-03 20:37:38 +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">
{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}
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)'}}/>
<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'>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
setShowNewRequestModal(true)
}}>
2022-03-13 18:31:16 +01:00
New Request
2022-01-01 10:07:34 +01:00
</div>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
2022-03-13 18:31:16 +01:00
setShowNewFolderModal(true)
}}>
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>
{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}
collectionUid={collection.uid}
2021-12-03 20:37:38 +01:00
/>
}) : null}
</div>
) : null}
</div>
</StyledWrapper>
);
};
export default Collection;