import React, { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { MultiSelect, Search, TabPanel } from '@carbon/react'; import { useConfig, useDebounce, useSession } from '@openmrs/esm-framework'; import { type PharmacyConfig } from '../config-schema'; import PrescriptionsTable from './prescriptions-table.component'; import styles from './prescriptions.scss'; import { type SimpleLocation } from '../types'; interface PrescriptionTabPanelProps { isTabActive: boolean; status?: string; customPrescriptionsTableEndpoint?: string; locations: SimpleLocation[]; isLocationsLoading: boolean; } const PrescriptionTabPanel: React.FC = ({ status, isTabActive, customPrescriptionsTableEndpoint, locations, isLocationsLoading, }) => { const { t } = useTranslation(); const config = useConfig(); const isInitialized = useRef(false); const { sessionLocation } = useSession(); const [searchTerm, setSearchTerm] = useState(''); const debouncedSearchTerm = useDebounce(searchTerm, 500); const [filterLocations, setFilterLocations] = useState([]); // set any initially selected locations useEffect(() => { if (!isInitialized.current && !isLocationsLoading && sessionLocation?.uuid) { setFilterLocations( locations?.filter((l) => { return sessionLocation?.uuid === l.associatedPharmacyLocation; }) || [], ); isInitialized.current = true; // we only want to run when the component is first mounted so we don't override user changes } }, [isLocationsLoading, sessionLocation, locations]); return (
{config.locationBehavior?.locationFilter?.enabled && !isLocationsLoading && isInitialized.current && locations?.length > 1 && ( item?.name} onChange={({ selectedItems }) => { setFilterLocations(selectedItems); }} className={styles.locationFilter} /> )} { e.preventDefault(); setSearchTerm(e.target.value); }} size="md" className={styles.patientSearch} />
); }; export default PrescriptionTabPanel;