1
- import React , { useMemo } from 'react' ;
2
- import { Form , Button , Input , DatePicker , Space , Select } from 'antd' ;
1
+ import React from 'react' ;
2
+ import { Form , Button , Input , DatePicker , Space } from 'antd' ;
3
3
import { SelectProps } from 'antd/lib/select' ;
4
4
import { AsyncSelectProps , formItemLayout , tailLayout } from '@opensrp/react-utils' ;
5
5
import { useTranslation } from '../../mls' ;
6
6
import { useQueryClient , useMutation } from 'react-query' ;
7
7
import { Dictionary } from '@onaio/utils' ;
8
+ import { supplyMgSnomedCode , snomedCodeSystem } from '../../helpers/utils' ;
8
9
import {
9
10
sendSuccessNotification ,
10
11
sendErrorNotification ,
@@ -21,20 +22,20 @@ import {
21
22
donor ,
22
23
PONumber ,
23
24
groupResourceType ,
25
+ valuesetResourceType ,
26
+ UNICEF_SECTION_ENDPOINT ,
24
27
} from '../../constants' ;
25
28
import { groupSelectfilterFunction , SelectOption } from '../ProductForm/utils' ;
26
- import { IGroup } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IGroup' ;
27
29
import { FHIRServiceClass , AsyncSelect } from '@opensrp/react-utils' ;
28
30
import { IValueSet } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IValueSet' ;
29
- import { useQuery } from 'react-query ' ;
30
- import { getValuesetSelectOptions } from './utils ' ;
31
+ import { getValuesetSelectOptions , projectOptions } from './utils ' ;
32
+ import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle ' ;
31
33
32
34
const { Item : FormItem } = Form ;
33
35
34
36
export interface LocationInventoryFormProps {
35
37
fhirBaseURL : string ;
36
38
initialValues : Dictionary ;
37
- products ?: IGroup [ ] ;
38
39
disabled : string [ ] ;
39
40
cancelUrl ?: string ;
40
41
successUrl ?: string ;
@@ -45,29 +46,21 @@ const defaultProps = {
45
46
disabled : [ ] ,
46
47
} ;
47
48
49
+ const productQueryFilters = {
50
+ code : `${ snomedCodeSystem } |${ supplyMgSnomedCode } ` ,
51
+ } ;
52
+
48
53
/**
49
54
* Add location inventory form
50
55
*
51
56
* @param props - LocationInventoryFormProps component props
52
57
* @returns returns form to add location inventories
53
58
*/
54
59
const AddLocationInventoryForm = ( props : LocationInventoryFormProps ) => {
55
- const { fhirBaseURL, initialValues, products } = props ;
60
+ const { fhirBaseURL, initialValues } = props ;
56
61
const { t } = useTranslation ( ) ;
57
62
const queryClient = useQueryClient ( ) ;
58
63
59
- const projectOptions = useMemo (
60
- ( ) => products ?. map ( ( prod : IGroup ) => ( { value : prod . id , label : prod . name } ) ) ,
61
- [ products ]
62
- ) ;
63
-
64
- const valuesetResourceType = 'ValueSet' ;
65
- const resourceId = '2826/$expand' ;
66
- const groupQuery = ( ) =>
67
- useQuery ( [ valuesetResourceType , resourceId ] , async ( ) =>
68
- new FHIRServiceClass < IValueSet > ( fhirBaseURL , valuesetResourceType ) . read ( resourceId as string )
69
- ) ;
70
-
71
64
const { mutate, isLoading } = useMutation (
72
65
( values : Dictionary ) => {
73
66
return postLocationInventory ( fhirBaseURL , values ) ;
@@ -85,17 +78,59 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
85
78
}
86
79
) ;
87
80
88
- const unicefSectionProps : AsyncSelectProps = {
81
+ const unicefSectionProps : AsyncSelectProps < IValueSet > = {
89
82
id : 'unicefSection' ,
90
83
name : unicefSection ,
91
84
label : t ( 'UNICEF section' ) ,
92
- dataLoader : groupQuery as any ,
93
- optionsGetter : getValuesetSelectOptions as any ,
85
+ optionsGetter : getValuesetSelectOptions ,
86
+ selectProps : {
87
+ placeholder : t ( 'Select section' ) ,
88
+ showSearch : true ,
89
+ filterOption : groupSelectfilterFunction as SelectProps < SelectOption [ ] > [ 'filterOption' ] ,
90
+ } ,
91
+ useQueryParams : {
92
+ key : [ valuesetResourceType , UNICEF_SECTION_ENDPOINT ] ,
93
+ queryFn : async ( ) =>
94
+ new FHIRServiceClass < IValueSet > ( fhirBaseURL , valuesetResourceType ) . read (
95
+ UNICEF_SECTION_ENDPOINT as string
96
+ ) ,
97
+ } ,
98
+ } ;
99
+
100
+ const donorSelectProps : AsyncSelectProps < IValueSet > = {
101
+ id : 'donor' ,
102
+ name : donor ,
103
+ label : t ( 'Donor' ) ,
104
+ optionsGetter : getValuesetSelectOptions ,
105
+ selectProps : {
106
+ placeholder : t ( 'Select donor' ) ,
107
+ showSearch : true ,
108
+ filterOption : groupSelectfilterFunction as SelectProps < SelectOption [ ] > [ 'filterOption' ] ,
109
+ } ,
110
+ useQueryParams : {
111
+ key : [ valuesetResourceType , UNICEF_SECTION_ENDPOINT ] ,
112
+ queryFn : async ( ) =>
113
+ new FHIRServiceClass < IValueSet > ( fhirBaseURL , valuesetResourceType ) . read (
114
+ UNICEF_SECTION_ENDPOINT as string
115
+ ) ,
116
+ } ,
117
+ } ;
118
+
119
+ const projectsSelectProps : AsyncSelectProps < IBundle > = {
120
+ id : 'project' ,
121
+ name : productName ,
122
+ label : t ( 'Product name' ) ,
123
+ optionsGetter : projectOptions ,
94
124
selectProps : {
95
125
placeholder : t ( 'Select product' ) ,
96
126
showSearch : true ,
97
127
filterOption : groupSelectfilterFunction as SelectProps < SelectOption [ ] > [ 'filterOption' ] ,
98
128
} ,
129
+ useQueryParams : {
130
+ key : [ groupResourceType ] ,
131
+ queryFn : async ( ) =>
132
+ new FHIRServiceClass < IBundle > ( fhirBaseURL , groupResourceType ) . list ( productQueryFilters ) ,
133
+ } ,
99
134
} ;
100
135
101
136
return (
@@ -107,14 +142,16 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
107
142
} }
108
143
initialValues = { initialValues }
109
144
>
110
- < FormItem id = "productName" name = { productName } label = { t ( 'Product name' ) } >
145
+ { /* <FormItem id="productName" name={productName} label={t('Product name')}>
111
146
<Select
112
147
placeholder={t('Select product')}
113
148
options={projectOptions}
114
149
showSearch={true}
115
150
filterOption={groupSelectfilterFunction as SelectProps<SelectOption[]>['filterOption']}
116
151
/>
117
- </ FormItem >
152
+ </FormItem> */ }
153
+
154
+ < AsyncSelect { ...projectsSelectProps } />
118
155
119
156
< FormItem id = "quantity" name = { quantity } label = { t ( 'Quantity (Optional)' ) } >
120
157
< Input required = { false } type = "number" />
@@ -142,9 +179,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
142
179
< Input type = "number" />
143
180
</ FormItem >
144
181
145
- < FormItem id = "donor" name = { donor } label = { t ( 'Donor' ) } >
146
- < Input />
147
- </ FormItem >
182
+ < AsyncSelect { ...donorSelectProps } />
148
183
149
184
< FormItem id = "poNumber" name = { PONumber } label = { t ( 'PO number' ) } >
150
185
< Input type = "number" />
0 commit comments