docbrown/app/javascript/listings/singleListing/DropdownMenu.jsx
Michael Kohl 7f75f99560
[deploy] Rename classified listings (#7910)
* Change models and related files

* Update controllers and specs

* More renaming

* Seek and destroy, I mean search and replace

* Round up the stragglers

* Ground control to Major Travis...

* More fixes

* PR feedback

* Various fixes

* Rename view

* Fix list query builder

* Unify request specs

* Fix some API spec errors

* Fix remaining API specs

* Make spec conform to API

* Fix leftover problems

* Fix JS tests

* Fix column name in select

* Fix API specs

* Fix search specs

* Paging Mr. Travis
2020-05-27 13:35:09 +00:00

91 lines
2.2 KiB
JavaScript

import PropTypes from 'prop-types';
import { h, Component, createRef } from 'preact';
// eslint-disable-next-line import/no-unresolved
import ThreeDotsIcon from 'images/three-dots.svg';
import listingPropTypes from './listingPropTypes';
const MenuButton = ({ onClick }) => (
<button
type="button"
className="dropdown-btn"
aria-label="Toggle dropdown menu"
onClick={onClick}
>
<img
src={ThreeDotsIcon}
className="dropdown-icon"
alt="Dropdown menu icon"
/>
</button>
);
MenuButton.propTypes = {
onClick: PropTypes.func.isRequired,
};
class DropdownMenu extends Component {
componentRef = createRef();
static propTypes = {
isOwner: PropTypes.bool.isRequired,
listing: listingPropTypes.isRequired,
};
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
}
toggleMenu = () => {
const { isOpen } = this.state;
this.setState({ isOpen: !isOpen }, this.addOrRemoveClickOutsideHandler);
};
addOrRemoveClickOutsideHandler = () => {
const { isOpen } = this.state;
return isOpen
? document.addEventListener('mousedown', this.handleClickOutside)
: document.removeEventListener('mousedown', this.handleClickOutside);
};
handleClickOutside = (e) => {
if (
this.componentRef.current &&
!this.componentRef.current.contains(e.target)
) {
this.toggleMenu();
}
};
render() {
const { listing, isOwner } = this.props;
const { isOpen } = this.state;
const { id, category, slug } = listing;
const editUrl = `/listings/${id}/edit`;
const reportUrl = `/report-abuse?url=https://dev.to/listings/${category}/${slug}`;
return (
<div className="dropdown-menu" ref={this.componentRef}>
<MenuButton onClick={this.toggleMenu} />
<div className="dropdown">
<div
className={['dropdown-content', isOpen ? 'showing' : ''].join(' ')}
>
{isOwner ? (
<a href={editUrl} className="listing-edit-button">
Edit
</a>
) : (
<a href={reportUrl}>Report Abuse</a>
)}
</div>
</div>
</div>
);
}
}
export default DropdownMenu;