Skip to content

Commit

Permalink
feat: add org unit edit form (#430)
Browse files Browse the repository at this point in the history
* feat: add org unit edit form

* feat: small changes to org unit select and image select

* fix(orgunit): fix image preview url

* fix(orgunit): create default valueFormatter

* fix: fix type issue

* feat: minor fixes

---------

Co-authored-by: Birk Johansson <[email protected]>
  • Loading branch information
flaminic and Birkbjo authored Nov 5, 2024
1 parent 4eccf6d commit 79a1892
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 91 deletions.
95 changes: 61 additions & 34 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-10-23T11:07:53.225Z\n"
"PO-Revision-Date: 2024-10-23T11:07:53.225Z\n"
"POT-Creation-Date: 2024-11-05T07:23:15.731Z\n"
"PO-Revision-Date: 2024-11-05T07:23:15.732Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -123,21 +123,6 @@ msgstr "Failed to load {{label}}"
msgid "Failed to load"
msgstr "Failed to load"

msgid "Download"
msgstr "Download"

msgid "Merge"
msgstr "Merge"

msgid "Delete source data element values"
msgstr "Delete source data element values"

msgid "Last updated"
msgstr "Last updated"

msgid "Discard"
msgstr "Discard"

msgid "Aggregation level(s)"
msgstr "Aggregation level(s)"

Expand Down Expand Up @@ -237,6 +222,9 @@ msgstr "Created"
msgid "Last updated by"
msgstr "Last updated by"

msgid "Last updated"
msgstr "Last updated"

msgid "Id"
msgstr "Id"

Expand All @@ -258,6 +246,9 @@ msgstr "Details"
msgid "Failed to load details"
msgstr "Failed to load details"

msgid "Download"
msgstr "Download"

msgid "Download {{section}}"
msgstr "Download {{section}}"

Expand Down Expand Up @@ -297,6 +288,9 @@ msgstr "Clear all filters"
msgid "Category"
msgstr "Category"

msgid "Category option"
msgstr "Category option"

msgid "Category option group"
msgstr "Category option group"

Expand Down Expand Up @@ -435,9 +429,6 @@ msgstr "Search for a user or group"
msgid "Categories"
msgstr "Categories"

msgid "Category option"
msgstr "Category option"

msgid "Category options"
msgstr "Category options"

Expand Down Expand Up @@ -828,6 +819,27 @@ msgstr "GeoJSON"
msgid "Disaggregation"
msgstr "Disaggregation"

msgid "Point"
msgstr "Point"

msgid "MultiPoint"
msgstr "MultiPoint"

msgid "LineString"
msgstr "LineString"

msgid "MultiLineString"
msgstr "MultiLineString"

msgid "Polygon"
msgstr "Polygon"

msgid "MultiPolygon"
msgstr "MultiPolygon"

msgid "GeometryCollection"
msgstr "GeometryCollection"

msgid "Aggregation type"
msgstr "Aggregation type"

Expand Down Expand Up @@ -855,6 +867,9 @@ msgstr "Zero is significant"
msgid "Data dimension type"
msgstr "Data dimension type"

msgid "Ignore data approval"
msgstr "Ignore data approval"

msgid "This field requires a unique value, please choose another one"
msgstr "This field requires a unique value, please choose another one"

Expand Down Expand Up @@ -964,12 +979,24 @@ msgid "Category option Groups"
msgstr "Category option Groups"

msgid ""
"Choose the category option Groups to include in this category option group "
"Choose the category option groups to include in this category option group "
"set."
msgstr ""
"Choose the category option Groups to include in this category option group "
"Choose the category option groups to include in this category option group "
"set."

msgid "Available category option groups"
msgstr "Available category option groups"

msgid "Selected category option groups"
msgstr "Selected category option groups"

msgid "Filter available category option groups"
msgstr "Filter available category option groups"

msgid "Filter selected category option groups"
msgstr "Filter selected category option groups"

msgid "Set up the basic information for this category option group."
msgstr "Set up the basic information for this category option group."

Expand Down Expand Up @@ -1127,6 +1154,12 @@ msgstr ""
"included. PHU will still be available for the PHU level, but not included "
"in the aggregations to the levels above."

msgid "Longitude"
msgstr "Longitude"

msgid "{{type}} coordinates are not editable"
msgstr "{{type}} coordinates are not editable"

msgid "Upload an image"
msgstr "Upload an image"

Expand Down Expand Up @@ -1176,17 +1209,11 @@ msgstr "Location"
msgid "Set up the organisation unit location."
msgstr "Set up the organisation unit location."

msgid "Latitude"
msgstr "Latitude"

msgid "Longitude"
msgstr "Longitude"

msgid "Reference assignments"
msgstr "Reference assignments"
msgid "Reference assignment"
msgstr "Reference assignment"

msgid "Assign the organisation unit to related models."
msgstr "Assign the organisation unit to related models."
msgid "Assign the organisation unit to related objects."
msgstr "Assign the organisation unit to related objects."

msgid "Available data sets"
msgstr "Available data sets"
Expand All @@ -1212,8 +1239,8 @@ msgstr "Filter available programs"
msgid "Filter selected programs"
msgstr "Filter selected programs"

msgid "New organisation unit will be created inside {{displayName}}"
msgstr "New organisation unit will be created inside {{displayName}}"
msgid "Organisation unit will be positioned inside {{displayName}}"
msgstr "Organisation unit will be positioned inside {{displayName}}"

msgid "Creating first organisation unit"
msgstr "Creating first organisation unit"
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/DefaultFormErrorNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const ErrorList = ({ errors }: { errors: Record<string, string> }) => {
>
{labels.get(key) || key}:
</span>
<span>{value}</span>
<span>{JSON.stringify(value)}</span>
</li>
)
})}
Expand Down
11 changes: 11 additions & 0 deletions src/lib/constants/translatedModelConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,22 @@ export const DATA_DIMENSION_TYPE = {
ATTRIBUTE: i18n.t('Attribute'),
}

export const GEOMETRY_TYPE = {
POINT: i18n.t('Point'),
MULTIPOINT: i18n.t('MultiPoint'),
LINESTRING: i18n.t('LineString'),
MULTILINESTRING: i18n.t('MultiLineString'),
POLYGON: i18n.t('Polygon'),
MULTIPOLYGON: i18n.t('MultiPolygon'),
GEOMETRYCOLLECTION: i18n.t('GeometryCollection'),
}

const allConstantTranslations: Record<string, string> = {
...AGGREGATION_TYPE,
...DOMAIN_TYPE,
...VALUE_TYPE,
...DATA_DIMENSION_TYPE,
...GEOMETRY_TYPE,
}

export const getConstantTranslation = (constant: string): string => {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/form/createJsonPatchOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type PatchAttributeValue = {
value: AttributeValue['value']
}

type ModelWithAttributeValues = IdentifiableObject & {
export type ModelWithAttributeValues = {
id?: string
attributeValues?: PatchAttributeValue[]
}

Expand Down
27 changes: 23 additions & 4 deletions src/lib/form/useOnSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { useNavigate } from 'react-router-dom'
import { ModelSection } from '../../types'
import { IdentifiableObject } from '../../types/generated'
import { getSectionPath, useNavigateWithSearchState } from '../routeUtils'
import { createJsonPatchOperations } from './createJsonPatchOperations'
import {
createJsonPatchOperations,
ModelWithAttributeValues,
} from './createJsonPatchOperations'
import { useCreateModel } from './useCreateModel'
import { usePatchModel } from './usePatchModel'

Expand Down Expand Up @@ -57,9 +60,25 @@ export const useOnSubmitEdit = <TFormValues extends IdentifiableObject>({
)
}

export const useOnSubmitNew = <TFormValues>({
export const defaultNewValueFormatter = <
TFormValues extends ModelWithAttributeValues
>(
values: TFormValues
) => {
if (values.attributeValues) {
return {
...values,
attributeValues: values.attributeValues.filter(
({ value }) => !!value
),
}
}
return values
}

export const useOnSubmitNew = <TFormValues extends ModelWithAttributeValues>({
section,
valueFormatter,
valueFormatter = defaultNewValueFormatter,
}: {
section: ModelSection
valueFormatter?: (values: TFormValues) => Record<string, unknown>
Expand Down Expand Up @@ -96,6 +115,6 @@ export const useOnSubmitNew = <TFormValues>({
})
navigate(`/${getSectionPath(section)}`)
},
[createModel, saveAlert, navigate, section]
[createModel, saveAlert, navigate, section, valueFormatter]
)
}
80 changes: 80 additions & 0 deletions src/pages/organisationUnits/Edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 { OrganisationUnitFormField, organisationUnitSchema } from './form'

const fieldFilters = [
...DEFAULT_FIELD_FILTERS,
...ATTRIBUTE_VALUES_FIELD_FILTERS,
'name',
'code',
'shortName',
'openingDate',
'closedDate',
'comment',
'image[id,name]',
'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}
validate={(values: OrgUnitFormValues) => {
return validate(organisationUnitSchema, values)
}}
>
<DefaultEditFormContents section={section}>
<OrganisationUnitFormField />
</DefaultEditFormContents>
</FormBase>
)
}
19 changes: 0 additions & 19 deletions src/pages/organisationUnits/New.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,13 @@ import {
organisationUnitSchema,
} from './form'

const formatFormValues: (values: FormValues) => Record<string, unknown> = (
values
) => {
return {
...values,
geometry:
values.geometry?.longitude && values.geometry?.latitude
? {
type: 'Point',
coordinates: [
values.geometry?.longitude,
values.geometry?.latitude,
],
}
: undefined,
attributeValues: values.attributeValues.filter(({ value }) => !!value),
}
}
const section = SECTIONS_MAP.organisationUnit

export const Component = () => {
return (
<FormBase
onSubmit={useOnSubmitNew({
section,
valueFormatter: formatFormValues,
})}
initialValues={initialValues as FormValues}
validate={(values: FormValues) => {
Expand Down
Loading

0 comments on commit 79a1892

Please sign in to comment.