384 lines
8.0 KiB
TypeScript
384 lines
8.0 KiB
TypeScript
import {
|
|
XTask,
|
|
isFunction,
|
|
XTaskPriority,
|
|
nameofInstance,
|
|
XOneOrManyType,
|
|
isNullOrUndefined,
|
|
isNullOrEmptyString,
|
|
} from 'x-framework-core';
|
|
import {
|
|
RegistrationKeys,
|
|
RegistrationTaks,
|
|
VRegistrationSteps,
|
|
isContainsRegistrationTask,
|
|
} from './v-register.typings';
|
|
import {
|
|
XActionRequestDto,
|
|
XActionResponseDto,
|
|
} from 'x-framework-identity-sdk';
|
|
import { Observable } from 'rxjs';
|
|
import { Component, Input } from '@angular/core';
|
|
import { getRegistrationStepResourceID } from './v-register.tools';
|
|
import { VBaseInCardComponent } from '../../classes/v-base.component';
|
|
|
|
@Component({
|
|
selector: 'v-register',
|
|
templateUrl: './v-register.component.html',
|
|
styleUrls: ['./v-register.component.scss'],
|
|
})
|
|
export class VRegisterComponent extends VBaseInCardComponent {
|
|
//
|
|
//#region Props ...
|
|
readonly RegistrationSteps = Object.assign({}, VRegistrationSteps);
|
|
readonly RegistrationTaskNames = Object.assign({}, RegistrationTaks);
|
|
|
|
/**
|
|
* Custom CSS Classes ...
|
|
*/
|
|
cssClass: XOneOrManyType<string> = [];
|
|
|
|
/**
|
|
* Remove Card Marigin ...
|
|
*/
|
|
removeCardMargin: boolean = false;
|
|
|
|
//
|
|
@Input()
|
|
getTermsAndConditionsTask: (lang: string) => Observable<string> = undefined;
|
|
|
|
//
|
|
step: VRegistrationSteps;
|
|
actionRequest: XActionRequestDto;
|
|
actionResponse: XActionResponseDto;
|
|
//#endregion
|
|
|
|
//
|
|
//#region LifeCycles ...
|
|
afterViewInit(): void {
|
|
super.afterViewInit();
|
|
|
|
//
|
|
this.loadStepInfo();
|
|
}
|
|
|
|
async onChange(changeKeys: string[]) {
|
|
super.onChange(changeKeys);
|
|
|
|
//
|
|
let isChanged = false;
|
|
let applyChanged = false;
|
|
|
|
//
|
|
// getTermsAndConditionsTask ...
|
|
isChanged = changeKeys.includes(
|
|
nameofInstance(this, 'getTermsAndConditionsTask'),
|
|
);
|
|
if (isChanged) {
|
|
//
|
|
let isValid =
|
|
!isNullOrUndefined(this.getTermsAndConditionsTask) &&
|
|
isFunction(this.getTermsAndConditionsTask);
|
|
if (!isValid) {
|
|
//
|
|
this.getTermsAndConditionsTask = (lang: string) => {
|
|
return this.sharedService.saherElmAPIService.terms.termsProviderService.get(
|
|
lang,
|
|
);
|
|
};
|
|
}
|
|
}
|
|
|
|
//
|
|
if (applyChanged) {
|
|
this.detectChanges();
|
|
}
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region Task Handlers ...
|
|
/**
|
|
* Handle Task Start ...
|
|
*
|
|
* @param name
|
|
* @param task
|
|
*/
|
|
async onTaskStart(name: string, task: XTask<any>) {
|
|
super.onTaskStart(name, task);
|
|
|
|
//
|
|
// Validate Task Name ...
|
|
let isValid = isContainsRegistrationTask(name);
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
await this.setLoadingState(true);
|
|
}
|
|
|
|
/**
|
|
* Handle Task Failed ...
|
|
*
|
|
* @param name
|
|
* @param task
|
|
*/
|
|
async onTaskFailed(name: string, task: XTask<any>) {
|
|
super.onTaskFailed(name, task);
|
|
|
|
//
|
|
// Validate Task Name ...
|
|
let isValid = isContainsRegistrationTask(name);
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
await this.setLoadingState(false);
|
|
this.showError(task.error);
|
|
}
|
|
|
|
/**
|
|
* Handle Task Finished ...
|
|
*
|
|
* @param name
|
|
* @param task
|
|
*/
|
|
async onTaskFinished(name: string, task: XTask<any>) {
|
|
super.onTaskFinished(name, task);
|
|
|
|
//
|
|
// Validate Task Name ...
|
|
let isValid = isContainsRegistrationTask(name);
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
await this.setLoadingState(false);
|
|
|
|
//
|
|
if (name === this.RegistrationTaskNames.GetTermsAndConditions) {
|
|
//
|
|
// this.showSuccess();
|
|
this.handleTermsAndConditionsTaskFinished(task.result);
|
|
}
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region Providers ...
|
|
loadStepInfo() {
|
|
//
|
|
// Read Registration Step ...
|
|
if (isNullOrUndefined(this.step)) {
|
|
//
|
|
this.step =
|
|
this.managerService.secureStorageService.readOfType<VRegistrationSteps>(
|
|
RegistrationKeys.Step,
|
|
false,
|
|
);
|
|
|
|
//
|
|
// Set Terms as First Step ...
|
|
if (
|
|
isNullOrUndefined(this.step) ||
|
|
this.step == VRegistrationSteps.UnKnown
|
|
) {
|
|
this.step = VRegistrationSteps.Terms;
|
|
}
|
|
}
|
|
|
|
//
|
|
// Read Action Request ...
|
|
if (isNullOrUndefined(this.actionRequest)) {
|
|
//
|
|
this.actionRequest =
|
|
this.managerService.secureStorageService.readOfType<XActionRequestDto>(
|
|
RegistrationKeys.ActionRequest,
|
|
false,
|
|
);
|
|
}
|
|
|
|
//
|
|
// Read Action Response ...
|
|
if (isNullOrUndefined(this.actionResponse)) {
|
|
//
|
|
this.actionResponse =
|
|
this.managerService.secureStorageService.readOfType<XActionResponseDto>(
|
|
RegistrationKeys.ActionResponse,
|
|
false,
|
|
);
|
|
}
|
|
|
|
//
|
|
// Reload Card Sub Title ...
|
|
if (!isNullOrUndefined(this.step)) {
|
|
//
|
|
let stepResourceID = getRegistrationStepResourceID(this.step);
|
|
if (!isNullOrUndefined(stepResourceID)) {
|
|
this.cardSubTitle = this.resourceProvider(stepResourceID);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Prepare Current Step ...
|
|
this.prepareStep();
|
|
|
|
//
|
|
// Seva Step Info ...
|
|
this.saveStepInfo();
|
|
}
|
|
|
|
getCardAvatar() {
|
|
//
|
|
let result: string = undefined;
|
|
|
|
//
|
|
if (
|
|
!isNullOrUndefined(this.actionRequest) &&
|
|
!isNullOrUndefined(this.actionRequest.avatar)
|
|
) {
|
|
//
|
|
return this.actionRequest.avatar.name;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Preparing Registration Step Info ...
|
|
*
|
|
* @returns
|
|
*/
|
|
prepareStep() {
|
|
//
|
|
if (
|
|
isNullOrUndefined(this.step) ||
|
|
this.step !== this.RegistrationSteps.Terms ||
|
|
!isFunction(this.getTermsAndConditionsTask) ||
|
|
isNullOrUndefined(this.getTermsAndConditionsTask)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
let isPassed = false;
|
|
|
|
//
|
|
// Required Preparation Based on Step ...
|
|
if (this.step === this.RegistrationSteps.Terms) {
|
|
//
|
|
// Check Terms and Condiions Must read or not ...
|
|
let hasTerms =
|
|
!isNullOrUndefined(this.actionRequest) &&
|
|
isNullOrEmptyString(this.actionRequest.termsAndConditions);
|
|
isPassed = hasTerms && !!this.actionRequest.acceptTerms;
|
|
|
|
//
|
|
if (!isPassed) {
|
|
//
|
|
// Read Terms and Conditions and Attach it to ActionRequest if not Exists ...
|
|
if (!hasTerms) {
|
|
this.handleReadTermsAndConditions();
|
|
}
|
|
} else {
|
|
}
|
|
|
|
//
|
|
return;
|
|
}
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region Handlers ...
|
|
handleReadTermsAndConditions() {
|
|
//
|
|
// Validate ...
|
|
let isValid =
|
|
isNullOrUndefined(this.actionRequest) ||
|
|
isNullOrEmptyString(this.actionRequest.termsAndConditions);
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
this.issueGetTermsAndConditionsTask();
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region Private ...
|
|
private saveStepInfo() {
|
|
//
|
|
// Step ...
|
|
this.managerService.secureStorageService.write(
|
|
RegistrationKeys.Step,
|
|
this.step,
|
|
true,
|
|
);
|
|
|
|
//
|
|
// Action Request ...
|
|
this.managerService.secureStorageService.write(
|
|
RegistrationKeys.ActionRequest,
|
|
this.actionRequest,
|
|
true,
|
|
);
|
|
|
|
//
|
|
// Action Response ...
|
|
this.managerService.secureStorageService.write(
|
|
RegistrationKeys.ActionResponse,
|
|
this.actionResponse,
|
|
true,
|
|
);
|
|
}
|
|
|
|
private async issueGetTermsAndConditionsTask() {
|
|
//
|
|
const name = this.RegistrationTaskNames.GetTermsAndConditions;
|
|
|
|
//
|
|
let lang = '';
|
|
if (
|
|
!isNullOrUndefined(this.actionRequest) &&
|
|
!isNullOrEmptyString(this.actionRequest.lang)
|
|
) {
|
|
lang = this.actionRequest.lang;
|
|
} else {
|
|
lang = this.managerService.currentLocale;
|
|
}
|
|
if (isNullOrEmptyString(lang)) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
const task = this.getTermsAndConditionsTask(lang);
|
|
if (isNullOrUndefined(task)) {
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Add Or Update Task ...
|
|
await this.addOrUpdateTask({
|
|
name,
|
|
task,
|
|
affectLoading: true,
|
|
priority: XTaskPriority.High,
|
|
});
|
|
|
|
//
|
|
// Dispatch Task ...
|
|
await this.dispatchTask(name);
|
|
}
|
|
|
|
private handleTermsAndConditionsTaskFinished(result: string) {
|
|
console.log('recieved terms: ', result);
|
|
}
|
|
//#endregion
|
|
}
|