Skip to content

Commit

Permalink
Generate payload for location inventory resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ciremusyoka committed Mar 18, 2024
1 parent 011513e commit 5aa1997
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Form, Button, Input, DatePicker, Space } from 'antd';
import { Form, Button, Input, DatePicker, Space, Switch } from 'antd';
import { SelectProps } from 'antd/lib/select';
import { AsyncSelectProps, formItemLayout, tailLayout } from '@opensrp/react-utils';
import { useTranslation } from '../../mls';
Expand All @@ -12,7 +12,7 @@ import {
sendInfoNotification,
} from '@opensrp/notifications';
import {
productName,
product,
quantity,
deliveryDate,
accountabilityEndDate,
Expand All @@ -24,18 +24,31 @@ import {
groupResourceType,
valuesetResourceType,
UNICEF_SECTION_ENDPOINT,
id,
identifier,
active,
name,
type,
actual,
} from '../../constants';
import { groupSelectfilterFunction, SelectOption } from '../ProductForm/utils';
import { FHIRServiceClass, AsyncSelect } from '@opensrp/react-utils';
import { IValueSet } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IValueSet';
import { getValuesetSelectOptions, projectOptions } from './utils';
import {
getLocationInventoryPayload,
getValuesetSelectOptions,
handleDisabledFutureDates,
handleDisabledPastDates,
projectOptions,
} from './utils';
import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle';
import { GroupFormFields } from './types';

const { Item: FormItem } = Form;

export interface LocationInventoryFormProps {
fhirBaseURL: string;
initialValues: Dictionary;
initialValues: GroupFormFields;
disabled: string[];
cancelUrl?: string;
successUrl?: string;
Expand All @@ -62,7 +75,8 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
const queryClient = useQueryClient();

const { mutate, isLoading } = useMutation(
(values: Dictionary) => {
(values: GroupFormFields) => {
const payload = getLocationInventoryPayload(values);

Check failure on line 79 in packages/fhir-group-management/src/components/LocationInventory/form.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

'payload' is assigned a value but never used. Allowed unused vars must match /^_/u
return postLocationInventory(fhirBaseURL, values);
},
{
Expand All @@ -84,7 +98,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
label: t('UNICEF section'),
optionsGetter: getValuesetSelectOptions,
selectProps: {
placeholder: t('Select section'),
placeholder: t('Select UNICEF section'),
showSearch: true,
filterOption: groupSelectfilterFunction as SelectProps<SelectOption[]>['filterOption'],
},
Expand Down Expand Up @@ -118,7 +132,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {

const projectsSelectProps: AsyncSelectProps<IBundle> = {
id: 'project',
name: productName,
name: product,
label: t('Product name'),
optionsGetter: projectOptions,
selectProps: {
Expand All @@ -137,40 +151,31 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
<Form
requiredMark={false}
{...formItemLayout}
onFinish={(values: Dictionary) => {
onFinish={(values: GroupFormFields) => {
mutate(values);
}}
initialValues={initialValues}
>
{/* <FormItem id="productName" name={productName} label={t('Product name')}>
<Select
placeholder={t('Select product')}
options={projectOptions}
showSearch={true}
filterOption={groupSelectfilterFunction as SelectProps<SelectOption[]>['filterOption']}
/>
</FormItem> */}

<AsyncSelect {...projectsSelectProps} />

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

<FormItem id="deliveryDate" name={deliveryDate} label={t('Delivery date')}>
<DatePicker />
<DatePicker disabledDate={handleDisabledFutureDates} />
</FormItem>

<FormItem
id="accounterbilityEndDate"
name={accountabilityEndDate}
label={t('Accountability end date')}
>
<DatePicker />
<DatePicker disabledDate={handleDisabledPastDates} />
</FormItem>

<FormItem id="expiryDate" name={expiryDate} label={t('Expiry date (Optional)')}>
<DatePicker />
<DatePicker disabledDate={handleDisabledPastDates} />
</FormItem>

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

{/* start hidden fields */}
<FormItem hidden={true} id="id" name={id} label={t('Commodity Id')}>
<Input disabled={true} />
</FormItem>
<FormItem hidden={true} id="identifier" name={identifier} label={t('Identifier')}>
<Input disabled={true} />
</FormItem>
<FormItem hidden={true} id="active" name={active} label={t('Active')}>
<Switch checked={initialValues.active} disabled={true} />
</FormItem>
<FormItem hidden={true} id="actual" name={actual} label={t('Actual')}>
<Switch checked={initialValues.actual} disabled={true} />
</FormItem>
<FormItem hidden={true} id="name" name={name} label={t('Name')}>
<Input disabled={true} />
</FormItem>
<FormItem hidden={true} id="type" name={type} label={t('Type')}>
<Input disabled={true} />
</FormItem>
{/* End hidden fields */}

<FormItem {...tailLayout}>
<Space>
{/* todo: might remove cancel btn */}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { IGroup } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IGroup';
import {
product,
quantity,
deliveryDate,
accountabilityEndDate,
expiryDate,
unicefSection,
serialNumber,
donor,
PONumber,
id,
identifier,
active,
name,
type,
actual,
} from '../../constants';
import { Group } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/group';

export interface CommonGroupFormFields {
[id]?: string;
[identifier]?: string;
[active]?: boolean;
[actual]?: boolean;
[name]?: string;
[type]?: Group.TypeEnum;
[quantity]?: number;
[deliveryDate]: Date;
[accountabilityEndDate]: Date;
[expiryDate]: Date;
[serialNumber]: number;
[PONumber]: number;
[donor]: string;
[unicefSection]: string;
[product]: string;
}

export interface GroupFormFields<InitialObjects = IGroup> extends CommonGroupFormFields {
initialObject?: InitialObjects;
}
Loading

0 comments on commit 5aa1997

Please sign in to comment.