angular/projects/researchdatabox/app-config/src/app/app-config.component.ts
Application Config Component
selector | app-config |
templateUrl | ./app-config.component.html |
Properties |
|
Methods |
|
constructor(loggerService: LoggerService, utilService: UtilityService, fb: FormBuilder, document: Document, translationService: TranslationService, appConfigService: AppConfigService, formlyJsonschema: FormlyJsonschema, elementRef: ElementRef)
|
|||||||||||||||||||||||||||
Parameters :
|
Protected Async initComponent |
initComponent()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:69
|
Returns :
Promise<void>
|
onSubmit | ||||||
onSubmit(model: any)
|
||||||
Parameters :
Returns :
void
|
getInitSubject |
getInitSubject()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:71
|
For those interested in the init from RXJS-land. Note that it returns a BehaviorSubject instance, and will have an initial value of false, so process the return value as needed.
Returns :
Subject<any>
|
isInitializing |
isInitializing()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:95
|
Main flag to indicate the init status
Returns :
boolean
|
ngOnInit |
ngOnInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:50
|
Returns :
void
|
Async waitForDeps |
waitForDeps()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:78
|
Wait for dependencies to initialise
Returns :
Promise<any>
|
Async waitForInit |
waitForInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:89
|
For those interested in the init from the Promise-land
Returns :
Promise<any>
|
configKey |
Type : any
|
fields |
Type : FormlyFieldConfig[]
|
Default value : [
]
|
form |
Default value : new FormGroup({})
|
formSaveSuccessful |
Type : boolean
|
Default value : false
|
formSaveUnsuccessful |
Type : boolean
|
Default value : false
|
formSaving |
Type : boolean
|
Default value : false
|
model |
Type : object
|
Default value : {}
|
window |
Type : any
|
Protected brandingAndPortalUrl |
Type : string
|
Default value : ''
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:42
|
Private filterFn |
Default value : function(initStat: boolean) { return initStat; }
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:40
|
Protected initDependencies |
Type : Initable[]
|
Default value : []
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:39
|
Protected initSubject |
Type : BehaviorSubject<any>
|
Default value : new BehaviorSubject(false)
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:38
|
Protected isReady |
Type : boolean
|
Default value : false
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:37
|
import { Component, ElementRef, Inject } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { DOCUMENT } from "@angular/common"
import { BaseComponent, UtilityService, LoggerService, TranslationService, AppConfigService } from '@researchdatabox/portal-ng-common';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
import { JSONSchema7 } from 'json-schema';
import { AppConfig } from 'projects/researchdatabox/portal-ng-common/src/public-api';
import { clone as _clone } from 'lodash'
/**
* Application Config Component
*
*/
@Component({
selector: 'app-config',
templateUrl: './app-config.component.html'
})
export class AppConfigComponent extends BaseComponent {
window: any;
form = new FormGroup({});
model = {};
fields: FormlyFieldConfig[] = [
];
configKey: any;
formSaving: boolean = false;
formSaveSuccessful: boolean = false;
formSaveUnsuccessful: boolean = false;
constructor(
@Inject(LoggerService) private loggerService: LoggerService,
@Inject(UtilityService) protected utilService: UtilityService,
@Inject(FormBuilder) private fb: FormBuilder,
@Inject(DOCUMENT) private document: Document,
@Inject(TranslationService) private translationService: TranslationService,
@Inject(AppConfigService) private appConfigService: AppConfigService,
private formlyJsonschema: FormlyJsonschema,
@Inject(ElementRef) elementRef: ElementRef
) {
super();
this.loggerService.debug(`AppConfig waiting for deps to init...`);
this.window = this.document.defaultView;
// set this component's dependencies
this.configKey = elementRef.nativeElement.getAttribute('configKey');
this.initDependencies = [translationService, appConfigService];
}
protected override async initComponent(): Promise<void> {
let result: AppConfig = await this.appConfigService.getAppConfigForm(this.configKey)
const jsonObject: JSONSchema7 = result.schema as JSONSchema7;
const fieldOrder = result.fieldOrder;
let originalProperties = _clone(jsonObject.properties);
jsonObject.properties = {};
for (let field of fieldOrder) {
if (originalProperties) {
jsonObject.properties[field] = originalProperties[field];
}
}
this.fields = [this.formlyJsonschema.toFieldConfig(jsonObject)]
this.model = result.model;
this.loggerService.debug(`AppConfig initialised.`);
}
onSubmit(model: any) {
this.formSaving = true;
this.formSaveUnsuccessful = false;
this.formSaveSuccessful = false;
this.appConfigService.saveAppConfig(this.configKey, model).then((result: AppConfig) => {
this.formSaveSuccessful = true;
setTimeout(() => {
this.formSaveSuccessful = false;
}, 3000);
this.formSaving = false;
}).catch((error: any) => {
this.formSaveSuccessful = false;
this.formSaveUnsuccessful = true;
this.formSaving = false;
});
}
}
<!-- ngIf is needed so init completes and view has all the information it needs -->
<ng-container *ngIf="isReady">
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<p class="alert-info text-16 padding-10 mt-2" *ngIf="formSaving">Configuration is being saved <i class="fa fa-spinner fa-pulse fa-1x fa-fw"></i></p>
<p class="alert-success text-16 padding-10 mt-2" *ngIf="formSaveSuccessful">Configuration saved successfully</p>
<p class="alert-danger text-16 padding-10 mt-2" *ngIf="formSaveUnsuccessful">Configuration failed to save. Please try again.</p>
</ng-container>