import { Component , Input , OnInit } from '@angular/core' ; import { throwError } from 'rxjs' ; import { catchError , finalize } from 'rxjs/operators' ; import { errorToMessage } from '../../../shared/util/api-error' ; import { ExportFormat , EXPORT_FORMATS } from '../../models/export' ; import { Locale } from '../../models/locale' ; import { Project } from '../../models/project' ; import { ExportService } from '../../services/export.service' ; @ Component ( { selector : 'app-export-locale' , templateUrl : './export-locale.component.html' , styleUrls : [ './export-locale.component.css' ] , } ) export class ExportLocaleComponent implements OnInit { @ Input ( ) project : Project ; @ Input ( ) locales : Locale [ ] ; @ Input ( ) loading = false ; selectedLocale : Locale ; selectedFormat : ExportFormat ; availableFormats = EXPORT_FORMATS ; errorMessage : string ; constructor ( private exportService ) { } ngOnInit ( ) { } validInputs ( ) { return ! ! this . selectedFormat && ! ! this . selectedLocale ; } selectLocale ( locale ) { this . selectedLocale = locale ; } async export ( ) { if ( ! this . validInputs ( ) ) { return ; } this . errorMessage = undefined ; this . loading = true ; await this . exportService . exportAndDownload ( this . project . id , this . selectedLocale . code , this . selectedFormat ) . pipe ( catchError ( error => { console . error ( error ) ; this . errorMessage = errorToMessage ( error , 'ExportLocale' ) ; return throwError ( error ) ; } ) , finalize ( ( ) => ( this . loading = false ) ) ) . toPromise ( ) ; } }