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

feat: add format validation to date fields and to dates in schemas #451

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 26 additions & 7 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-11-25T10:03:10.019Z\n"
"PO-Revision-Date: 2024-11-25T10:03:10.019Z\n"
"POT-Creation-Date: 2024-11-26T14:06:00.934Z\n"
"PO-Revision-Date: 2024-11-26T14:06:00.934Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -102,6 +102,9 @@ msgstr "Set up information for the attributes assigned to {{modelName}}"
msgid "Code"
msgstr "Code"

msgid "Required"
msgstr "Required"

msgid "Description"
msgstr "Description"

Expand Down Expand Up @@ -891,6 +894,9 @@ msgstr "This field requires a unique value, please choose another one"
msgid "{{label}} (required)"
msgstr "{{label}} (required)"

msgid "Should not exceed {{maxLength}} characters"
msgstr "Should not exceed {{maxLength}} characters"

msgid "No changes to be saved"
msgstr "No changes to be saved"

Expand All @@ -903,9 +909,6 @@ msgstr "Cannot save empty object"
msgid "Created successfully"
msgstr "Created successfully"

msgid "Required"
msgstr "Required"

msgid "Period type"
msgstr "Period type"

Expand Down Expand Up @@ -1241,8 +1244,8 @@ msgstr ""
"Choose where this new organisation unit should be placed in the existing "
"hierarchy"

msgid "Set up the basic information for this organisation unit."
msgstr "Set up the basic information for this organisation unit."
msgid "Set up the basic information for this organisation unit"
msgstr "Set up the basic information for this organisation unit"

msgid "Opening date"
msgstr "Opening date"
Expand Down Expand Up @@ -1311,6 +1314,22 @@ msgstr ""
"This is the first organisation unit and will be created as the root of the "
"hierarchy."

msgid "Must be a valid mobile number"
msgstr "Must be a valid mobile number"

msgid "Must be a valid url"
msgstr "Must be a valid url"

msgid ""
"Longitude should be between -90 and 90. Latitude should be between -180 and "
"180"
msgstr ""
"Longitude should be between -90 and 90. Latitude should be between -180 and "
"180"

msgid "Parent organisation unit cannot be itself or a descendant of itself."
msgstr "Parent organisation unit cannot be itself or a descendant of itself."

msgid "No organisation units available"
msgstr "No organisation units available"

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@dhis2/app-runtime": "^3.9.3",
"@dhis2/multi-calendar-dates": "^2.0.0",
"@dhis2/ui": "^10.0.2",
"@dhis2/ui": "^10.1.3",
"@tanstack/react-table": "^8.16.0",
"@types/lodash": "^4.14.198",
"lodash": "^4.17.21",
Expand All @@ -56,6 +56,6 @@
"resolutions": {
"eslint": "^8",
"@dhis2/multi-calendar-dates": "^2.0.0",
"@dhis2/ui": "^10.0.2"
"@dhis2/ui": "^10.1.3"
}
}
27 changes: 18 additions & 9 deletions src/components/form/fields/DateField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import i18n from '@dhis2/d2-i18n'
import { CalendarInput, CalendarInputProps } from '@dhis2/ui'
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { useField } from 'react-final-form'
import { selectedLocale, useSystemSetting } from '../../../lib'

Expand All @@ -16,6 +17,7 @@ type ValidationProps = {
error: boolean
validationText?: string
valid?: boolean
validationCode?: string
}
export function DateField({
name,
Expand All @@ -29,13 +31,18 @@ export function DateField({
error: false,
})

const { input } = useField<string | undefined>(name, {
validate: () => {
if (validation.error) {
return validation.validationText
}
},
})
const { input, meta } = useField<string | undefined>(name)

useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really love this, but I guess with the validation API for the input, this is the easiest way... Doesn't handle change get called when it's cleared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know i don't either. I think the plan was to add this to the component, since other validations are handled there as well, so was hoping for it to be a temp thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Birkbjo let me know if you think what i just pushed is better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think thats a bit cleaner - nice! Just some minor lint issues now :)

if (input.value === '' && meta.touched && required) {
setValidation({
error: true,
valid: false,
validationCode: 'EMPTY',
validationText: i18n.t('Required'),
})
}
}, [required, input.value, meta.touched])

const handleChange: CalendarInputProps['onDateSelect'] = (
payload: {
Expand All @@ -54,13 +61,15 @@ export function DateField({
inputWidth={'400px'}
date={input.value}
name={name}
required={required}
calendar={calendar as CalendarInputProps['calendar']}
onDateSelect={handleChange}
timeZone={'utc'}
locale={locale}
format={'YYYY-MM-DD'}
onBlur={(_, e) => input.onBlur(e)}
clearable
label={required ? `${label} (required) *` : label}
label={required ? `${label} (required)` : label}
{...validation}
valid={validation?.valid && input?.value !== ''}
{...calendarInputProps}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/categoryOptions/form/categoryOptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const categoryOptionSchema = identifiable
})
.optional(),
description: z.string().trim().optional(),
startDate: z.string().optional(),
endDate: z.string().optional(),
startDate: z.string().date().optional(),
endDate: z.string().date().optional(),
organisationUnits: referenceCollection.optional(),
})
.refine(
Expand Down
4 changes: 2 additions & 2 deletions src/pages/organisationUnits/form/organisationUnitSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const organisationUnitSchema = identifiable
}),
})
.optional(),
openingDate: z.string(),
openingDate: z.string().date(),
email: z.string().email().optional(),
address: z
.string()
Expand All @@ -47,7 +47,7 @@ export const organisationUnitSchema = identifiable
.string()
.url({ message: i18n.t('Must be a valid url') })
.optional(),
closedDate: z.string().optional(),
closedDate: z.string().date().optional(),
comment: z
.string()
.max(2147483647, {
Expand Down
Loading
Loading