import { ApplicationConfig } from '../common/entities/ApplicationConfig'; import { Quote } from '../common/entities/Quote'; import { regions, Regions } from '../common/constants/Regions'; import { ServiceError } from '../common/entities/ServiceError'; import { HostExtractor } from '../common/util/HostExtractor'; import { UserDataState } from '../common/state/UserDataState'; import { QuoteService } from '../common/services/QuoteService'; import { EligibilityService } from '../common/services/EligibilityService'; import { OptimizelyService } from '../common/services/OptimizelyService'; import { AnalyticsService } from '../common/services/AnalyticsService'; import { LayoutState } from '../common/state/LayoutState'; import { paymentMethods } from '../common/constants/PaymentMethods'; import { Address } from '../common/entities/Address'; export class GetQuoteController implements ng.IController { regionRequired: boolean; quoteForm: ng.IFormController; error: string; loading: boolean; readonly regions: Regions; constructor(protected APP_CONFIG: ApplicationConfig, protected host: HostExtractor, protected userDataState: UserDataState, protected quoteService: QuoteService, protected eligibilityServiceNew: EligibilityService, protected $state: ng.ui.IStateService, protected $anchorScroll: ng.IAnchorScrollService, protected layoutState: LayoutState, optimizelyService: OptimizelyService, protected analyticsService: AnalyticsService) { // Display the logo - there is no page title, and you can't go back delete layoutState.title; delete layoutState.back; this.regions = regions; // Arriving on this page implies the old quote is defunct quoteService.clear(); eligibilityServiceNew.clear(); userDataState.quoteType = 'Quick'; userDataState.economy7 = userDataState.economy7 || false; this.parseQueryParams($state.params); analyticsService.clear(); analyticsService.push({ event: 'ViewQuoteForm', // These are specifically if they are in the URL for this event - not where they already exist in local storage postcode: $state.params.postcode, paymentMethod: $state.params.paymentMethod }); } protected parseQueryParams(params: ng.ui.IStateParamsService) { // Update the user data state vals only if these params exist in the url - i.e. don't override with nulls this.userDataState.postcode = params.postcode || this.userDataState.postcode; this.userDataState.paymentMethod = paymentMethods.find(p => p === params.paymentMethod) || this.userDataState.paymentMethod || 'Paym'; } submit() { if (this.quoteForm.$invalid) { this.$anchorScroll('error'); return; } this.loading = true; if (this.host.retailer === 'OVO' && this.userDataState.paymentMethod === 'Payg') return this.eligibilityServiceNew.checkSupplyAddress().then(() => this.quoteService.getQuote()).then(this.quoteHandler, this.errorHandler); else return this.quoteService.getQuote().then(quote => this.quoteHandler(quote), this.errorHandler); } quoteHandler = (quote: Quote) => { if (this.userDataState.paymentMethod === 'Payg') { this.$state.go('^.plan-payg'); } else { this.$state.go('^.quote'); } }; errorHandler = (error: ng.IHttpPromiseCallbackArg) => { switch (error.status) { case 428: this.error = 'Sorry, we could not find a region for your postcode'; this.regionRequired = true; break; case 422: this.error = 'Sorry, we are unable to supply your property'; break; case 408: case 503: this.error = 'Sorry, we are having some trouble with your request. Please try again in a few minutes'; break; default: this.error = "Whoops, something went wrong"; } this.loading = false; this.$anchorScroll('error'); }; updateAddress(address: Address) { this.userDataState.supplyAddress = address; this.userDataState.postcode = address ? address.postcode : undefined; } }