import { ServiceError } from '../common/entities/ServiceError'; import { UserDataState } from '../common/state/UserDataState'; import { QuoteService } from '../common/services/QuoteService'; import { QuoteType, Utility, UsagePeriod, UsageUnit, Insulation, EnergyUsage } from '../common/entities/Enums'; import { Quote } from '../common/entities/Quote'; import { Suppliers, SupplierTariff, SupplierDetails } from '../common/entities/Suppliers'; import { UsageDetails } from './fuel-usage-panel/FuelUsagePanel'; export class RefineQuoteController implements ng.IController { suppliers: Suppliers; error: ServiceError; // 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; usageBasedForm: ng.IFormController; estimatedForm: ng.IFormController; // Not binding the form directly to the local storage; we are updating that only on submit quoteType: QuoteType; economy7: boolean; gasUsageDetails: UsageDetails; electricityUsageDetails: UsageDetails; estimatedDetails: EstimatedDetails; constructor(private userDataState: UserDataState, private quoteService: QuoteService, private $state: ng.ui.IStateService, private mixpanel: Mixpanel) { this.loadingPage = true; // Only select second tab if that's the existing quote type - i.e. go to tab 1 if we have come from a quick quote this.quoteType = userDataState.quoteType === 'Estimated' ? 'Estimated' : 'UsageBased'; // Initialise values from local storage if they exist, and defaults if not this.gasUsageDetails = { period: userDataState.gasPeriod || 'Monthly', supplier: userDataState.previousGasSupplier, tariff: userDataState.previousGasTariff, unit: userDataState.gasUnit || 'KWh', usage: userDataState.gasUsage, }; this.electricityUsageDetails = { period: userDataState.electricityPeriod || 'Monthly', supplier: userDataState.previousElectricitySupplier, tariff: userDataState.previousElectricityTariff, unit: userDataState.electricityUnit || 'KWh', usage: userDataState.electricityUsage, }; this.estimatedDetails = { numberOfRooms: userDataState.numberOfRooms || 2, numberOfOccupants: userDataState.numberOfOccupants || 2, insulation: userDataState.insulation || 'Average', energyUsage: userDataState.energyUsage || 'EveningsAndWeekends' }; this.economy7 = userDataState.economy7 || false; } // This works with our version of angular because ui-router polyfills this function $onInit() { // Need to have at least a quick quote for this page, otherwise we won't have the region info... return this.quoteService.getQuote() .then(quote => this.quoteService.suppliers(quote.region)) .then(result => { this.suppliers = result.data; this.loadingPage = false; }) .catch(() => this.$state.go('^.get-quote')); } getTariffs(supplierId: string, utility: Utility): Promise { return this.quoteService.getQuote() .then(quote => this.quoteService.supplierTariffs({ region: quote.region, supplierId, utility })) .then(result => result.data); } submitUsageBased() { if (this.usageBasedForm.$invalid) return; this.userDataState.quoteType = 'UsageBased'; this.userDataState.gasPeriod = this.gasUsageDetails.period; this.userDataState.previousGasSupplier = this.gasUsageDetails.supplier; this.userDataState.previousGasTariff = this.gasUsageDetails.tariff; this.userDataState.gasUnit = this.gasUsageDetails.unit; this.userDataState.gasUsage = this.gasUsageDetails.usage; this.userDataState.electricityPeriod = this.electricityUsageDetails.period; this.userDataState.previousElectricitySupplier = this.electricityUsageDetails.supplier; this.userDataState.previousElectricityTariff = this.electricityUsageDetails.tariff; this.userDataState.electricityUnit = this.electricityUsageDetails.unit; this.userDataState.electricityUsage = this.electricityUsageDetails.usage; this.userDataState.economy7 = this.economy7; this.loadingButton = true; return this.quoteService.getNewQuote().then(this.quoteHandler, this.errorHandler); } submitEstimated() { if (this.estimatedForm.$invalid) return; this.userDataState.quoteType = 'Estimated'; this.userDataState.numberOfRooms = this.estimatedDetails.numberOfRooms; this.userDataState.numberOfOccupants = this.estimatedDetails.numberOfOccupants; this.userDataState.insulation = this.estimatedDetails.insulation; this.userDataState.energyUsage = this.estimatedDetails.energyUsage; this.userDataState.economy7 = this.economy7; this.loadingButton = true; return this.quoteService.getNewQuote().then(this.quoteHandler, this.errorHandler); } private quoteHandler = () => { this.mixpanel.track('QS - RefineQuote', { postcode: this.userDataState.postcode, fuel: this.userDataState.fuel, quoteType: this.userDataState.quoteType, economy7: this.userDataState.economy7 }); this.mixpanel.people.increment('Quotes'); this.$state.go('^.quote'); } private errorHandler = (error: ng.IHttpPromiseCallbackArg) => { switch (error.status) { case 400: this.error = error.data; break; case 404: case 422: this.error = { code: 'Sorry, we are unable to supply your property' }; break; case 409: this.error = { code: 'The supplier/tariff combination you have entered is not valid. Please check and re-submit. If you are unsure, please call us for help.' }; break; case 408: case 503: this.error = { code: 'Sorry, we are having some trouble with your request. Please try again in a few minutes' }; break; default: this.error = { code: "Whoops, something went wrong" }; } this.loadingButton = false; } } interface EstimatedDetails { numberOfRooms: number; numberOfOccupants: number; insulation: Insulation; energyUsage: EnergyUsage; }