import { Quote, Tariff } from '../common/entities/Quote'; import { ServiceError } from '../common/entities/ServiceError'; import { AnalyticsService } from '../common/services/AnalyticsService'; import { Title, Mode } from '../common/entities/Enums'; import { QuoteService } from '../common/services/QuoteService'; import { AcquisitionService, AcquisitionParameters } from '../common/services/AcquisitionService'; import { UserDataState } from '../common/state/UserDataState'; import { AgentState } from '../common/state/AgentState'; import { LayoutState } from '../common/state/LayoutState'; import { criticalCodes, CriticalCodes } from '../common/constants/CriticalCodes'; import { serviceOptIns, ServiceOptIns } from '../common/constants/ServiceOptIns'; import { titles } from '../common/constants/Titles'; import { HostExtractor } from '../common/util/HostExtractor'; import { ErrorScroll } from '../common/util/ErrorScroll'; import { ModalService } from '../common/services/ModalService'; import { AppConfigType } from '../common/entities/AppConfig'; import { EligibilityService } from '../common/services/EligibilityService'; import { PropertyQuestionsService } from "../common/services/PropertyQuestionsService"; import * as _ from 'lodash'; import { LocalDate, nativeJs, convert } from "js-joda"; export class SwitchController implements ng.IController { isAgentMode: boolean; // The first one of these is for handling the initial page load, the second is for the animation on the submit button loadingPage: boolean; loadingButton: boolean; tariffName: string; moveInDate: Date; quote: Quote; tariff: Tariff; step: 'PersonalDetails' | 'BankDetails' | 'MeterDetails'; readonly titles: Title[]; readonly criticalCodes: CriticalCodes; readonly serviceOptIns: ServiceOptIns; // Should we show the information about losing your smart meter functionality? showSmartMeterWarning: boolean; readonly mode: Mode; showMeterReadingForm: boolean; personalDetailsForm: angular.IFormController; bankDetailsForm: angular.IFormController; meterDetailsForm: angular.IFormController; // Most of the fields in the form map directly to the userDataState, but these are the fields we don't want to cache in local storage: data: AcquisitionParameters; password: string; // Error from the acquisition call error: ServiceError; constructor(private quoteService: QuoteService, private acquisitionService: AcquisitionService, public userDataState: UserDataState, private agentState: AgentState, layoutState: LayoutState, private $state: angular.ui.IStateService, public host: HostExtractor, private errorScroll: ErrorScroll, private modal: ModalService, analyticsService: AnalyticsService, private appConfig: AppConfigType, private eligibilityService: EligibilityService, private propertyQuestionsService: PropertyQuestionsService) { layoutState.title = 'Switch'; this.loadingPage = true; this.isAgentMode = host.isAgentMode; this.showSmartMeterWarning = host.mode === 'Switch'; this.mode = host.mode; this.step = 'PersonalDetails'; this.titles = titles; this.criticalCodes = criticalCodes; this.serviceOptIns = serviceOptIns; if (this.userDataState.moveInDate) this.moveInDate = convert(this.userDataState.moveInDate).toDate(); this.data = { payInArrears: false, ...(agentState.username ? { agentId: agentState.username } : {}), }; analyticsService.push({ event: 'ViewSwitchForm' }); } $onInit() { return this.quoteService.getQuote().then(this.quoteHandler, error => this.$state.go("^.get-quote")); } private quoteHandler = (quote: Quote) => { this.quote = quote; this.tariff = quote.tariffs[this.userDataState.tariffId]; const bundle = this.quote.bundles.find(bundle => bundle.name === this.userDataState.bundleName); if (!this.tariff || (this.userDataState.bundleName && !bundle)) { this.$state.go('^.quote'); } else { if (this.userDataState.bundleName) { this.tariffName = bundle.displayName; } else { this.tariffName = this.tariff.name; } this.loadingPage = false; } this.showMeterReadingForm = this.userDataState.quoteType === 'MoveIn'; }; submitPersonalDetails() { if (this.personalDetailsForm.$valid) { this.step = 'BankDetails'; } else this.errorScroll.scrollToError(this.personalDetailsForm); } submitBankDetails() { if (this.bankDetailsForm.$valid) { if (this.showMeterReadingForm) this.step = 'MeterDetails'; else return this.submit(); } else this.errorScroll.scrollToError(this.bankDetailsForm); } submitMeterDetails() { if (this.meterDetailsForm.$valid) { return this.submit(); } else this.errorScroll.scrollToError(this.meterDetailsForm); } private submit() { this.loadingButton = true; return this.acquisitionService.acquisition(this.quote, this.data) .then(() => this.$state.go('^.success'), this.errorHandler) .then(() => this.loadingButton = false); } private errorHandler = (error: ng.IHttpPromiseCallbackArg) => { this.error = error.data; // Navigate back to the previous step(s) if there is a service error from these if (this.serviceErrorOnForm(this.personalDetailsForm)) this.step = 'PersonalDetails'; else if (this.serviceErrorOnForm(this.bankDetailsForm)) this.step = 'BankDetails'; } private serviceErrorOnForm(form: angular.IFormController): boolean { return this.error.errorList.some(value => form[value.id]); } saveMoveInDate() { this.userDataState.moveInDate = LocalDate.from(nativeJs(this.moveInDate)); } openFoundationModal() { this.modal.open({ content: require('./FoundationModal.html') }); } }