From 8ce5e1f9c3d0c9b6fdc007bdf10f7f1ad658a06e Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Wed, 4 Dec 2024 13:57:30 +0100 Subject: [PATCH 1/2] fix: show zero values, add factor validations --- i18n/en.pot | 69 +++++++++++++++++-- .../modelValue/ModelValueRenderer.tsx | 2 +- src/lib/models/useFieldValidators.ts | 29 ++++++-- .../form/IndicatorTypesFormFields.tsx | 4 ++ .../form/IndicatorTypesSchema.ts | 9 +-- 5 files changed, 95 insertions(+), 18 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index ca3938f55..65a41639b 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -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-27T08:30:38.416Z\n" -"PO-Revision-Date: 2024-11-27T08:30:38.417Z\n" -"POT-Creation-Date: 2024-11-26T14:06:00.934Z\n" -"PO-Revision-Date: 2024-11-26T14:06:00.934Z\n" +"POT-Creation-Date: 2024-12-04T10:11:21.431Z\n" +"PO-Revision-Date: 2024-12-04T10:11:21.431Z\n" msgid "schemas" msgstr "schemas" @@ -425,6 +423,15 @@ msgstr "Translation updated successfully" msgid "Save translations" msgstr "Save translations" +msgid "Go back" +msgstr "Go back" + +msgid "Next section" +msgstr "Next section" + +msgid "Save and exit" +msgstr "Save and exit" + msgid "Can edit and capture" msgstr "Can edit and capture" @@ -1215,6 +1222,60 @@ msgstr "" "included. PHU will still be available for the PHU level, but not included " "in the aggregations to the levels above." +msgid "Set up the basic information for this data set." +msgstr "Set up the basic information for this data set." + +msgid "Configure data elements" +msgstr "Configure data elements" + +msgid "Choose what data is collected for this data set." +msgstr "Choose what data is collected for this data set." + +msgid "Configure data entry periods" +msgstr "Configure data entry periods" + +msgid "Choose for what time periods data can be entered for this data set" +msgstr "Choose for what time periods data can be entered for this data set" + +msgid "Validation and limitations" +msgstr "Validation and limitations" + +msgid "Configure how data can and must be entered for this data" +msgstr "Configure how data can and must be entered for this data" + +msgid "Configure which organisation units can collect data for this data set." +msgstr "Configure which organisation units can collect data for this data set." + +msgid "Data entry form" +msgstr "Data entry form" + +msgid "Advanced options" +msgstr "Advanced options" + +msgid "These options are used for advanced data set configurations." +msgstr "These options are used for advanced data set configurations." + +msgid "Setup" +msgstr "Setup" + +msgid "Data" +msgstr "Data" + +msgid "Data Elements" +msgstr "Data Elements" + +msgid "Periods" +msgstr "Periods" + +msgid "Organisation Units" +msgstr "Organisation Units" + +msgid "Form" +msgstr "Form" + +msgid "Advanced" +msgstr "Advanced" + msgid "Set up the basic information for this Indicator Type." msgstr "Set up the basic information for this Indicator Type." diff --git a/src/components/sectionList/modelValue/ModelValueRenderer.tsx b/src/components/sectionList/modelValue/ModelValueRenderer.tsx index 7e8652643..f6d29298d 100644 --- a/src/components/sectionList/modelValue/ModelValueRenderer.tsx +++ b/src/components/sectionList/modelValue/ModelValueRenderer.tsx @@ -41,7 +41,7 @@ export const ModelValueRenderer = ({ return } - if (value) { + if (value ?? false) { return } return null diff --git a/src/lib/models/useFieldValidators.ts b/src/lib/models/useFieldValidators.ts index b47835d3b..851c0b75b 100644 --- a/src/lib/models/useFieldValidators.ts +++ b/src/lib/models/useFieldValidators.ts @@ -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, @@ -20,14 +34,13 @@ 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) @@ -35,6 +48,12 @@ export function useValidator({ 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(validators), diff --git a/src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx b/src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx index f1ade7d24..09b6d5ab5 100644 --- a/src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx +++ b/src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx @@ -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 ( <> @@ -39,6 +42,7 @@ export const IndicatorTypesFormFields = () => { inputWidth="400px" component={InputFieldFF} label={i18n.t('Factor')} + validate={validateFactor} required /> diff --git a/src/pages/indicatorTypes/form/IndicatorTypesSchema.ts b/src/pages/indicatorTypes/form/IndicatorTypesSchema.ts index e530b2142..639f3da93 100644 --- a/src/pages/indicatorTypes/form/IndicatorTypesSchema.ts +++ b/src/pages/indicatorTypes/form/IndicatorTypesSchema.ts @@ -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) From bdd76bf6cb256c9f41679508037addf37c1754c0 Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Mon, 9 Dec 2024 09:20:33 +0100 Subject: [PATCH 2/2] fix: type checks --- .../sectionList/modelValue/ModelValueRenderer.tsx | 9 ++++++++- src/types/schemaBase.ts | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/components/sectionList/modelValue/ModelValueRenderer.tsx b/src/components/sectionList/modelValue/ModelValueRenderer.tsx index f6d29298d..8f34ad10f 100644 --- a/src/components/sectionList/modelValue/ModelValueRenderer.tsx +++ b/src/components/sectionList/modelValue/ModelValueRenderer.tsx @@ -18,6 +18,12 @@ export const ModelValueRenderer = ({ value, schemaProperty, }: ValueDetails) => { + const hasToStringMethod = ( + value: unknown + ): value is { toString: () => string } => + typeof value === 'object' && + typeof (value as any).toString === 'function' + if (path === 'sharing.public' && typeof value === 'string') { return } @@ -41,8 +47,9 @@ export const ModelValueRenderer = ({ return } - if (value ?? false) { + if (value !== null && value !== undefined && hasToStringMethod(value)) { return } + return null } diff --git a/src/types/schemaBase.ts b/src/types/schemaBase.ts index 704887230..fc5802b09 100644 --- a/src/types/schemaBase.ts +++ b/src/types/schemaBase.ts @@ -35,6 +35,8 @@ export interface SchemaFieldProperty { unique: boolean required: boolean length?: number + max?: number + min?: number persisted: boolean collectionName?: string collection: boolean