Skip to content

Commit

Permalink
fix: address feedback from org unit new form
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Nov 21, 2024
1 parent f3812fc commit 367d462
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 20 deletions.
9 changes: 5 additions & 4 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +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-11-13T12:57:12.003Z\n"
"PO-Revision-Date: 2024-11-13T12:57:12.004Z\n"
"POT-Creation-Date: 2024-11-13T08:29:01.348Z\n"
"PO-Revision-Date: 2024-11-13T08:29:01.348Z\n"
"POT-Creation-Date: 2024-11-21T09:51:36.932Z\n"
"PO-Revision-Date: 2024-11-21T09:51:36.933Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -1042,6 +1040,9 @@ msgstr ""
"Choose when, and for which organisation units this category option will be "
"available."

msgid "Form name should not exceed 230 characters"
msgstr "Form name should not exceed 230 characters"

msgid "End date should be after start date"
msgstr "End date should be after start date"

Expand Down
2 changes: 1 addition & 1 deletion src/components/form/fields/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function DateField({
validationText={meta.touched ? meta.error : undefined}
onBlur={(_, e) => input.onBlur(e)}
clearable
label={required ? `${label} *` : label}
label={required ? `${label} (required) *` : label}
{...calendarInputProps}
/>
</div>
Expand Down
9 changes: 7 additions & 2 deletions src/lib/form/modelFormSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod'
import i18n from '@dhis2/d2-i18n'

Check failure on line 2 in src/lib/form/modelFormSchemas.ts

View workflow job for this annotation

GitHub Actions / lint

`@dhis2/d2-i18n` import should occur before import of `zod`

/* Note that these schemas describes validations for what we send to the server,
/* Note that these schemas describes validations for what we send to the server,
and not what is stored in the form. Unknown keys are stripped by default. */

const modelReference = z.object({ id: z.string() })
Expand All @@ -15,7 +16,11 @@ const identifiable = z.object({
const attributeValues = z
.array(
z.object({
value: z.string(),
value: z.string().max(230, {
message: i18n.t('Should not exceed {{maxLength}} characters', {
maxLength: 230,
}),
}),
attribute: z.object({
id: z.string(),
}),
Expand Down
1 change: 0 additions & 1 deletion src/pages/organisationUnits/New.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from './form'

const section = SECTIONS_MAP.organisationUnit

export const Component = () => {
return (
<FormBase
Expand Down
6 changes: 4 additions & 2 deletions src/pages/organisationUnits/form/GeometryFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ export function GeometryFields() {
type: 'Point',
coordinates: [longitude || undefined, latitude || undefined],
}

input.onChange(geometry)
input.onBlur()
}

return !input.value || input.value?.type === 'Point' ? (
<>
<Field
name={fieldName}
error={meta.touched && !!meta.error}
validationText={meta.touched ? meta.error?.type : undefined}
validationText={
meta.touched ? meta.error?.coordinates : undefined
}
>
<InputField
onChange={(e) =>
Expand Down
8 changes: 7 additions & 1 deletion src/pages/organisationUnits/form/ImageField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ export function ImageField() {
}

return (
<UIField label={i18n.t('Image')} name="image">
<UIField
label={i18n.t('Image')}
name="image"
error={!!(input.value && input.value.error)}
valid={!!(input.value && input.value.id)}
validationText={input.value?.error}
>
<div className={css.fileInputWrapper}>
<ImagePreview
fileBase64={fileBase64}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import i18n from '@dhis2/d2-i18n'
import { InputFieldFF } from '@dhis2/ui'
import { Field, InputFieldFF } from '@dhis2/ui'
import React from 'react'
import { Field as FieldRFF } from 'react-final-form'
import {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function OrganisationUnitFormField() {

<StandardFormSectionDescription>
{i18n.t(
'Set up the basic information for this organisation unit.'
'Set up the basic information for this organisation unit'
)}
</StandardFormSectionDescription>
<DefaultIdentifiableFields />
Expand Down
66 changes: 59 additions & 7 deletions src/pages/organisationUnits/form/organisationUnitSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import { z } from 'zod'
import { getDefaults, modelFormSchemas } from '../../../lib'

Expand All @@ -9,20 +10,71 @@ export const organisationUnitSchema = identifiable
.extend({
shortName: z.string().trim().default(''),
code: z.string().trim().optional(),
description: z.string().trim().optional(),
description: z
.string()
.trim()
.max(500, {
message: i18n.t('Should not exceed {{maxLength}} characters', {
maxLength: 500,
}),
})
.optional(),
image: z.object({ id: z.string() }).optional(),
phoneNumber: z.string().optional(),
contactPerson: z.string().optional(),
phoneNumber: z
.string()
.min(10, { message: i18n.t('Must be a valid mobile number') })
.max(14, { message: i18n.t('Must be a valid mobile number') })
.optional(),
contactPerson: z
.string()
.max(230, {
message: i18n.t('Should not exceed {{maxLength}} characters', {
maxLength: 230,
}),
})
.optional(),
openingDate: z.string(),
email: z.string().optional(),
address: z.string().optional(),
url: z.string().optional(),
email: z.string().email().optional(),
address: z
.string()
.max(230, {
message: i18n.t('Should not exceed {{maxLength}} characters', {
maxLength: 230,
}),
})
.optional(),
url: z
.string()
.url({ message: i18n.t('Must be a valid url') })
.optional(),
closedDate: z.string().optional(),
comment: z
.string()
.max(230, {
message: i18n.t('Should not exceed {{maxLength}} characters', {
maxLength: 230,
}),
})
.optional(),
parent: z.object({ id: z.string() }).optional(),
geometry: z
.object({
type: z.literal('Point'),
coordinates: z.array(z.number()).length(2),
coordinates: z
.array(z.number())
.length(2)
.refine(
(coord) =>
coord[0] >= -90 &&
coord[0] <= 90 &&
coord[1] >= -180 &&
coord[1] <= 180,
{
message: i18n.t(
'Longitude should be between -90 and 90. Latitude should be between -180 and 180'
),
}
),
})
.or(
z.object({
Expand Down

0 comments on commit 367d462

Please sign in to comment.