/* eslint-disable react/no-multi-comp */ import React from 'react'; import {IUser, IVocabulary, IVocabularyItem, IArticle} from 'superdesk-api'; import {IPropsItemListInfo} from '../ListItemInfo'; import {SuperdeskReactComponent} from 'core/SuperdeskReactComponent'; import {getVocabularyItemNameTranslated, gettext} from 'core/utils'; import {Popover} from 'superdesk-ui-framework/react'; import {IRelatedEntitiesToFetch} from '.'; const SEPARATOR = /; const AUTHORS_TO_SHOW_AT_ONCE: number = 2; export class Authors extends SuperdeskReactComponent { public static getRelatedEntities(item: IArticle): IRelatedEntitiesToFetch { if (item.authors == null) { return []; } else { const userIds = item.authors .filter(({_id}) => _id != null) // _id is not present in ingested items .map((author) => ({collection: 'users', id: author._id[0]})); return [ ...userIds, {collection: 'vocabularies', id: 'author_roles'}, ]; } } private related: { getUser: (id: string) => IUser; getAuthorRole: (id: string) => IVocabularyItem; }; constructor(props: IPropsItemListInfo) { super(props); this.related = { getUser: (id) => this.props.relatedEntities['users'].get(id), getAuthorRole: (qcode: string) => { const authorRoles: IVocabulary = this.props.relatedEntities['vocabularies'].get('author_roles'); return authorRoles.items.find((role) => role.qcode === qcode); }, }; } render() { const {options} = this.props; if (this.props.item.authors == null || options == null) { return null; } const authors = this.props.item.authors .filter(({_id}) => _id != null) // _id is not present in ingested items .map(({_id}) => ({userId: _id[0], roleId: _id[1]})) .filter(({roleId}) => options.includeRoles.includes(roleId)); if (authors.length < 1) { return null; } const renderAuthorRole = (roleQcode: string) => { return ( {getVocabularyItemNameTranslated(this.related.getAuthorRole(roleQcode))}:{' '} ); }; const renderUser = (userId: string) => { const user = this.related.getUser(userId); return ( {user[options.displayField] ?? user.display_name} ); }; return ( { authors.slice(0, AUTHORS_TO_SHOW_AT_ONCE).map(({userId, roleId}, index) => ( {index > 0 && SEPARATOR} {renderAuthorRole(roleId)} {renderUser(userId)}{' '} )) } {/** rendering outside of .container in order to prevent ellipsis hiding "All authors" button */} { authors.length > AUTHORS_TO_SHOW_AT_ONCE && ( {SEPARATOR} { authors.map(({userId, roleId}) => ( {renderAuthorRole(roleId)} {renderUser(userId)} )) } ) } ); } }