From 21e73b8f15bcb31fa04a2a262bbad74d7d2363f8 Mon Sep 17 00:00:00 2001 From: Flaminia Date: Tue, 17 Dec 2024 09:07:52 +0100 Subject: [PATCH] feat: do not allow creation of org units at level 1 unless it's the first (#470) [skip release] * fix: require a parent orgunit when creating new org units * feat: add tests specs --- src/pages/organisationUnits/New.spec.tsx | 2 ++ .../organisationUnits/form/OrganisationUnitSelector.tsx | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) 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', () => {}) }) 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)