import { Component, Injector, OnInit, ViewChild } from '@angular/core'; import { Router } from '@angular/router'; import { AppConsts } from '@shared/AppConsts'; import { accountModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { AccountServiceProxy, CreateClinicAccountInput, CustomerServiceProxy, PasswordComplexitySetting, ProfileServiceProxy, RegisterOutput } from '@shared/service-proxies/service-proxies'; import { LoginService } from '../login/login.service'; import { RegisterModel } from './register.model'; import { finalize, catchError } from 'rxjs/operators'; import { RecaptchaComponent } from 'ng-recaptcha'; import { ILatLng } from '@app/sprintship/fleet-management/driver-proximities/driver-proximities.directive'; import { GooglePlaceDirective } from 'ngx-google-places-autocomplete'; import { Address } from 'ngx-google-places-autocomplete/objects/address'; @Component({ templateUrl: './register.component.html', animations: [accountModuleAnimation()] }) export class RegisterComponent extends AppComponentBase implements OnInit { @ViewChild('recaptchaRef', {static: false}) recaptchaRef: RecaptchaComponent; @ViewChild('placesRef', { static: true }) placesRef: GooglePlaceDirective; model: RegisterModel = new RegisterModel(); passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting(); recaptchaSiteKey: string = AppConsts.recaptchaSiteKey; terms: boolean = false; isEnabledLaboratory: boolean = false; saving = false; clinicInput = new CreateClinicAccountInput(); completeAddress: string = ""; format_address = "" addressLine1 = ""; addressLine2: string; street_number = ""; route = ""; neighborhood = ""; sublocality = ""; place_id_ = "" placeExist = true; origin: ILatLng = { latitude: 38.889931, longitude: -77.009003 }; postal_code: any country: any state: any city: any place_id: any longitude: any latitude: any locationId: number addressTypeName: string = ""; formattedAddress: string = ""; addressId: number; contactId: number; addressTypeId: any; filteredAddressType: any; constructor( injector: Injector, private _accountService: AccountServiceProxy, private _router: Router, private readonly _loginService: LoginService, private _profileService: ProfileServiceProxy, private _customerProfileService: CustomerServiceProxy ) { super(injector); } ngOnInit() { //Prevent to register new users in the host context if (this.appSession.tenant == null) { this._router.navigate(['account/login']); return; } this._profileService.getPasswordComplexitySetting().subscribe(result => { this.passwordComplexitySetting = result.setting; }); this.isEnabledLaboratory = abp.features.isEnabled('App.ControllerRouteManagementFeature.RouteDetailsManagementFeature'); } get useCaptcha(): boolean { return this.setting.getBoolean('App.UserManagement.UseCaptchaOnRegistration'); } save(): void { if (this.useCaptcha && !this.model.captchaResponse) { this.message.warn(this.l('CaptchaCanNotBeEmpty')); return; } this.saving = true; if (this.isEnabledLaboratory) { this.clinicInput.userInput = this.model; this.clinicInput.addressLine1 = this.addressLine1; this.clinicInput.addressLine2 = this.addressLine2; this.clinicInput.city = this.city; this.clinicInput.state = this.state; this.clinicInput.postalCodeValue = this.postal_code; this.clinicInput.country = this.country; this.clinicInput.addressTypeName = this.addressTypeName; this.clinicInput.formattedAddress = this.completeAddress; this.clinicInput.latitude = this.latitude; this.clinicInput.longitude = this.longitude; this._customerProfileService.createClinicAccount(this.clinicInput) .pipe(finalize(() => { this.saving = false; })) .pipe(catchError((err, caught): any => { this.recaptchaRef.reset(); })).subscribe((result: RegisterOutput) => { if (!result.canLogin) { this.notify.success(this.l('SuccessfullyRegistered')); this._router.navigate(['account/login']); return; } //Autheticate this.saving = true; this._loginService.authenticateModel.userNameOrEmailAddress = this.model.userName; this._loginService.authenticateModel.password = this.model.password; this._loginService.authenticate(() => { this.saving = false; }); }); } else { this._accountService.register(this.model) .pipe(finalize(() => { this.saving = false; })) .pipe(catchError((err, caught): any => { this.recaptchaRef.reset(); })) .subscribe((result: RegisterOutput) => { if (!result.canLogin) { this.notify.success(this.l('SuccessfullyRegistered')); this._router.navigate(['account/login']); return; } //Autheticate this.saving = true; this._loginService.authenticateModel.userNameOrEmailAddress = this.model.userName; this._loginService.authenticateModel.password = this.model.password; this._loginService.authenticate(() => { this.saving = false; }); }); } } captchaResolved(captchaResponse: string): void { this.model.captchaResponse = captchaResponse; } public pickUpAddressChange(address: Address) { this.placeExist = true; $('#nextStep').removeAttr("disabled"); this.format_address = address.formatted_address; this.addressLine1 = ""; this.street_number = ""; this.route = ""; this.neighborhood = ""; this.sublocality = ""; this.place_id = address.place_id; let x: ILatLng = { longitude: address.geometry.location.lng(), latitude: address.geometry.location.lat(), } this.origin = x; for (var data in address.address_components) { for (var data2 in address.address_components[data].types) { if (address.address_components[data].types[data2] == "postal_code") { this.postal_code = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "country") { this.country = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "administrative_area_level_1") { this.state = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "locality" || address.address_components[data].types[data2] == "postal_town" || address.address_components[data].types[data2] == "administrative_area_level_3") { this.city = address.address_components[data].short_name; } if (address.address_components[data].types[data2] == "route") { this.route = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "street_number") { this.street_number = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "neighborhood") { this.neighborhood = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "sublocality") { this.sublocality = address.address_components[data].long_name; } } } this.street_number = this.street_number == undefined || this.route == "" || this.route == " " ? "" : this.street_number + " "; this.route = this.route == undefined || this.route == "" || this.route == " " ? "" : "" + this.route + ", "; this.neighborhood = this.neighborhood == undefined || this.neighborhood == "" || this.neighborhood == " " ? "" : "" + this.neighborhood + ", "; this.sublocality = this.sublocality == undefined || this.sublocality == "" || this.sublocality == " " ? "" : "" + this.sublocality + ", "; this.addressLine1 = this.street_number + this.route + this.sublocality + this.neighborhood; this.longitude = address.geometry.location.lng(); this.latitude = address.geometry.location.lat(); this.addressTypeName = address.types[0] == null ? "" : address.types[0]; } onSubmitPickup(): void { try { if (this.placesRef.place.address_components == undefined || this.placesRef.place.address_components == null) { this.placeExist = false; } else { this.placeExist = true; } } catch (e) { this.placeExist = false; } if (!this.placeExist) { $('#nextStep').attr("disabled", ""); } } }