import { Dialog, Button, Gridicon } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import { isEmpty } from 'lodash'; import { Component } from 'react'; import FormFieldset from 'calypso/components/forms/form-fieldset'; import TranslatableTextarea from './translatable-textarea'; import TranslatedSuccess from './translated-success'; import { getSingleTranslationData, getTranslationPermaLink, submitTranslation } from './utils.js'; /** * Module varialbles */ const TRANSLATION_FETCH_ERROR = 'translation-fetch-error'; const TRANSLATION_SUBMIT_ERROR = 'tranlsation-submit-error'; export class Translatable extends Component { state = { showDialog: false, originalData: {}, formState: {}, }; hasDataLoaded() { return ! isEmpty( this.state.originalData ) || ! isEmpty( this.state.error ); } handleTranslationChange = ( event ) => { const { name, value } = event.target; this.setState( { formState: { ...this.state.formState, [ name ]: value, }, submitting: false, submissionSuccess: false, } ); }; closeDialog = () => { this.setState( { showDialog: false, submissionSuccess: false, formState: this.state.submissionSuccess ? {} : this.state.formState, originalData: this.state.submissionSuccess ? {} : this.state.originalData, } ); }; openDialog = ( event ) => { event.preventDefault(); this.setState( { showDialog: true } ); const { singular, context, plural, locale } = this.props; if ( ! this.hasDataLoaded() ) { getSingleTranslationData( locale, { singular, context, plural } ) .then( ( originalData ) => this.setState( { originalData, translationUrl: getTranslationPermaLink( originalData.originalId, locale ), formState: { translatedSingular: originalData.translatedSingular, translatedPlural: originalData.translatedPlural, }, } ) ) .catch( () => { this.setState( { error: TRANSLATION_FETCH_ERROR, } ); } ); } }; submitForm = () => { this.setState( { submitting: true, } ); submitTranslation( this.state.originalData.originalId, this.state.formState, this.props.locale ) .then( ( originalData ) => { this.setState( { error: originalData.error, originalData, submitting: false, submissionSuccess: true, } ); } ) .catch( () => { this.setState( { error: TRANSLATION_SUBMIT_ERROR, } ); } ); }; getDialogButtons = () => { const { translate } = this.props; const buttons = [ , ]; if ( ! this.state.submissionSuccess ) { buttons.push( ); } return buttons; }; renderPlaceholder() { return (
); } getErrorMessage( errorType ) { const { translate } = this.props; switch ( errorType ) { case TRANSLATION_FETCH_ERROR: return translate( "Sorry, we couldn't find the translation for this string." ); case TRANSLATION_SUBMIT_ERROR: return translate( "Sorry, we couldn't submit the translation for this string." ); default: return translate( "Sorry, we've encountered an error." ); } } renderTranslatableContent() { const { error, submissionSuccess, originalData, submitting, formState } = this.state; if ( error ) { return (

{ this.getErrorMessage( this.state.error ) }

); } if ( submissionSuccess ) { return ; } return [ originalData.comment &&

{ originalData.comment }

, , this.state.formState.translatedPlural && ( ), ]; } renderDialogContent() { const { translate } = this.props; return (

{ translate( 'Translate to %(localeName)s', { args: { localeName: this.props.locale.name }, } ) }

{ this.hasDataLoaded() ? this.renderTranslatableContent() : this.renderPlaceholder() }
); } render() { const { untranslated, children } = this.props; const classes = clsx( 'translatable community-translator__element', { 'is-untranslated': untranslated, } ); return ( { children } { this.state.showDialog && ( { this.renderDialogContent() } ) } ); } } export default localize( Translatable );