import React, { useState, useEffect } from 'react'; import { usePracticeTypeOptions } from '../../config/serviceCategories'; import { Card, CardHeader, CardBody, CardDivider } from '../ui/wp-card'; import { Elevation } from '../ui/elevation'; import { InputControl } from '../ui/input-control'; import { Divider } from '../ui/divider'; import { DismissibleAlert } from '../ui/dismissible-alert'; import { Heading } from '../ui/heading'; import { Flex, FlexItem, FlexBlock } from '../ui/flex'; import { Spacer } from '../ui/spacer'; import { TabPanel } from '../ui/tab-panel'; import { useFormContext, Controller, useFieldArray, useWatch } from 'react-hook-form'; import { IValueSet, useValueSetServiceContext } from '../../services'; import { FhirAddress, FhirCode, FhirCodeableConcept, FhirContactPoint, FhirIdentifier, FhirReference, PhotoAttachment } from '../../components'; import { PlusCircle, XCircle } from 'lucide-react'; import { Attachment } from 'fhir/r4'; // - WordPress's ToggleControl, swapped for the ui/* adapter. Same props, same JSX; // only `__nextHasNoMarginBottom` is dropped, which means nothing outside WordPress. import { ToggleControl } from '../ui/toggle-control'; interface FhirOrganizationProps { photo?: Attachment; onPhotoChange?: (photo: Attachment | undefined) => void; elevated?: boolean; disabled?: boolean; } export const FhirOrganization: React.FC = ({ photo, onPhotoChange, elevated = true, disabled = false }) => { const { control, watch, getValues } = useFormContext(); const practiceTypeOptions = usePracticeTypeOptions(); const { fetchValueSet, fetchTimezoneOptions, valueSetUrls } = useValueSetServiceContext(); const [loading, setLoading] = useState(false); // value sets const [careTeamMemberFunction, setCareTeamMemberFunction] = useState([]); const [practitionerSpecialty, setPractitionerSpecialty] = useState([]); const [timezones, setTimezones] = useState([]); const spacerMargin = '24px'; const isPartOf = watch('isPartOf'); const aliasFieldArray = useFieldArray({ control: control, name: 'alias' }); const orgIdentifierFieldArray = useFieldArray({ control: control, name: 'identifier' }); const npiFieldArray = useFieldArray({ control: control, name: 'npi' }); const cliaFieldArray = useFieldArray({ control: control, name: 'clia' }); const naicFieldArray = useFieldArray({ control: control, name: 'naic' }); const addressFieldArray = useFieldArray({ control: control, name: 'address' }); const telecomFieldArray = useFieldArray({ control: control, name: 'telecom' }); const endpointFieldArray = useFieldArray({ control, name: 'endpoint' }); useWatch({ control, name: 'alias' }); useEffect(() => { const loadValueSets = async () => { setLoading(true); try { const [ careTeamMemberFunctionValueSet, practitionerSpecialtyValueSet, timezonesValueSet ] = await Promise.all([ fetchValueSet(valueSetUrls.careTeamMemberFunction), fetchValueSet(valueSetUrls.practitionerSpecialty), fetchTimezoneOptions() ]); setCareTeamMemberFunction(careTeamMemberFunctionValueSet); setPractitionerSpecialty(practitionerSpecialtyValueSet); setTimezones(timezonesValueSet) } catch (err) { console.error('Error loading value sets:', err); } finally { setLoading(false); } }; loadValueSets(); }, [fetchValueSet, fetchTimezoneOptions, valueSetUrls]); const canAddAlias = () => { const aliases = getValues('alias'); const lastAlias = aliases[aliases.length - 1]; return lastAlias.trim() !== ''; }; const addAlias = () => { if (canAddAlias()) { aliasFieldArray.append(''); } }; const removeAlias = (index: number) => { aliasFieldArray.remove(index); }; return ( {elevated && } Organization Information {onPhotoChange && ( <> )} {/* name */} ( <> {fieldState.error && ( Organization name is required )} )}/> {/* active */} ( )}/> {/* timezone */} {/* isPartOf */} {/* (*/} {/* */} {/* )}/>*/} {/* partOf */} {isPartOf && ( <> )} {/* type */} {/* Aliases */} Aliases {/* alias */} {aliasFieldArray.fields.map((field, index) => (
( 1 ? index + 1 : ''}`}/> )}/> {index === aliasFieldArray.fields.length - 1 && ( )} {aliasFieldArray.fields.length > 1 && ( )}
))}
{(tab) => { // address if (tab.name === 'address') { return (
{/* address */}
); } // npi if (tab.name === 'npi') { return (
{/* NPI */}
); } // clia if (tab.name === 'clia') { return (
{/* CLIA */}
); } // naic if (tab.name === 'naic') { return (
{/* NAIC */}
); } // identifiers if (tab.name === 'identifiers') { return (
{/* Other Identifiers */}
); } // contact if (tab.name === 'contact') { return (
{/* telecom */}
); } // contacts if (tab.name === 'contacts') { return (
{/**/} {/* */} {/* Organization Contacts*/} {/* */} {/* */} {/* */} {/* */} {/**/} {/**/} {/**/} {/*{contactFieldArray.fields.length === 0 && (*/} {/* */} {/* No contacts specified.*/} {/* */} {/*)}*/} {/*{contactFieldArray.fields.map((field, index) => (*/} {/* */} {/* */} {/* */} {/* */} {/* Contact {index + 1}*/} {/* */} {/* */} {/*
); } }}
); }; export default FhirOrganization;