import React from 'react'; import {RelativeDate} from 'core/datetime/relativeDate'; import {state as State} from 'apps/search/components/fields/state'; import {connectServices} from 'core/helpers/ReactRenderAsync'; import {IArticle} from 'superdesk-api'; import {gettext} from 'core/utils'; import {AuthoringWorkspaceService} from '../authoring/services/AuthoringWorkspaceService'; interface IProps { item: IArticle; datetime: any; authoringWorkspace: AuthoringWorkspaceService; TranslationService: any; } interface IState { translations: Array; translationsLookup: Dictionary; } class TranslationsWidgetComponent extends React.Component { static propTypes: any; constructor(props) { super(props); this.state = { translations: null, translationsLookup: null, }; } componentDidMount() { const {item, TranslationService} = this.props; TranslationService.getTranslations(item) .then((response) => { const translations: Array = response._items; this.setState({ translations: translations, translationsLookup: translations.reduce((result, reference) => { result[reference._id] = reference; return result; }, {}), }); }); } render() { const {authoringWorkspace, datetime} = this.props; const {translations, translationsLookup} = this.state; if (translations == null) { return null; } const sortOldestFirst = (a: IArticle, b: IArticle) => new Date(b.firstcreated) > new Date(a.firstcreated) ? -1 : 1; const listClassNames = 'sd-list-item__column sd-list-item__column--grow sd-list-item__column--no-border'; return (
{ translations.sort(sortOldestFirst).map((translation: IArticle) => (
authoringWorkspace.popup(translation, 'edit')} className="sd-list-item sd-shadow--z1" >
{translation.language} {translation.headline}
{ translation.translated_from == null ? ( {gettext('Original')} ) : (
{gettext('Translated from')} {translationsLookup[translation.translated_from].language}
) }
)) }
); } } export const TranslationsWidget = connectServices( TranslationsWidgetComponent, ['TranslationService', 'authoringWorkspace', 'datetime'], );