-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
214 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react' | ||
import { useQuery } from 'react-query' | ||
import { useParams } from 'react-router-dom' | ||
import { DefaultEditFormContents, FormBase } from '../../components' | ||
import { | ||
ATTRIBUTE_VALUES_FIELD_FILTERS, | ||
DEFAULT_FIELD_FILTERS, | ||
SECTIONS_MAP, | ||
useOnSubmitEdit, | ||
validate, | ||
} from '../../lib' | ||
import { useBoundResourceQueryFn } from '../../lib/query/useBoundQueryFn' | ||
import { OrganisationUnit, PickWithFieldFilters } from '../../types/generated' | ||
import { | ||
FormValues, | ||
OrganisationUnitFormField, | ||
organisationUnitSchema, | ||
} from './form' | ||
|
||
const fieldFilters = [ | ||
...DEFAULT_FIELD_FILTERS, | ||
...ATTRIBUTE_VALUES_FIELD_FILTERS, | ||
'name', | ||
'code', | ||
'shortName', | ||
'openingDate', | ||
'closedDate', | ||
'comment', | ||
'image', | ||
'description', | ||
'contactPerson', | ||
'address', | ||
'email', | ||
'phoneNumber', | ||
'url', | ||
'geometry', | ||
'dataSets', | ||
'programs', | ||
'level', | ||
'path', | ||
'parent[id,path, displayName]', | ||
] as const | ||
|
||
export type OrgUnitFormValues = PickWithFieldFilters< | ||
OrganisationUnit, | ||
typeof fieldFilters | ||
> | ||
|
||
const section = SECTIONS_MAP.organisationUnit | ||
|
||
export const Component = () => { | ||
const queryFn = useBoundResourceQueryFn() | ||
const modelId = useParams().id as string | ||
|
||
const query = { | ||
resource: 'organisationUnits', | ||
id: modelId, | ||
params: { | ||
fields: fieldFilters.concat(), | ||
}, | ||
} | ||
const orgUnit = useQuery({ | ||
queryKey: [query], | ||
queryFn: queryFn<OrgUnitFormValues>, | ||
}) | ||
|
||
return ( | ||
<FormBase | ||
onSubmit={useOnSubmitEdit({ | ||
section, | ||
modelId, | ||
})} | ||
section={section} | ||
initialValues={orgUnit.data as FormValues} | ||
validate={(values: FormValues) => { | ||
return validate(organisationUnitSchema, values) | ||
}} | ||
> | ||
<DefaultEditFormContents section={section}> | ||
<OrganisationUnitFormField /> | ||
</DefaultEditFormContents> | ||
</FormBase> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { Field, InputField } from '@dhis2/ui' | ||
import React from 'react' | ||
import { useField } from 'react-final-form' | ||
|
||
export function GeometryFields() { | ||
const fieldName = 'geometry' | ||
const { input, meta } = useField(fieldName) | ||
|
||
const handleChange = ({ | ||
longitude, | ||
latitude, | ||
}: { | ||
longitude?: string | ||
latitude?: string | ||
}) => { | ||
const geometry = { | ||
type: 'Point', | ||
coordinates: [longitude || undefined, latitude || undefined], | ||
} | ||
|
||
input.onChange(geometry) | ||
} | ||
|
||
return !input.value || input.value?.type === 'Point' ? ( | ||
<> | ||
<Field | ||
name={fieldName} | ||
error={meta.touched && meta.error} | ||
validationText={meta.touched ? meta.error : undefined} | ||
> | ||
<InputField | ||
onChange={(e) => | ||
handleChange({ | ||
longitude: e.value, | ||
latitude: input.value.coordinates?.[1], | ||
}) | ||
} | ||
label={i18n.t('Longitude')} | ||
inputWidth="400px" | ||
name="longitude" | ||
type="number" | ||
value={input.value.coordinates?.[0].toString()} | ||
min="-90" | ||
max="90" | ||
step="any" | ||
/> | ||
<InputField | ||
onChange={(e) => | ||
handleChange({ | ||
longitude: input.value.coordinates?.[0], | ||
latitude: e.value, | ||
}) | ||
} | ||
inputWidth="400px" | ||
label={i18n.t('Longitude')} | ||
name="latitude" | ||
type="number" | ||
value={input.value.coordinates?.[1].toString()} | ||
min="-180" | ||
max="180" | ||
step="any" | ||
/> | ||
</Field> | ||
</> | ||
) : ( | ||
<InputField | ||
value={i18n.t('{{type}} coordinates are not editable', { | ||
type: input.value?.type, | ||
})} | ||
inputWidth="400px" | ||
disabled | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.