|
| 1 | +import { Component, Input, OnInit } from "@angular/core"; |
| 2 | +import { OpenmrsHttpClientService } from "../../modules/openmrs-http-client/services/openmrs-http-client.service"; |
| 3 | +import { Observable } from "rxjs"; |
| 4 | +import { Dropdown } from "../../modules/form/models/dropdown.model"; |
| 5 | +import { FormValue } from "../../modules/form/models/form-value.model"; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: "app-shared-remote-patient-history", |
| 9 | + templateUrl: "./shared-remote-patient-history.component.html", |
| 10 | + styleUrl: "./shared-remote-patient-history.component.scss", |
| 11 | +}) |
| 12 | +export class SharedRemotePatientHistoryComponent implements OnInit { |
| 13 | + @Input() patient: any; |
| 14 | + @Input() activeVisit: any; |
| 15 | + selectedIdentifier: string; |
| 16 | + remotePatientHistory$: Observable<any>; |
| 17 | + identifierFormField: any; |
| 18 | + constructor(private httpClientService: OpenmrsHttpClientService) {} |
| 19 | + |
| 20 | + ngOnInit(): void { |
| 21 | + this.selectedIdentifier = this.getPreferredIdentifier( |
| 22 | + this.patient?.identifiers |
| 23 | + ); |
| 24 | + this.identifierFormField = new Dropdown({ |
| 25 | + id: "identifierType", |
| 26 | + key: "identifierType", |
| 27 | + label: "Identifier Types", |
| 28 | + options: this.patient?.identifiers?.map((identifier: any) => { |
| 29 | + return { |
| 30 | + id: identifier?.identifierType?.display, |
| 31 | + key: identifier?.identifierType?.display, |
| 32 | + label: identifier?.identifierType?.display, |
| 33 | + value: identifier?.identifier, |
| 34 | + }; |
| 35 | + }), |
| 36 | + }); |
| 37 | + this.getRemoteHistory(); |
| 38 | + } |
| 39 | + |
| 40 | + getRemoteHistory(): void { |
| 41 | + this.remotePatientHistory$ = this.httpClientService.get( |
| 42 | + `icare/sharedrecords?id=` + this.selectedIdentifier |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + onFormUpdate(formValue: FormValue): void { |
| 47 | + const values = formValue.getValues(); |
| 48 | + const identifierType = values?.identifierType?.value; |
| 49 | + if (identifierType) { |
| 50 | + this.selectedIdentifier = this.getIdentifierByIdentifierType( |
| 51 | + identifierType, |
| 52 | + this.patient?.identifiers |
| 53 | + ); |
| 54 | + this.getRemoteHistory(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + getIdentifierByIdentifierType( |
| 59 | + identifierType: string, |
| 60 | + identifiers: any[] |
| 61 | + ): string { |
| 62 | + return (identifiers?.filter( |
| 63 | + (identifier: any) => |
| 64 | + identifier?.identifierType?.display === identifierType |
| 65 | + ) || [])[0]?.identifier; |
| 66 | + } |
| 67 | + |
| 68 | + getPreferredIdentifier(identifiers: any[]): string { |
| 69 | + return (identifiers?.filter((identifier: any) => identifier?.preferred) || |
| 70 | + [])[0]?.identifier; |
| 71 | + } |
| 72 | +} |
0 commit comments