Skip to content

Commit

Permalink
feat: zod and format to handle responsibility of changing date format
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Dec 5, 2024
1 parent 8e49e88 commit d822c50
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/components/form/FormBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {
FormRenderProps,
Form as ReactFinalForm,
} from 'react-final-form'
import { defaultValueFormatter } from '../../lib/form/'
import { createFormValidate, defaultValueFormatter } from '../../lib/form/'

Check warning on line 8 in src/components/form/FormBase.tsx

View workflow job for this annotation

GitHub Actions / lint

'createFormValidate' is defined but never used
import {
PartialAttributeValue,
getAllAttributeValues,
} from '../../lib/models/attributes'
import { LoadingSpinner } from '../loading/LoadingSpinner'
import { useCustomAttributesQuery } from './attributes'
import { organisationUnitSchema } from '../../pages/organisationUnits/form'

Check failure on line 15 in src/components/form/FormBase.tsx

View workflow job for this annotation

GitHub Actions / lint

`../../pages/organisationUnits/form` import should occur before import of `../loading/LoadingSpinner`

Check warning on line 15 in src/components/form/FormBase.tsx

View workflow job for this annotation

GitHub Actions / lint

'organisationUnitSchema' is defined but never used

type MaybeModelWithAttributes = {
id?: string
Expand Down
10 changes: 3 additions & 7 deletions src/components/form/fields/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ export function DateField({
valid: true,
})

const { input, meta } = useField<string | undefined>(name)
const { input } = useField<string | undefined>(name, {
format: (value) => (value ? value.substring(0, 10) : ''),
})

useEffect(() => {
if (input.value) {
const dateWithNoTimestamp = input.value.substring(0, 10)
input.onChange(dateWithNoTimestamp)
}
}, [])
const handleChange: CalendarInputProps['onDateSelect'] = (
payload: {
calendarDateString: string
Expand Down
3 changes: 3 additions & 0 deletions src/lib/form/modelFormSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ const style = z.object({
icon: z.string().optional(),
})

const dateWithoutTimestampSchema = z.coerce.date()

export const modelFormSchemas = {
objectReference: modelReference,
referenceCollection,
identifiable,
attributeValues,
withAttributeValues,
dateWithoutTimestampSchema,
style,
modelReference,
}
8 changes: 6 additions & 2 deletions src/pages/organisationUnits/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '../../lib'
import { useBoundResourceQueryFn } from '../../lib/query/useBoundQueryFn'
import { OrganisationUnit, PickWithFieldFilters } from '../../types/generated'
import { OrganisationUnitFormField, validate } from './form'
import {
OrganisationUnitFormField,
organisationUnitSchema,
validate,
} from './form'

const fieldFilters = [
...DEFAULT_FIELD_FILTERS,
Expand Down Expand Up @@ -67,7 +71,7 @@ export const Component = () => {
})}
section={section}
initialValues={orgUnit.data}
validate={validate}
schema={organisationUnitSchema}
>
<DefaultEditFormContents section={section}>
<OrganisationUnitFormField />
Expand Down
12 changes: 8 additions & 4 deletions src/pages/organisationUnits/form/organisationUnitSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import i18n from '@dhis2/d2-i18n'
import { z } from 'zod'
import { createFormValidate, getDefaults, modelFormSchemas } from '../../../lib'

const { withAttributeValues, identifiable, referenceCollection } =
modelFormSchemas
const {
withAttributeValues,
identifiable,
referenceCollection,
dateWithoutTimestampSchema,
} = modelFormSchemas

export const organisationUnitSchema = identifiable
.merge(withAttributeValues)
Expand All @@ -25,7 +29,7 @@ export const organisationUnitSchema = identifiable
}),
})
.optional(),
openingDate: z.string().date(),
openingDate: dateWithoutTimestampSchema,
email: z.string().email().optional(),
address: z
.string()
Expand All @@ -39,7 +43,7 @@ export const organisationUnitSchema = identifiable
.string()
.url({ message: i18n.t('Must be a valid url') })
.optional(),
closedDate: z.string().date().optional(),
closedDate: dateWithoutTimestampSchema.optional(),
comment: z
.string()
.max(2000, {
Expand Down
1 change: 1 addition & 0 deletions src/pages/organisationUnits/list/OrganisationUnitList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export const OrganisationUnitList = () => {
return (
<div>
<SectionListTitle />
<IdentifiableFilter />
<div className={css.listDetailsWrapper}>
<Toolbar
selectedModels={
Expand Down

0 comments on commit d822c50

Please sign in to comment.