src/app/modules/org-management/components/create-org-type/create-org-type.component.ts
This component helps to display the creation/updation popup.
It also creates and updates organisation type.
OnInit
OnDestroy
AfterViewInit
selector | app-create-org-type |
templateUrl | ./create-org-type.component.html |
Properties |
|
Methods |
constructor(activatedRoute: ActivatedRoute, resourceService: ResourceService, toasterService: ToasterService, routerNavigationService: RouterNavigationService, orgTypeService: OrgTypeService, navigationhelperService: NavigationHelperService)
|
||||||||||||||||||||||||||||
Constructor to create injected service(s) object Default method of DeleteComponent class
Parameters :
|
addOrgType |
addOrgType()
|
This method calls the add organisation type API with the organisation type name. After success or failure it is redirected to the organisation type listing page with proper messaga.
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
This method helps to identify that the page is creation or updation by subscribing the actiavtedRoute url. It also sets the data to the updation form by subscribing the activatedRoute param
Returns :
void
|
redirect |
redirect()
|
This method helps to redirect to the parent component page, i.e, view organisation type page
Returns :
void
|
setInteractEventData |
setInteractEventData()
|
Returns :
void
|
updateOrgType |
updateOrgType()
|
This method calls the update organisation type API with a object of organisation type identifier and name. After success or failure it is redirected to the organisation type listing page with proper messaga.
Returns :
void
|
Public activatedRoute |
Type : ActivatedRoute
|
To send activatedRoute.snapshot to routerNavigationService |
Public addOrganizationType |
Type : IInteractEventEdata
|
Public cancelModal |
Type : IInteractEventEdata
|
createForm |
Default value : true
|
This flag helps to identify whether a form is creation or updation. It is used to display the creation/updation form. |
disableApproveBtn |
Default value : false
|
modal |
Decorators :
@ViewChild('modal', {static: true})
|
Public navigationhelperService |
Type : NavigationHelperService
|
orgName |
Default value : new UntypedFormControl()
|
Creates a object of the form control |
orgTypeId |
Type : string
|
Contains the organisation type identifier |
Public orgTypeService |
Type : OrgTypeService
|
To call OrgType Service for craeting/updating organisation type |
pageId |
Type : string
|
pageUri |
Type : string
|
page uri for telemetry |
Public resourceService |
Type : ResourceService
|
To call resource service which helps to use language constant |
Public routerNavigationService |
Type : RouterNavigationService
|
To navigate back to parent component |
telemetryImpression |
Type : IImpressionEventInput
|
telemetryImpression |
Private toasterService |
Type : ToasterService
|
To show toaster(error, success etc) after any API calls |
Public unsubscribe$ |
Default value : new Subject<void>()
|
Public updateOrganizationType |
Type : IInteractEventEdata
|
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ResourceService, ToasterService, RouterNavigationService, ServerResponse, NavigationHelperService } from '@sunbird/shared';
import { OrgTypeService } from './../../services/';
import { UntypedFormControl } from '@angular/forms';
import * as _ from 'lodash-es';
import { IImpressionEventInput, IInteractEventEdata } from '@sunbird/telemetry';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
/**
* This component helps to display the creation/updation popup.
*
* It also creates and updates organisation type.
*/
@Component({
selector: 'app-create-org-type',
templateUrl: './create-org-type.component.html'
})
export class CreateOrgTypeComponent implements OnInit, OnDestroy, AfterViewInit {
public addOrganizationType: IInteractEventEdata;
public updateOrganizationType: IInteractEventEdata;
public cancelModal: IInteractEventEdata;
@ViewChild('modal', {static: true}) modal;
pageId: string;
/**
* telemetryImpression
*/
telemetryImpression: IImpressionEventInput;
/**
* page uri for telemetry
*/
pageUri: string;
/**
* This flag helps to identify whether a form is creation or updation.
* It is used to display the creation/updation form.
*/
createForm = true;
/**
* Creates a object of the form control
*/
orgName = new UntypedFormControl();
/**
* Contains the organisation type identifier
*/
orgTypeId: string;
/**
* To send activatedRoute.snapshot to routerNavigationService
*/
public activatedRoute: ActivatedRoute;
/**
* To call resource service which helps to use language constant
*/
public resourceService: ResourceService;
/**
* To show toaster(error, success etc) after any API calls
*/
private toasterService: ToasterService;
/**
* To navigate back to parent component
*/
public routerNavigationService: RouterNavigationService;
/**
* To call OrgType Service for craeting/updating organisation type
*/
public orgTypeService: OrgTypeService;
public unsubscribe$ = new Subject<void>();
disableApproveBtn = false;
/**
* Constructor to create injected service(s) object
*
* Default method of DeleteComponent class
*
* @param {ActivatedRoute} activatedRoute Reference of ActivatedRoute
* @param {ResourceService} resourceService Reference of ResourceService
* @param {ToasterService} toasterService Reference of ToasterService
* @param {RouterNavigationService} routerNavigationService Reference of routerNavigationService
* @param {OrgTypeService} orgTypeService Reference of OrgTypeService
*/
constructor(activatedRoute: ActivatedRoute,
resourceService: ResourceService,
toasterService: ToasterService,
routerNavigationService: RouterNavigationService,
orgTypeService: OrgTypeService,
public navigationhelperService: NavigationHelperService) {
this.activatedRoute = activatedRoute;
this.resourceService = resourceService;
this.toasterService = toasterService;
this.routerNavigationService = routerNavigationService;
this.orgTypeService = orgTypeService;
}
/**
* This method calls the add organisation type API with the organisation
* type name.
*
* After success or failure it is redirected to the organisation type listing page
* with proper messaga.
*/
addOrgType(): void {
this.orgTypeService.addOrgType(this.orgName.value).pipe(
takeUntil(this.unsubscribe$))
.subscribe(
(apiResponse: ServerResponse) => {
this.toasterService.success(this.resourceService.messages.smsg.m0035);
this.modal.deny();
this.redirect();
},
err => {
this.toasterService.error(err.error.params.errmsg);
this.redirect();
}
);
}
/**
* This method calls the update organisation type API with a object of
* organisation type identifier and name.
*
* After success or failure it is redirected to the organisation type listing page
* with proper messaga.
*/
updateOrgType(): void {
const param = { 'id': this.orgTypeId, 'name': this.orgName.value };
this.orgTypeService.updateOrgType(param).pipe(
takeUntil(this.unsubscribe$))
.subscribe(
(apiResponse: ServerResponse) => {
this.toasterService.success(this.orgName.value + ' ' + this.resourceService.messages.smsg.m0037);
this.modal.deny();
this.redirect();
},
err => {
this.toasterService.error(err.error.params.errmsg);
this.redirect();
}
);
}
/**
* This method helps to redirect to the parent component
* page, i.e, view organisation type page
*
*/
redirect(): void {
this.routerNavigationService.navigateToParentUrl(this.activatedRoute.snapshot);
}
/**
* This method helps to identify that the page is creation
* or updation by subscribing the actiavtedRoute url.
*
* It also sets the data to the updation form by subscribing the
* activatedRoute param
*/
ngOnInit() {
this.activatedRoute.url.subscribe(url => {
if (url[0].path === 'update') {
this.createForm = false;
this.pageUri = 'orgType/update/' + this.orgTypeId;
this.pageId = 'update-organization-type';
this.orgTypeService.orgTypeData$.subscribe((orgTypeList) => {
if (orgTypeList && orgTypeList.orgTypeData) {
_.find(orgTypeList.orgTypeData.result.response, (orgList) => {
this.orgTypeId = this.activatedRoute.snapshot.params.orgId;
if (orgList.id === this.orgTypeId) {
this.orgName = new UntypedFormControl(orgList.name);
return true;
}
});
}
});
} else if (url[0].path === 'create') {
this.createForm = true;
this.pageUri = 'orgType/create';
this.pageId = 'create-organization-type';
}
});
this.setInteractEventData();
}
setInteractEventData() {
this.addOrganizationType = {
id: 'create-organization-type',
type: 'click',
pageid: this.pageId
};
this.updateOrganizationType = {
id: 'update-organization-type',
type: 'click',
pageid: this.pageId
};
this.cancelModal = {
id: 'cancel',
type: 'click',
pageid: this.pageId
};
}
ngAfterViewInit () {
setTimeout(() => {
this.telemetryImpression = {
context: {
env: this.activatedRoute.snapshot.data.telemetry.env
},
edata: {
type: this.activatedRoute.snapshot.data.telemetry.type,
pageid: this.pageId,
uri: this.pageUri,
subtype: this.activatedRoute.snapshot.data.telemetry.subtype,
duration: this.navigationhelperService.getPageLoadTime()
}
};
});
}
ngOnDestroy() {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
}
<app-modal-wrapper [config]="{disableClose: false, size: 'normal'}" (dismiss)="redirect()" #modal>
<ng-template sbModalContent>
<div class="sb-modal">
<div class="transition ui dimmer page modals active visible">
<div class="ui modal transition active visible normal" [appTelemetryImpression]="telemetryImpression">
<!--Header-->
<div *ngIf="createForm" class="sb-modal-header">
{{resourceService?.frmelmnts?.lbl?.addorgtype}}
</div>
<div *ngIf="!createForm" class="sb-modal-header">
{{resourceService?.frmelmnts?.lbl?.updateorgtype}}
</div>
<!--/Header-->
<!--Content-->
<div class="sb-modal-content">
<div class="ui fluid icon input">
<input *ngIf="createForm" class="form-control" [formControl]="orgName" type="text"
placeholder="{{resourceService?.frmelmnts?.lbl?.addorgtype}}">
<input *ngIf="!createForm" class="form-control" [formControl]="orgName" type="text"
placeholder="{{resourceService?.frmelmnts?.lbl?.updateorgtype}}">
</div>
</div>
<!--/Content-->
<!--Actions-->
<div class="sb-modal-actions">
<button class="sb-btn sb-btn-normal sb-btn-primary" *ngIf="createForm" tabindex="0"
(click)="disableApproveBtn=true; addOrgType()" [class.disabled]="disabled"
appTelemetryInteract [telemetryInteractEdata]="addOrganizationType">
{{resourceService?.frmelmnts?.btn?.add}}
</button>
<button class="sb-btn sb-btn-normal sb-btn-primary" *ngIf="!createForm" tabindex="0"
(click)="disableApproveBtn=true; updateOrgType()" [class.disabled]="disabled"
appTelemetryInteract [telemetryInteractEdata]="updateOrganizationType">
{{resourceService?.frmelmnts?.btn?.update}}
</button>
<button class="sb-btn sb-btn-normal sb-btn-outline-primary" tabindex="0" (click)="modal.deny()"
appTelemetryInteract [telemetryInteractEdata]="cancelModal">
{{resourceService?.frmelmnts?.btn?.cancel}}
</button>
</div>
<!--/Actions-->
</div>
</div>
</div>
</ng-template>
</app-modal-wrapper>