Skip to content

Commit 5aa1997

Browse files
committed
Generate payload for location inventory resource
1 parent 011513e commit 5aa1997

File tree

4 files changed

+302
-28
lines changed

4 files changed

+302
-28
lines changed

packages/fhir-group-management/src/components/LocationInventory/form.tsx

+46-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Form, Button, Input, DatePicker, Space } from 'antd';
2+
import { Form, Button, Input, DatePicker, Space, Switch } from 'antd';
33
import { SelectProps } from 'antd/lib/select';
44
import { AsyncSelectProps, formItemLayout, tailLayout } from '@opensrp/react-utils';
55
import { useTranslation } from '../../mls';
@@ -12,7 +12,7 @@ import {
1212
sendInfoNotification,
1313
} from '@opensrp/notifications';
1414
import {
15-
productName,
15+
product,
1616
quantity,
1717
deliveryDate,
1818
accountabilityEndDate,
@@ -24,18 +24,31 @@ import {
2424
groupResourceType,
2525
valuesetResourceType,
2626
UNICEF_SECTION_ENDPOINT,
27+
id,
28+
identifier,
29+
active,
30+
name,
31+
type,
32+
actual,
2733
} from '../../constants';
2834
import { groupSelectfilterFunction, SelectOption } from '../ProductForm/utils';
2935
import { FHIRServiceClass, AsyncSelect } from '@opensrp/react-utils';
3036
import { IValueSet } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IValueSet';
31-
import { getValuesetSelectOptions, projectOptions } from './utils';
37+
import {
38+
getLocationInventoryPayload,
39+
getValuesetSelectOptions,
40+
handleDisabledFutureDates,
41+
handleDisabledPastDates,
42+
projectOptions,
43+
} from './utils';
3244
import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle';
45+
import { GroupFormFields } from './types';
3346

3447
const { Item: FormItem } = Form;
3548

3649
export interface LocationInventoryFormProps {
3750
fhirBaseURL: string;
38-
initialValues: Dictionary;
51+
initialValues: GroupFormFields;
3952
disabled: string[];
4053
cancelUrl?: string;
4154
successUrl?: string;
@@ -62,7 +75,8 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
6275
const queryClient = useQueryClient();
6376

6477
const { mutate, isLoading } = useMutation(
65-
(values: Dictionary) => {
78+
(values: GroupFormFields) => {
79+
const payload = getLocationInventoryPayload(values);
6680
return postLocationInventory(fhirBaseURL, values);
6781
},
6882
{
@@ -84,7 +98,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
8498
label: t('UNICEF section'),
8599
optionsGetter: getValuesetSelectOptions,
86100
selectProps: {
87-
placeholder: t('Select section'),
101+
placeholder: t('Select UNICEF section'),
88102
showSearch: true,
89103
filterOption: groupSelectfilterFunction as SelectProps<SelectOption[]>['filterOption'],
90104
},
@@ -118,7 +132,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
118132

119133
const projectsSelectProps: AsyncSelectProps<IBundle> = {
120134
id: 'project',
121-
name: productName,
135+
name: product,
122136
label: t('Product name'),
123137
optionsGetter: projectOptions,
124138
selectProps: {
@@ -137,40 +151,31 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
137151
<Form
138152
requiredMark={false}
139153
{...formItemLayout}
140-
onFinish={(values: Dictionary) => {
154+
onFinish={(values: GroupFormFields) => {
141155
mutate(values);
142156
}}
143157
initialValues={initialValues}
144158
>
145-
{/* <FormItem id="productName" name={productName} label={t('Product name')}>
146-
<Select
147-
placeholder={t('Select product')}
148-
options={projectOptions}
149-
showSearch={true}
150-
filterOption={groupSelectfilterFunction as SelectProps<SelectOption[]>['filterOption']}
151-
/>
152-
</FormItem> */}
153-
154159
<AsyncSelect {...projectsSelectProps} />
155160

156161
<FormItem id="quantity" name={quantity} label={t('Quantity (Optional)')}>
157162
<Input required={false} type="number" />
158163
</FormItem>
159164

160165
<FormItem id="deliveryDate" name={deliveryDate} label={t('Delivery date')}>
161-
<DatePicker />
166+
<DatePicker disabledDate={handleDisabledFutureDates} />
162167
</FormItem>
163168

164169
<FormItem
165170
id="accounterbilityEndDate"
166171
name={accountabilityEndDate}
167172
label={t('Accountability end date')}
168173
>
169-
<DatePicker />
174+
<DatePicker disabledDate={handleDisabledPastDates} />
170175
</FormItem>
171176

172177
<FormItem id="expiryDate" name={expiryDate} label={t('Expiry date (Optional)')}>
173-
<DatePicker />
178+
<DatePicker disabledDate={handleDisabledPastDates} />
174179
</FormItem>
175180

176181
<AsyncSelect {...unicefSectionProps} />
@@ -185,6 +190,27 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
185190
<Input type="number" />
186191
</FormItem>
187192

193+
{/* start hidden fields */}
194+
<FormItem hidden={true} id="id" name={id} label={t('Commodity Id')}>
195+
<Input disabled={true} />
196+
</FormItem>
197+
<FormItem hidden={true} id="identifier" name={identifier} label={t('Identifier')}>
198+
<Input disabled={true} />
199+
</FormItem>
200+
<FormItem hidden={true} id="active" name={active} label={t('Active')}>
201+
<Switch checked={initialValues.active} disabled={true} />
202+
</FormItem>
203+
<FormItem hidden={true} id="actual" name={actual} label={t('Actual')}>
204+
<Switch checked={initialValues.actual} disabled={true} />
205+
</FormItem>
206+
<FormItem hidden={true} id="name" name={name} label={t('Name')}>
207+
<Input disabled={true} />
208+
</FormItem>
209+
<FormItem hidden={true} id="type" name={type} label={t('Type')}>
210+
<Input disabled={true} />
211+
</FormItem>
212+
{/* End hidden fields */}
213+
188214
<FormItem {...tailLayout}>
189215
<Space>
190216
{/* todo: might remove cancel btn */}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IGroup } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IGroup';
2+
import {
3+
product,
4+
quantity,
5+
deliveryDate,
6+
accountabilityEndDate,
7+
expiryDate,
8+
unicefSection,
9+
serialNumber,
10+
donor,
11+
PONumber,
12+
id,
13+
identifier,
14+
active,
15+
name,
16+
type,
17+
actual,
18+
} from '../../constants';
19+
import { Group } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/group';
20+
21+
export interface CommonGroupFormFields {
22+
[id]?: string;
23+
[identifier]?: string;
24+
[active]?: boolean;
25+
[actual]?: boolean;
26+
[name]?: string;
27+
[type]?: Group.TypeEnum;
28+
[quantity]?: number;
29+
[deliveryDate]: Date;
30+
[accountabilityEndDate]: Date;
31+
[expiryDate]: Date;
32+
[serialNumber]: number;
33+
[PONumber]: number;
34+
[donor]: string;
35+
[unicefSection]: string;
36+
[product]: string;
37+
}
38+
39+
export interface GroupFormFields<InitialObjects = IGroup> extends CommonGroupFormFields {
40+
initialObject?: InitialObjects;
41+
}

0 commit comments

Comments
 (0)