import { DismissibleAlert } from '../ui/dismissible-alert'; import { CustomSelectControl } from "../ui/custom-select-control"; import type { CustomSelectOption } from "../ui/custom-select-control"; import { Controller, useFormContext } from 'react-hook-form'; import { PractitionerRole, Practitioner, Reference } from 'fhir/r4'; import { PractitionerWithRoles, usePractitionerRoleServiceContext, useFhirServiceContext } from '../../services'; interface FhirPractitionerRoleSelectorProps { label: string; context: string; practitionersList: PractitionerWithRoles[]; required?: boolean; disabled?: boolean; } interface PractitionerRoleValue { practitioner: Practitioner; role: PractitionerRole; } const PractitionerRoleSelector = ({ label, context, practitionersList, required = false, disabled = false }: FhirPractitionerRoleSelectorProps) => { const { control } = useFormContext(); const { getPractitionerRoleDisplay } = usePractitionerRoleServiceContext(); const { extractReferenceId } = useFhirServiceContext(); const options = [ { key: null, name: '', value: null }, ...practitionersList.flatMap(practitionerWithRoles => practitionerWithRoles.roles.map((role: PractitionerRole) => ({ key: role.id as string, name: getPractitionerRoleDisplay(role), value: { practitioner: practitionerWithRoles.practitioner, role: role } as PractitionerRoleValue })) ) ]; const getCurrentOption = (fieldValue: Reference | null) => { if (!fieldValue) { return options[0]; } return options.find(option => option.value && option.value.role?.id === extractReferenceId(fieldValue)) || options[0]; }; return ( { return ( <> { field.onChange(newValue.selectedItem.value); }}/> {fieldState.error && ( Practitioner role is required )} ); }}/> ); }; export default PractitionerRoleSelector;