Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WiktorNowikow/5062bc27eb5f209541353a3f40d4b7bc to your computer and use it in GitHub Desktop.
Save WiktorNowikow/5062bc27eb5f209541353a3f40d4b7bc to your computer and use it in GitHub Desktop.
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { MessageService} from 'primeng/api';
import { ObjectOperations } from 'app/Infrastructure/object-operations';
import { FormBuilder, FormControl, Validators, FormGroup } from '@angular/forms';
import * as _ from 'lodash';
import { InstitutionWithoutInnDto, InstitutionsWithoutInnClient} from 'app/OpenMedis-api';
import { DatePipe } from '@angular/common';
import { BehaviorSubject, Subscription } from 'rxjs';
import { InstitutionCommonInfo } from 'app/models/InstitutionCommonInfo';
import { TranslateService } from '@ngx-translate/core';
import { CheckPermissionService } from 'app/common/check-permission.service';
import { InstitutionsWithoutInnDetailFacade } from './institutions-without-inn-detail.facade';
import { PERMISSIONS, Permissions } from 'app/common/permissions-list';
@Component({
selector: 'app-institutions-without-inn-detail',
templateUrl: './institutions-without-inn-detail.component.html',
styleUrls: ['./institutions-without-inn-detail.component.css'],
providers: [ObjectOperations]
})
export class InstitutionsWithoutInnDetailComponent implements OnInit {
@Input() parametrItem:BehaviorSubject<InstitutionWithoutInnDto>;
public item: InstitutionWithoutInnDto=new InstitutionWithoutInnDto();
public InstitutionWithInn:boolean=false;
//glossaries
public glosItemStatuses$:any=new BehaviorSubject<any>(null);
public unitsHealthCareSystemsOfHealthCareSystem$:any=new BehaviorSubject<any>(null);
public typesAndLevels$:any=new BehaviorSubject<any>(null);
public institutionCommonInfo$:any=new BehaviorSubject<InstitutionCommonInfo>(null);
userform: FormGroup;
id:number;
NoEntry:string;
Success:string;
FormSubnited:string;
Error:string;
ValidationFailed:string;
public permissions: Permissions =PERMISSIONS;
saveItemSubscription: Subscription;
currentItemSubscription: Subscription;
constructor(
private fb: FormBuilder,
private facade: InstitutionsWithoutInnDetailFacade,
public currentClient: InstitutionsWithoutInnClient,
public messageService: MessageService,
private objectOperations: ObjectOperations,
public translateService: TranslateService,
public checkPermissionService:CheckPermissionService
){}
ngOnInit(): void {
this.translateService.stream(['NoEntry','Success','FormSubnited','Error','ValidationFailed' ]).subscribe(value =>
{
this.NoEntry=value['NoEntry'];
this.Success=value['Success'];
this.FormSubnited=value['FormSubnited'];
this.Error=value['Error'];
this.ValidationFailed=value['ValidationFailed'];
});
this.saveItemSubscription = this.facade.getSubject().subscribe(result => {
if (Boolean(result)) {
this.messageService.add({key: 'success', severity:'info', summary:this.Success, detail:this.FormSubnited});
this.userform.markAsPristine();
} else {
this.messageService.add({key: 'error',severity:'error', summary:this.Error, detail:this.ValidationFailed});
}
});
this.facade.loadGlosItemStatuses();
this.glosItemStatuses$ = this.facade.getGlosItemStatuses();
this.facade.loadUnitsHealthCareSystemsOfHealthCareSystem();
this.unitsHealthCareSystemsOfHealthCareSystem$ = this.facade.getUnitsHealthCareSystemsOfHealthCareSystem();
this.facade.loadTypesAndLevels();
this.typesAndLevels$ = this.facade.getTypesAndLevels();
this.userform = this.fb.group({
"active":new FormControl('', Validators.compose([Validators.required])),
"institutionType":new FormControl('', Validators.compose([Validators.required])),
"typeOrLevel":new FormControl('', Validators.compose([Validators.required])),
"name0_ru":new FormControl('', Validators.compose([Validators.required])),
"name0_uz":new FormControl('', Validators.compose([Validators.required])),
"name0_uzlat":new FormControl('', Validators.compose([Validators.required])),
"name0_kar":new FormControl('', Validators.compose([Validators.required])),
"name0_karlat":new FormControl('', Validators.compose([Validators.required])),
"name0_lat":new FormControl('', Validators.compose([Validators.required])),
"institutionRegion":new FormControl(),
"institutionDistrict":new FormControl(),
"headInstitutionInn":new FormControl('', Validators.compose([Validators.required])),
"comment":new FormControl()
});
this.currentItemSubscription=this.facade.getItem().subscribe(item=>{
this.facade.setInstitution(item);
this.institutionCommonInfo$=this.facade.getInstitution();
if(item?.id)
{
this.item = item;
this.id=item.id;
this.userform.patchValue(item);
}
});
}
ngOnDestroy() {
// unsubscribe to ensure no memory leaks
this.saveItemSubscription.unsubscribe();
this.currentItemSubscription.unsubscribe();
}
save() {
if(this.userform.pristine)return;//if from without changes
//map data from form controls to item entity
Object.keys(this.userform.controls).forEach(key => { this.item[key] = this.userform.get(key).value;});
//Update entity
if(this.id!=undefined)
{
this.facade.Update(this.id,this.item);
}
//create entity
else{
this.facade.Add(this.item);
}
}
setInstitutionAddress(event){
this.userform.controls['institutionRegion'].setValue(event.region);
this.userform.controls['institutionDistrict'].setValue(event.district);
this.userform.controls['headInstitutionInn'].setValue(event.institutionIdentifier);
}
}
import { Injectable } from '@angular/core';
import { InstitutionsWithoutInnDetailState } from './institutions-without-inn-detail.state';
import { InstitutionsWithoutInnClient, InstitutionWithoutInnDto, CommonLocalGlossariesClient, CommonGlossariesClient } from 'app/OpenMedis-api';
import { Subject } from 'rxjs';
import { InstitutionCommonInfo } from 'app/models/InstitutionCommonInfo';
@Injectable({
providedIn: 'root'
})
export class InstitutionsWithoutInnDetailFacade {
private subject = new Subject<any>();
private subjectAdded = new Subject<any>();
private subjectUpdated = new Subject<any>();
constructor(
private state: InstitutionsWithoutInnDetailState,
private commonLocalGlossariesClient: CommonLocalGlossariesClient,
private commonGlossariesClient: CommonGlossariesClient,
public currentClient: InstitutionsWithoutInnClient){}
public getSubject(){
return this.subject.asObservable();
}
public getAddedSubject(){
return this.subjectAdded.asObservable();
}
public getUpdatedSubject(){
return this.subjectUpdated.asObservable();
}
loadGlosItemStatuses(){
this.commonLocalGlossariesClient
.getGlosItemStatuses()
.subscribe(result=> {
this.state.setGlosItemStatuses(result.map(p => ({label: p.key, value: p.value})));
let glosItemStatusesForTable=[];
result.map(p => glosItemStatusesForTable[p.value]=p.key);
this.state.setGlosItemStatusesForTable(glosItemStatusesForTable);
},error => console.error(error));
}
getGlosItemStatuses(){
return this.state.getGlosItemStatuses();
}
getGlosItemStatusesForTable(){
return this.state.getGlosItemStatusesForTable;
}
loadUnitsHealthCareSystemsOfHealthCareSystem(){
this.commonGlossariesClient.getUnitsHealthCareSystemsOfHealthCareSystemByLevel(3)
.subscribe(result=>{
this.state.setUnitsHealthCareSystemsOfHealthCareSystem(result.map(p => ({label: p.key, value: p.value})));
});
}
loadTypesAndLevels(){
this.commonLocalGlossariesClient.getSeparationOfInstitutionsByTypeAndLevel()
.subscribe(result=>{
this.state.setTypesAndLevels( result.map(p => ({label: p.key, value: p.value})));
});
}
getUnitsHealthCareSystemsOfHealthCareSystem(){
return this.state.getUnitsHealthCareSystemsOfHealthCareSystem();
}
getTypesAndLevels(){
return this.state.getTypesAndLevels();
}
setInstitution(item){
if(!item || !item.id) return;
let institutionCommonInfo:InstitutionCommonInfo = new InstitutionCommonInfo();
this.commonGlossariesClient.getInstitutionById(item.headInstitutionInn)
.subscribe(result=>{
institutionCommonInfo.institutionDistrict=result.soato_district;
institutionCommonInfo.institutionId= item.id;
institutionCommonInfo.institutionOrgIdentifier= result.sp_inn;
institutionCommonInfo.institutionRegion=result.soato_region;
institutionCommonInfo.institutionWithInn=false;
institutionCommonInfo.typeOrLevel= item.typeOrLevel;
this.state.setInstitution(institutionCommonInfo);
});
}
getInstitution(){
return this.state.getInstitution();
}
setItem(item){this.state.setItem(item);}
getItem(){ return this.state.getItem();}
Add(item: InstitutionWithoutInnDto){
this.currentClient.create(item).subscribe(
newItem =>{
this.state.setItem(newItem);
this.subject.next(true);
this.subjectAdded.next(true);
},
error => {
console.error(error);
this.subject.next(false);
}
);
}
Update(id:number,item:InstitutionWithoutInnDto){
this.currentClient.update(id,item).subscribe(
() =>{ this.state.setItem(item);
this.subject.next(true);
this.subjectUpdated.next(true);
},
error => {
console.error(error);
this.subject.next(false);
}
);
}
}
import { Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { InstitutionCommonInfo } from 'app/models/InstitutionCommonInfo';
@Injectable({
providedIn: 'root'
})
export class InstitutionsWithoutInnDetailState{
private item$: any=new BehaviorSubject<any>(null);
//glossaries
private glosItemStatuses$:any=new BehaviorSubject<any>(null);
private glosItemStatusesForTable$:any=new BehaviorSubject<any>(null);
private unitsHealthCareSystemsOfHealthCareSystem$:any=new BehaviorSubject<any>(null);
private typesAndLevels$:any=new BehaviorSubject<any>(null);
private institutionCommonInfo$:any=new BehaviorSubject<InstitutionCommonInfo>(null);
constructor(){}
setGlosItemStatuses(items:any){
this.glosItemStatuses$.next(items);
}
setGlosItemStatusesForTable(items:any){
this.glosItemStatusesForTable$.next(items);
}
getGlosItemStatuses(){
return this.glosItemStatuses$.asObservable();
}
getGlosItemStatusesForTable(){
return this.glosItemStatusesForTable$.asObservable();
}
setUnitsHealthCareSystemsOfHealthCareSystem(items:any){
this.unitsHealthCareSystemsOfHealthCareSystem$.next(items);
}
setTypesAndLevels(items:any){
this.typesAndLevels$.next(items);
}
getUnitsHealthCareSystemsOfHealthCareSystem(){
return this.unitsHealthCareSystemsOfHealthCareSystem$.asObservable();
}
getTypesAndLevels(){
return this.typesAndLevels$.asObservable();
}
setInstitution(institutionCommonInfo){
this.institutionCommonInfo$.next(institutionCommonInfo);
}
getInstitution(){
return this.institutionCommonInfo$.asObservable();
}
public setItem(item: any){
this.item$.next(item);
}
public getItem(){
return this.item$.asObservable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment