From d083a959a410d0edcffe1af96c4d3b8e3db0c925 Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Mon, 16 Dec 2024 15:48:31 +0100 Subject: [PATCH 1/2] fix: require a parent orgunit when creating new org units --- .../organisationUnits/form/OrganisationUnitSelector.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx b/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx index b66a7449..f12dd070 100644 --- a/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx +++ b/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx @@ -2,13 +2,17 @@ import i18n from '@dhis2/d2-i18n' import { Field, NoticeBox, OrganisationUnitTree } from '@dhis2/ui' import { IconInfo16 } from '@dhis2/ui-icons' import React, { useState } from 'react' -import { useField } from 'react-final-form' +import { FieldRenderProps, useField } from 'react-final-form' import { useCurrentUserRootOrgUnits } from '../../../lib/user/currentUserStore' import classes from './OrganisationUnitSelector.module.css' export function OrganisationUnitSelector() { const fieldName = 'parent' - const { input, meta } = useField(fieldName, { format: (value) => value }) + const { input, meta } = useField(fieldName, { + format: (value) => value, + validate: (value) => + !value && userRootOrgUnits.length > 0 ? 'Required' : undefined, + }) const userRootOrgUnits = useCurrentUserRootOrgUnits() const userRootOrgUnitsIds = userRootOrgUnits.map((unit) => unit.id) const userRootOrgUnitsPaths = userRootOrgUnits.map((unit) => unit.path) From 4b0878aefd201e8d6dff339d145f1f532aa951c4 Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Mon, 16 Dec 2024 16:01:22 +0100 Subject: [PATCH 2/2] feat: add tests specs --- src/pages/organisationUnits/New.spec.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pages/organisationUnits/New.spec.tsx b/src/pages/organisationUnits/New.spec.tsx index 08135911..3a780888 100644 --- a/src/pages/organisationUnits/New.spec.tsx +++ b/src/pages/organisationUnits/New.spec.tsx @@ -28,4 +28,6 @@ describe('Indicator type add form additional tests', () => { it('should show an error if latitude is above 180', () => {}) it('should show an error if latitude is below -180', () => {}) it('should show an error if email is not valid', () => {}) + it('should show an error if parent org unit is not selected', () => {}) + it('should allow creation of the first org unit', () => {}) })