-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 1478-servicepoint-parent
- Loading branch information
Showing
10 changed files
with
660 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/react-utils/src/components/AsyncSelect/ClientSideActionsSelect/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react'; | ||
import { URLParams } from '@opensrp/server-service'; | ||
import { useQuery } from 'react-query'; | ||
import { Divider, Select, Empty, Spin, Alert } from 'antd'; | ||
import { IResource } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IResource'; | ||
import { getResourcesFromBundle } from '../../../helpers/utils'; | ||
import { useTranslation } from '../../../mls'; | ||
import { loadAllResources } from '../../../helpers/fhir-utils'; | ||
import { | ||
AbstractedSelectOptions, | ||
defaultSelectFilterFunction, | ||
SelectOption, | ||
TransformOptions, | ||
} from '../utils'; | ||
|
||
export interface ClientSideActionsSelectProps<ResourceT extends IResource> | ||
extends AbstractedSelectOptions<ResourceT> { | ||
fhirBaseUrl: string; | ||
resourceType: string; | ||
extraQueryParams?: URLParams; | ||
transformOption: TransformOptions<ResourceT>; | ||
getFullOptionOnChange?: (obj: SelectOption<ResourceT> | SelectOption<ResourceT>[]) => void; | ||
} | ||
|
||
/** | ||
* Select component that loads all options as a single resource | ||
* | ||
* @param props - component props | ||
*/ | ||
export function ClientSideActionsSelect<ResourceT extends IResource>( | ||
props: ClientSideActionsSelectProps<ResourceT> | ||
) { | ||
const { | ||
fhirBaseUrl, | ||
resourceType, | ||
extraQueryParams = {}, | ||
transformOption, | ||
onChange, | ||
getFullOptionOnChange, | ||
...restProps | ||
} = props; | ||
|
||
const { t } = useTranslation(); | ||
|
||
const { | ||
data: options, | ||
isLoading, | ||
error, | ||
} = useQuery({ | ||
queryKey: [ClientSideActionsSelect.name, resourceType], | ||
queryFn: async () => { | ||
return await loadAllResources(fhirBaseUrl, resourceType, extraQueryParams); | ||
}, | ||
refetchOnWindowFocus: false, | ||
select: (bundle) => { | ||
const options = getResourcesFromBundle<ResourceT>(bundle).map((resource) => | ||
transformOption(resource) | ||
); | ||
return options as SelectOption<ResourceT>[]; | ||
}, | ||
}); | ||
|
||
const changeHandler = ( | ||
value: string, | ||
fullOption: SelectOption<ResourceT> | SelectOption<ResourceT>[] | ||
) => { | ||
const saneFullOption = Array.isArray(fullOption) ? fullOption.slice() : fullOption; | ||
props.onChange?.(value, saneFullOption); | ||
getFullOptionOnChange?.(saneFullOption); | ||
}; | ||
|
||
const propsToSelect = { | ||
className: 'asyncSelect', | ||
filterOption: defaultSelectFilterFunction, | ||
...restProps, | ||
onChange: changeHandler, | ||
loading: isLoading, | ||
notFoundContent: isLoading ? <Spin size="small" /> : <Empty description={t('No data')} />, | ||
options, | ||
dropdownRender: (menu: React.ReactNode) => ( | ||
<> | ||
{!error && options?.length && menu} | ||
<Divider style={{ margin: '8px 0' }} /> | ||
{error && <Alert message={t('Unable to load dropdown options.')} type="error" showIcon />} | ||
</> | ||
), | ||
}; | ||
|
||
return <Select {...propsToSelect}></Select>; | ||
} |
Oops, something went wrong.