import React from 'react'; import {IArticle, IRelatedArticle} from 'superdesk-api'; import ng from 'core/services/ng'; const TYPES_TO_ICONS = { picture: 'icon-photo', graphic: 'icon-graphic', video: 'icon-video', audio: 'icon-audio', text: 'icon-text', }; interface IProps { item: IArticle; } interface IState { types: Array; } /** * Render icon with count for each item type present in associations. */ export class AssociatedItemsList extends React.Component { readonly state = {types: []}; componentDidMount() { this.getAssociations(); } getAssociations() { const content = ng.get('content'); const associations = this.props.item.associations || {}; const related = Object.values(associations) .filter((_related) => _related != null); const relatedTypes = related .map((_related: IRelatedArticle) => _related.type) .filter((type) => type != null); if (relatedTypes.length === related.length) { // types for all this.setState({types: relatedTypes}); return; } content.fetchAssociations(this.props.item).then((_associations: Array) => { this.setState({ types: Object.values(_associations).filter((assoc) => assoc != null).map((assoc) => assoc.type), }); }); } render() { const icons = Object.keys(TYPES_TO_ICONS).map((type) => ({ type: type, icon: TYPES_TO_ICONS[type], count: this.state.types.filter((_type) => _type === type).length, })) .filter((data) => data.count > 0) .map((associatedType) => ( {associatedType.count} )); return icons.length ? ( {/* push it right */} {icons} ) : null; } }