docbrown/app/javascript/modCenter/singleArticle/index.jsx
Arit Amana a78ccf2310
[deploy] [ModCenter] Fix "FlagUser" Modal functionality (#8995)
* Make FlagUserModal show up on click

* Explain extra div

* Make sure "Vomit All" reaction fires accurately

* Make test-file comment clearer

* Fix bug when 2 articles are open and the FlagUser modal does not open for the first article

* Still working it out ...

* Tests broken; need help

* Get rid of debugging alerts

* Fixed broken tests for <SingleArticle />

* Write tests for ModerationArticles component

* Complete tests for ModerationArticles component

* Employ safer Preact testing techniques

* Query all articles using "data-testid"

* Revert changes

* Got it working with portals for non-iframe implementation.

* wip tests

* Got mod tools flag user modal wprking on article page again.

* Added some documentation the code.

* Now confirm is disabled until you select an item in the flag user modal.

* Revert "wip tests"

This reverts commit fb7a0825039fd377cad04d9dedad9d1146b03978.

* test prep

* Fixed broken test.

* Refactored to use useRef hook.

* Rename a variable

* remove unnecessary comments

Co-authored-by: Nick Taylor <nick@dev.to>
2020-07-15 13:54:52 -04:00

97 lines
2.7 KiB
JavaScript

import PropTypes from 'prop-types';
import { h, Component } from 'preact';
import { createPortal } from 'preact/compat';
import { toggleFlagUserModal, FlagUserModal } from '../../packs/flagUserModal';
import { formatDate } from './util';
export default class SingleArticle extends Component {
activateToggle = (e) => {
e.preventDefault();
const { id, path, toggleArticle } = this.props;
toggleArticle(id, path);
};
render() {
const {
id,
title,
publishedAt,
cachedTagList,
user,
key,
articleOpened,
path,
} = this.props;
const tags = cachedTagList.split(', ').map((tag) => {
if (tag) {
return (
<span className="crayons-tag" key={key}>
<span className="crayons-tag__prefix">#</span>
{tag}
</span>
);
}
});
const newAuthorNotification = user.articles_count <= 3 ? '👋 ' : '';
const modContainer = id
? document.getElementById(`mod-iframe-${id}`)
: document.getElementById('mod-container');
// Check whether context is ModCenter or Friday-Night-Mode
if (modContainer) {
modContainer.addEventListener('load', () => {
modContainer.contentWindow.document
.getElementById('open-flag-user-modal')
.addEventListener('click', toggleFlagUserModal);
});
}
return (
<>
{modContainer &&
createPortal(
<FlagUserModal moderationUrl={path} authorId={user.id} />,
document.querySelector('.flag-user-modal-container'),
)}
<button
data-testid={`mod-article-${id}`}
type="button"
className="moderation-single-article"
onClick={this.activateToggle}
>
<span className="article-title">
<header>
<h3 className="fs-base fw-bold lh-tight">{title}</h3>
</header>
{tags}
</span>
<span className="article-author fs-s lw-medium lh-tight">
{newAuthorNotification}
{user.name}
</span>
<span className="article-published-at fs-s fw-bold lh-tight">
<time dateTime={publishedAt}>{formatDate(publishedAt)}</time>
</span>
<div
className={`article-iframes-container ${
articleOpened ? 'opened' : ''
}`}
id={`article-iframe-${id}`}
/>
</button>
</>
);
}
}
SingleArticle.propTypes = {
id: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
path: PropTypes.string.isRequired,
publishedAt: PropTypes.string.isRequired,
cachedTagList: PropTypes.isRequired,
user: PropTypes.isRequired,
key: PropTypes.number.isRequired,
};