import { Quote, TariffWithId } from '../common/entities/Quote'; import { TariffId, Utility } from '../common/entities/Enums'; import { QuoteService } from '../common/services/QuoteService'; import { ModalService } from '../common/services/ModalService'; import { UserDataState } from '../common/state/UserDataState'; import tilModalOptions from '../common/modals/TILModal'; import * as _ from 'lodash'; const ONLINE_DISCOUNT_AMOUNT = 30; export class QuoteController { quote: Quote; onlineDiscountAmount: number; loading: boolean; showSavings: boolean; tariffs: TariffWithId[]; selectedTariffMobile: number; constructor(private $state: angular.ui.IStateService, quoteService: QuoteService, private $scope: ng.IScope, private modal: ModalService, private userDataState: UserDataState) { this.loading = true; this.selectedTariffMobile = 0; quoteService.getQuote().then( quote => { this.quote = quote; this.showSavings = Object.keys(quote.tariffs).map(tariffId => quote.tariffs[tariffId]).every(tariff => tariff.expectedAnnualSavings > 0); // Need to convert object into an array so we can guarantee the ordering of tariffs this.tariffs = _.sortBy((Object.keys(quote.tariffs) as TariffId[]).map(id => ({ id, ...(quote.tariffs[id]) })), t => t.expectedAnnualSpend); // TODO there has to be a better way of getting this info than storing the discount amount in the FE this.onlineDiscountAmount = ONLINE_DISCOUNT_AMOUNT * (quote.fuel === 'Dual' ? 2 : 1); this.loading = false; }, error => $state.go('^.get-quote') ) } scrollToTariff(index: number) { this.selectedTariffMobile = index; // Irritating that we need to explicitly call the digest cycle but this is because of how the scroll functionality is implemented this.$scope.$digest(); } // This controller is only used for PAYM selectTariff(tariffId: TariffId) { this.userDataState.tariffId = tariffId; this.$state.go('^.plan'); } openTilModal(tariffId: TariffId, utility: Utility) { this.modal.open(tilModalOptions({ tariff: this.quote.tariffs[tariffId], til: this.quote.tariffs[tariffId].tils[utility], serviceType: this.quote.serviceType, utility: utility })); } }