|
| 1 | +import React from 'react'; |
| 2 | +import { Helmet } from 'react-helmet'; |
| 3 | +import { CommodityForm } from '../../ProductForm'; |
| 4 | +import { useParams } from 'react-router'; |
| 5 | +import { |
| 6 | + accountabilityPeriod, |
| 7 | + appropriateUsage, |
| 8 | + availability, |
| 9 | + condition, |
| 10 | + groupResourceType, |
| 11 | + isAttractiveItem, |
| 12 | + LIST_COMMODITY_URL, |
| 13 | + materialNumber, |
| 14 | + productImage, |
| 15 | +} from '../../../constants'; |
| 16 | +import { Spin } from 'antd'; |
| 17 | +import { PageHeader } from '@opensrp/react-utils'; |
| 18 | +import { useQuery } from 'react-query'; |
| 19 | +import { FHIRServiceClass, BrokenPage } from '@opensrp/react-utils'; |
| 20 | +import { IGroup } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IGroup'; |
| 21 | +import { |
| 22 | + generateGroupPayload, |
| 23 | + getGroupFormFields, |
| 24 | + postPutGroup, |
| 25 | + updateListReferencesFactory, |
| 26 | + validationRulesFactory, |
| 27 | +} from './utils'; |
| 28 | +import { useTranslation } from '../../../mls'; |
| 29 | + |
| 30 | +export interface GroupAddEditProps { |
| 31 | + fhirBaseURL: string; |
| 32 | + listId: string; |
| 33 | +} |
| 34 | + |
| 35 | +export interface RouteParams { |
| 36 | + id?: string; |
| 37 | +} |
| 38 | + |
| 39 | +export const CommodityAddEdit = (props: GroupAddEditProps) => { |
| 40 | + const { fhirBaseURL: fhirBaseUrl, listId } = props; |
| 41 | + |
| 42 | + const { id: resourceId } = useParams<RouteParams>(); |
| 43 | + const { t } = useTranslation(); |
| 44 | + |
| 45 | + const groupQuery = useQuery( |
| 46 | + [groupResourceType, resourceId], |
| 47 | + async () => |
| 48 | + new FHIRServiceClass<IGroup>(fhirBaseUrl, groupResourceType).read(resourceId as string), |
| 49 | + { |
| 50 | + enabled: !!resourceId, |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + if (!groupQuery.isIdle && groupQuery.isLoading) { |
| 55 | + return <Spin size="large" className="custom-spinner"></Spin>; |
| 56 | + } |
| 57 | + |
| 58 | + if (groupQuery.error && !groupQuery.data) { |
| 59 | + return <BrokenPage errorMessage={(groupQuery.error as Error).message} />; |
| 60 | + } |
| 61 | + |
| 62 | + const initialValues = getGroupFormFields(groupQuery.data); |
| 63 | + |
| 64 | + const pageTitle = groupQuery.data |
| 65 | + ? t('Edit Commodity | {{name}}', { name: groupQuery.data.name ?? '' }) |
| 66 | + : t('Create Commodity'); |
| 67 | + |
| 68 | + const postSuccess = updateListReferencesFactory(fhirBaseUrl, listId); |
| 69 | + |
| 70 | + return ( |
| 71 | + <section className="content-section"> |
| 72 | + <Helmet> |
| 73 | + <title>{pageTitle}</title> |
| 74 | + </Helmet> |
| 75 | + <PageHeader title={pageTitle} /> |
| 76 | + <div className="bg-white p-5"> |
| 77 | + <CommodityForm |
| 78 | + hidden={[ |
| 79 | + materialNumber, |
| 80 | + isAttractiveItem, |
| 81 | + availability, |
| 82 | + condition, |
| 83 | + appropriateUsage, |
| 84 | + accountabilityPeriod, |
| 85 | + productImage, |
| 86 | + ]} |
| 87 | + fhirBaseUrl={fhirBaseUrl} |
| 88 | + initialValues={initialValues} |
| 89 | + cancelUrl={LIST_COMMODITY_URL} |
| 90 | + successUrl={LIST_COMMODITY_URL} |
| 91 | + postSuccess={postSuccess} |
| 92 | + validationRulesFactory={validationRulesFactory} |
| 93 | + mutationEffect={async (initialValues, values) => { |
| 94 | + const payload = generateGroupPayload(values, initialValues); |
| 95 | + return postPutGroup(fhirBaseUrl, payload); |
| 96 | + }} |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </section> |
| 100 | + ); |
| 101 | +}; |
0 commit comments