import { HttpClient , HttpParams } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { FeltReportResponse } from './felt-report-response' ; import { FeltReportReponseError } from './felt-report-reponse-error' ; import { FeltReportResponseErrorDetails } from './felt-report-response-error-details' ; import { FeltReport } from 'app/tell-us/felt-report' ; import { environment } from 'environments/environment' ; import { GeoService } from '@shared/geo.service' ; const DYFI_FORM_VERSION = '1.12' ; @ Injectable ( ) export class FormSubmitService { formResponse$ = new BehaviorSubject < FeltReportResponse | FeltReportReponseError > ( null ) ; responseUrl = environment . DYFI_RESPONSE_URL ; constructor ( public geoService , public httpClient ) { } param code param message createErrorResponse ( code , message ) { if ( ! code && ! message ) { return ; } const errorDetail = { code : code , message : message } ; const error = { error : errorDetail } ; this . formResponse$ . next ( error ) ; } param feltReport onSubmit ( feltReport ) { let params = new HttpParams ( ) ; const validated = feltReport . valid ; if ( validated ) { for ( const key in feltReport ) { if ( feltReport . hasOwnProperty ( key ) && key !== 'location' ) { params = params . append ( key , feltReport [ key ] ) ; } } params = this . parseLocation ( params , feltReport ) ; params = params . append ( 'format' , 'json' ) ; params = params . append ( 'form_version' , DYFI_FORM_VERSION ) ; this . httpClient . post ( this . responseUrl , params ) . pipe ( catchError ( val => { return of ( val ) ; } ) ) . subscribe ( response => { if ( response && ! response . error ) { this . formResponse$ . next ( response ) ; } else { const code = response . status ? response . status : null ; const message = response . statusText ? response . statusText : 'Server Error' ; this . createErrorResponse ( code , message ) ; } } ) ; } else { const code = 400 ; const message = 'Error, invalid form entry' ; this . createErrorResponse ( code , message ) ; } } param params param feltReport parseLocation ( params , feltReport ) { const latitude = feltReport . ciim_mapLat ; const longitude = feltReport . ciim_mapLon ; const confidence = feltReport . ciim_mapConfidence ; const address = feltReport . ciim_mapAddress ; const method = this . geoService . method$ . value ; if ( latitude || latitude === 0 ) { params = params . append ( 'ciim_mapLat' , latitude ) ; } if ( longitude || longitude === 0 ) { params = params . append ( 'ciim_mapLon' , longitude ) ; } if ( confidence || confidence === 0 ) { params = params . append ( 'ciim_mapConfidence' , confidence ) ; } if ( address && method === 'geocode' ) { params = params . append ( 'ciim_mapAddress' , address ) ; } else { params = params . append ( 'ciim_mapAddress' , '' ) ; } return params ; } /**
     * Destroys the response component so the user can fill out another
     * DYFI form in the same session, and loads the form in main tell-us component
     */ resetResponse ( ) { this . formResponse$ . next ( null ) ; } }