docbrown/app/javascript/articles/components/ReactionsCount.jsx
Joshua Wehner 8a45a4bf67
Enable multiple reactions on #index (including front-end render) (#19169)
* Revert "(Temporarily) Remove multiple reactions from #index (#19142)"

This reverts commit a45d300639.

* Clean up reaction category count mechanism for #index

* Remove, unnecessary

* Expose reaction categories in JSON response

* Update test with new categories

* Fix flaky sample / minimum floor

* buildArticle (infinite scroll) with multiple reactions

* If we're doing #index front-end, we aren't feature-flag'd

* nbsp

* react (home feed) has multiple_reactions

* Update app/assets/javascripts/utilities/buildArticleHTML.js

Co-authored-by: Rajat Talesra <rajat@forem.com>

* Clean up Reactable#reaction_categories

* Use 'multiple_reactions_icons_container'

* Try adding a ReactionCount test

* Adapt memory fix from 76dd53d

* Try adding categories to fixture

* Attempt to eager-load distinct public categories

* Setting dependent to the default for rubocop

* Try making image assets more public?

* Revert "Fix flaky sample / minimum floor"

---------

Co-authored-by: Rajat Talesra <rajat@forem.com>
Co-authored-by: Mac Siri <mac@forem.com>
2023-03-03 15:17:19 +01:00

70 lines
1.7 KiB
JavaScript

import { h } from 'preact';
import { articlePropTypes } from '../../common-prop-types';
import { locale } from '../../utilities/locale';
export const ReactionsCount = ({ article }) => {
const totalReactions = article.public_reactions_count || 0;
if (totalReactions === 0) {
return;
}
function buildIcons() {
const reversable = article.public_reaction_categories;
if (reversable === undefined) {
return;
}
reversable.reverse();
const icons = reversable.map((category) => {
const path = `/assets/${category.icon}.svg`;
const alt = category.name;
return (
<span className="crayons_icon_container" key={category.slug}>
<img src={path} width="18" height="18" alt={alt} />
</span>
);
});
return (
<span className="multiple_reactions_icons_container" dir="rtl">
{icons}
</span>
);
}
function buildCounter() {
const reactionText = `${
totalReactions == 1
? locale('core.reaction')
: `${locale('core.reaction')}s`
}`;
return (
<span className="aggregate_reactions_counter">
<span className="hidden s:inline" title="Number of reactions">
{totalReactions}&nbsp;{reactionText}
</span>
</span>
);
}
return (
<a
href="{article.path}"
className="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"
data-reaction-count
data-reactable-id="{article.id}"
>
<div className="multiple_reactions_aggregate">
{buildIcons()}
{buildCounter()}
</div>
</a>
);
};
ReactionsCount.propTypes = {
article: articlePropTypes.isRequired,
};
ReactionsCount.displayName = 'ReactionsCount';