Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show zero values, add factor validations #463

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-12-05T06:06:51.471Z\n"
"PO-Revision-Date: 2024-12-05T06:06:51.471Z\n"
"POT-Creation-Date: 2024-12-03T07:31:45.454Z\n"
"PO-Revision-Date: 2024-12-03T07:31:45.455Z\n"
"POT-Creation-Date: 2024-12-09T08:26:30.983Z\n"
"PO-Revision-Date: 2024-12-09T08:26:30.983Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -1012,6 +1010,9 @@ msgstr "Skip category total in reports"
msgid "Choose the categories to include in this category combo."
msgstr "Choose the categories to include in this category combo."

msgid "At least one category is required"
msgstr "At least one category is required"

msgid ""
"The number of generated category option combinations exceeds the limit of "
"{{limit}}"
Expand Down
8 changes: 7 additions & 1 deletion src/components/sectionList/modelValue/ModelValueRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const ModelValueRenderer = ({
value,
schemaProperty,
}: ValueDetails) => {
const hasToStringMethod = (
value: unknown
): value is { toString: () => string } =>
typeof (value as any).toString === 'function'

if (path === 'sharing.public' && typeof value === 'string') {
return <PublicAccessValue value={value} />
}
Expand All @@ -41,8 +46,9 @@ export const ModelValueRenderer = ({
return <BooleanValue value={value} />
}

if (value) {
if (value !== null && value !== undefined && hasToStringMethod(value)) {
return <TextValue value={value.toString()} />
}

return null
}
29 changes: 24 additions & 5 deletions src/lib/models/useFieldValidators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Validator } from '@dhis2/ui'
import { createMaxNumber, createMinNumber, Validator } from '@dhis2/ui'
import { useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { SchemaFieldPropertyType, SchemaSection } from '../../types'
import { SchemaFieldProperty, SchemaSection } from '../../types'
import { composeAsyncValidators, required } from '../form'
import { useSchema } from '../schemas'
import { checkMaxLengthFromProperty } from './useCheckMaxLengthFromSchema'
import { useIsFieldValueUnique } from './useIsFieldValueUnique'

export function checkMaxValueFromProperty(
propertyDetails: SchemaFieldProperty
): (value: unknown) => string | undefined {
const maxValue = propertyDetails.max
return maxValue == undefined ? () => undefined : createMaxNumber(maxValue)
}

export function checkMinValueFromProperty(
propertyDetails: SchemaFieldProperty
): (value: unknown) => string | undefined {
const minValue = propertyDetails.min
return minValue == undefined ? () => undefined : createMinNumber(minValue)
}

export function useValidator({
schemaSection,
property,
Expand All @@ -20,21 +34,26 @@ export function useValidator({
const validators = useMemo(() => [] as Validator[], [])
const params = useParams()
const modelId = params.id as string
const checkMaxLength = checkMaxLengthFromProperty(propertyDetails)
const checkIsValueTaken = useIsFieldValueUnique({
model: schemaSection.namePlural,
field: property,
id: modelId,
}) as Validator
if (propertyDetails.length) {
validators.push(checkMaxLength)
if (propertyDetails.propertyType !== 'INTEGER' && propertyDetails.length) {
validators.push(checkMaxLengthFromProperty(propertyDetails))
}
if (propertyDetails.unique) {
validators.push(checkIsValueTaken)
}
if (propertyDetails.required) {
validators.push(required)
}
if (propertyDetails.propertyType === 'INTEGER' && propertyDetails.max) {
validators.push(checkMaxValueFromProperty(propertyDetails))
}
if (propertyDetails.propertyType === 'INTEGER' && propertyDetails.min) {
validators.push(checkMinValueFromProperty(propertyDetails))
}

return useMemo(
() => composeAsyncValidators<string>(validators),
Expand Down
4 changes: 4 additions & 0 deletions src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import {
NameField,
} from '../../../components'
import { SECTIONS_MAP, useSchemaSectionHandleOrThrow } from '../../../lib'
import { useValidator } from '../../../lib/models/useFieldValidators'

export const IndicatorTypesFormFields = () => {
const section = SECTIONS_MAP.indicatorType
const schemaSection = useSchemaSectionHandleOrThrow()

const validateFactor = useValidator({ schemaSection, property: 'factor' })

return (
<>
<StandardFormSection>
Expand All @@ -39,6 +42,7 @@ export const IndicatorTypesFormFields = () => {
inputWidth="400px"
component={InputFieldFF}
label={i18n.t('Factor')}
validate={validateFactor}
required
/>
</StandardFormField>
Expand Down
9 changes: 1 addition & 8 deletions src/pages/indicatorTypes/form/IndicatorTypesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ const { identifiable } = modelFormSchemas
export const IndicatorSchema = identifiable.extend({
factor: z.coerce
.number({ invalid_type_error: 'Please enter a number' })
.int()
.max(
Number.MAX_SAFE_INTEGER,
`The number is too large. Please enter a valid integer.`
)
.refine((value) => value !== 0, {
message: 'Zero is not a valid value for factor',
}),
.int(),
})

export const initialValues = getDefaults(IndicatorSchema)
Expand Down
2 changes: 2 additions & 0 deletions src/types/schemaBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface SchemaFieldProperty {
unique: boolean
required: boolean
length?: number
max?: number
min?: number
persisted: boolean
collectionName?: string
collection: boolean
Expand Down
Loading