Skip to content

Commit

Permalink
fix: minor changes to address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Oct 21, 2024
1 parent 8bd6709 commit 8d62c32
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 31 deletions.
18 changes: 6 additions & 12 deletions src/lib/models/useCheckMaxLengthFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ export function useCheckMaxLengthFromSchema(
property: string
) {
const schema = useSchema(model)
return useCheckMaxLengthFromProperty(schema.properties[property])
return checkMaxLengthFromProperty(schema.properties[property])
}

export function useCheckMaxLengthFromProperty(
export function checkMaxLengthFromProperty(
propertyDetails: SchemaFieldProperty
) {
): (value: unknown) => (string | undefined) {
const maxLength = propertyDetails.length
const checkMaxLength = useMemo(
() =>
maxLength == undefined
? () => undefined
: createMaxCharacterLength(maxLength),
[maxLength]
)

return checkMaxLength
return maxLength == undefined
? () => undefined
: createMaxCharacterLength(maxLength)
}
4 changes: 2 additions & 2 deletions src/lib/models/useFieldValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useParams } from 'react-router-dom'
import { SchemaFieldPropertyType, SchemaSection } from '../../types'
import { composeAsyncValidators, required } from '../form'
import { useSchema } from '../schemas'
import { useCheckMaxLengthFromProperty } from './useCheckMaxLengthFromSchema'
import { checkMaxLengthFromProperty } from './useCheckMaxLengthFromSchema'
import { useIsFieldValueUnique } from './useIsFieldValueUnique'

export function useValidator({
Expand All @@ -20,7 +20,7 @@ export function useValidator({
const validators = useMemo(() => [] as Validator[], [])
const params = useParams()
const modelId = params.id as string
const checkMaxLength = useCheckMaxLengthFromProperty(propertyDetails)
const checkMaxLength = checkMaxLengthFromProperty(propertyDetails)
const checkIsValueTaken = useIsFieldValueUnique({
model: schemaSection.namePlural,
field: property,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/organisationUnits/form/ImageField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function ImageField() {
const ADD_NEW_FILE_RESOURCE_MUTATION = {
resource: 'fileResources',
type: 'create',
data: (de: object) => de,
data: fileToUpload,
} as const
const fileToUploadDetails = {
name: fileToUpload.name,
Expand Down
7 changes: 3 additions & 4 deletions src/pages/organisationUnits/form/OrganisationUnitSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ export function OrganisationUnitSelector() {
<IconInfo16 />
<p>
{i18n.t(
'New organisation unit will be created inside'
) +
' ' +
selectedOrgUnit}
'New organisation unit will be created inside {{selectedOrgUnit}}',
{ selectedOrgUnit }
)}
</p>
</div>
)}
Expand Down
17 changes: 5 additions & 12 deletions src/pages/organisationUnits/form/organisationUnitSchema.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { z } from 'zod'
import { getDefaults } from '../../../lib'
import { getDefaults, modelFormSchemas } from '../../../lib'
import i18n from '@dhis2/d2-i18n'

Check failure on line 3 in src/pages/organisationUnits/form/organisationUnitSchema.ts

View workflow job for this annotation

GitHub Actions / lint

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

export const organisationUnitSchema = z.object({
const { withAttributeValues } = modelFormSchemas

export const organisationUnitSchema = withAttributeValues.extend({
name: z.string().trim().default(''),
shortName: z.string().trim().default(''),
code: z.string().trim().optional(),
description: z.string().trim().optional(),
image: z.object({ id: z.string() }).optional(),
attributeValues: z
.array(
z.object({
value: z.string().optional(),
attribute: z.object({
id: z.string(),
}),
})
)
.optional(),
phoneNumber: z.string().optional(),
contactPerson: z.string().optional(),
openingDate: z.string().default(''),
Expand Down
1 change: 1 addition & 0 deletions src/pages/organisationUnits/form/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OrganisationUnit } from '../../../types/generated'

export type FormValues = Omit<OrganisationUnit, 'parent'> & {
// this is an array as teh orgunit selector operates on arrays
parent: { id: string }[]
}

0 comments on commit 8d62c32

Please sign in to comment.