From ec3a0df52c88e7557d6ec6ffc500fc8efa1b0970 Mon Sep 17 00:00:00 2001 From: kajambiya Date: Wed, 27 Sep 2023 14:28:29 +0300 Subject: [PATCH] fix build fail(UISelectExtended initialization issues) --- .../ui-select-extended/ui-select-extended.tsx | 9 +++++++- src/hooks/useDataSourceConfig.tsx | 22 ------------------- .../inbuilt-components/control-templates.ts | 18 --------------- .../inbuilt-components/inbuiltControls.ts | 20 +++++++++++++++-- src/registry/registry.ts | 3 +-- 5 files changed, 27 insertions(+), 45 deletions(-) delete mode 100644 src/hooks/useDataSourceConfig.tsx delete mode 100644 src/registry/inbuilt-components/control-templates.ts diff --git a/src/components/inputs/ui-select-extended/ui-select-extended.tsx b/src/components/inputs/ui-select-extended/ui-select-extended.tsx index d87df09bb..ee396eb46 100644 --- a/src/components/inputs/ui-select-extended/ui-select-extended.tsx +++ b/src/components/inputs/ui-select-extended/ui-select-extended.tsx @@ -13,6 +13,7 @@ import debounce from 'lodash-es/debounce'; import { useTranslation } from 'react-i18next'; import { getRegisteredDataSource } from '../../../registry/registry'; import { useDataSourceConfig } from '../../../hooks/useDataSourceConfig'; +import { getControlTemplate } from '../../../registry/inbuilt-components/inbuiltControls'; const UISelectExtended: React.FC = ({ question, handler, onChange }) => { const { t } = useTranslation(); @@ -29,11 +30,17 @@ const UISelectExtended: React.FC = ({ question, handler, onC const [inputValue, setInputValue] = useState(''); const isProcessingSelection = useRef(false); const [dataSource, setDataSource] = useState(null); + const [config, setConfig] = useState({}); - const config = useDataSourceConfig(question); + //const config = {}; //useDataSourceConfig(question); useEffect(() => { const datasourceName = question.questionOptions?.datasource?.name; + setConfig( + datasourceName + ? question.questionOptions.datasource?.config + : getControlTemplate(question.questionOptions.rendering)?.datasource?.config, + ); getRegisteredDataSource(datasourceName ? datasourceName : question.questionOptions.rendering).then(ds => setDataSource(ds), ); diff --git a/src/hooks/useDataSourceConfig.tsx b/src/hooks/useDataSourceConfig.tsx deleted file mode 100644 index d9d0c1fde..000000000 --- a/src/hooks/useDataSourceConfig.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { useEffect, useState } from 'react'; -import { getControlTemplate } from '../registry/inbuilt-components/control-templates'; -import { OHRIFormField } from '../api/types'; -import { useTranslation } from 'react-i18next'; - -export function useDataSourceConfig(field: OHRIFormField) { - const [config, setConfig] = useState({}); - const { t } = useTranslation(); - - useEffect(() => { - if (field.questionOptions.datasource?.name) { - setConfig(field.questionOptions.datasource.config); - } else { - const template = getControlTemplate(field.questionOptions.rendering); - setConfig(template.datasource.config); - } - }, [field]); - - return { - config, - }; -} diff --git a/src/registry/inbuilt-components/control-templates.ts b/src/registry/inbuilt-components/control-templates.ts deleted file mode 100644 index 2821c5909..000000000 --- a/src/registry/inbuilt-components/control-templates.ts +++ /dev/null @@ -1,18 +0,0 @@ -import UISelectExtended from '../../components/inputs/ui-select-extended/ui-select-extended'; - -export const controlTemplates = [ - { - name: 'drug', - baseControlComponent: UISelectExtended, - datasource: { - name: 'drug_datasource', - config: { - class: '8d490dfc-c2cc-11de-8d13-0010c6dffd0f', - }, - }, - }, -]; - -export const getControlTemplate = (name: string) => { - return controlTemplates.find(template => template.name === name); -}; diff --git a/src/registry/inbuilt-components/inbuiltControls.ts b/src/registry/inbuilt-components/inbuiltControls.ts index d448840fc..2ae36d429 100644 --- a/src/registry/inbuilt-components/inbuiltControls.ts +++ b/src/registry/inbuilt-components/inbuiltControls.ts @@ -16,11 +16,23 @@ import OHRIToggle from '../../components/inputs/toggle/ohri-toggle.component'; import UISelectExtended from '../../components/inputs/ui-select-extended/ui-select-extended'; import { OHRIRepeat } from '../../components/repeat/ohri-repeat.component'; import { RegistryItem } from '../registry'; -import { controlTemplates } from './control-templates'; /** * @internal */ +const controlTemplates = [ + { + name: 'drug', + baseControlComponent: UISelectExtended, + datasource: { + name: 'drug_datasource', + config: { + class: '8d490dfc-c2cc-11de-8d13-0010c6dffd0f', + }, + }, + }, +]; + export const inbuiltControls: Array>> = [ { name: 'OHRIText', @@ -111,7 +123,11 @@ export const inbuiltControls: Array ({ name: `${template.name}Control`, - component: UISelectExtended,//template.baseControlComponent, + component: template.baseControlComponent, type: template.name.toLowerCase(), })), ]; + +export const getControlTemplate = (name: string) => { + return controlTemplates.find(template => template.name === name); +}; diff --git a/src/registry/registry.ts b/src/registry/registry.ts index a3c56d619..141908119 100644 --- a/src/registry/registry.ts +++ b/src/registry/registry.ts @@ -1,11 +1,10 @@ import { DataSource, FieldValidator, OHRIFormFieldProps, PostSubmissionAction, SubmissionHandler } from '../api/types'; import { getGlobalStore } from '@openmrs/esm-framework'; import { OHRIFormsStore } from '../constants'; -import { inbuiltControls } from './inbuilt-components/inbuiltControls'; +import { getControlTemplate, inbuiltControls } from './inbuilt-components/inbuiltControls'; import { inbuiltFieldSubmissionHandlers } from './inbuilt-components/inbuiltFieldSubmissionHandlers'; import { inbuiltValidators } from './inbuilt-components/inbuiltValidators'; import { inbuiltDataSources } from './inbuilt-components/inbuiltDataSources'; -import { getControlTemplate } from './inbuilt-components/control-templates'; /** * @internal