import { QuoteService } from '../../common/services/QuoteService'; import { UserDataState } from '../../common/state/UserDataState'; import { Quote, Tariff } from '../../common/entities/Quote'; import { HostExtractor } from '../../common/util/HostExtractor'; import { ServiceType, Utility, TariffId } from '../../common/entities/Enums'; import { TrustpilotClient } from '../../common/clients/TrustpilotClient'; import { TrustpilotSummary } from '../../common/entities/Trustpilot'; import { AnalyticsService } from '../../common/services/AnalyticsService'; import { ModalService } from '../../common/services/ModalService'; import tilModalOptions from '../../common/modals/TILModal'; import { isNil } from 'lodash'; export class PlanPaymController { pageLoading: boolean; quote: Quote; selectedTariff: Tariff; emailQuoteAvailable: boolean; emailFormCurrentStatus: EmailFormStatus; trustpilotContent: TrustpilotSummary; emailLoading: boolean; error: string; constructor( private $state: ng.ui.IStateService, private $location: ng.ILocationService, private $q: QService, private quoteService: QuoteService, private userDataState: UserDataState, private host: HostExtractor, private trustpilotClient: TrustpilotClient, private analyticsService: AnalyticsService, private modal: ModalService ) { } $onInit() { this.pageLoading = true; const quote = this.quoteService.getQuote(); // The catch block here is so that in the $q.all() we can set trustpilotContent to undefined // and still load the page (just without the trustpilot area). const trustpilotSummary = this.trustpilotClient.getSummary().catch(() => undefined); return this.$q.all({ trustpilotSummary, quote }) .then(response => { this.quote = response.quote; this.selectedTariff = this.quote.tariffs[this.userDataState.tariffId]; if (isNil(this.selectedTariff)) { return this.$state.go('^.quote'); } this.emailFormCurrentStatus = 'hidden'; this.analyticsService.push({ event: 'ViewPlan', tariff: this.userDataState.tariffId, cost: this.selectedTariff.expectedAnnualSpend }); this.emailQuoteAvailable = this.host.mode !== 'MoveIn'; this.trustpilotContent = response.trustpilotSummary; this.pageLoading = false; }, error => this.$state.go('^.get-quote')); } saveQuote() { if (!this.emailLoading) { this.emailLoading = true; return this.quoteService.saveQuote({ email: this.userDataState.emailAddress, quote: this.quote, tariffId: this.userDataState.tariffId }).then(() => { this.emailLoading = false; this.emailFormCurrentStatus = 'completed'; }).catch(() => { this.emailLoading = false; this.error = 'Sorry, we could not save your quote. Please try again later.'; }); } } openTilModal(utility: Utility) { this.modal.open(tilModalOptions({ tariff: this.selectedTariff, til: this.selectedTariff.tils[utility], serviceType: this.quote.serviceType, utility })); } showEmail() { this.emailFormCurrentStatus = 'visible'; } switch() { this.$state.go('^.switch'); } } type EmailFormStatus = 'hidden' | 'visible' | 'completed';