From b1adff95fbe081a07953c97b179e0e0adbeefc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Saracca?= Date: Wed, 13 Nov 2024 13:33:01 -0300 Subject: [PATCH] feat: validate that selected facets are not empty --- public/locales/en/shared.json | 5 ++- .../BrowseSearchFacetsSection.module.scss | 9 ++++++ .../BrowseSearchFacetsSection.tsx | 31 +++++++++++++------ .../collection-form/useSubmitCollection.ts | 2 -- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/public/locales/en/shared.json b/public/locales/en/shared.json index 70811f611..0380b9915 100644 --- a/public/locales/en/shared.json +++ b/public/locales/en/shared.json @@ -126,7 +126,10 @@ "label": "Browse/Search Facets", "helperText": "Choose and order the metadata fields to use as facets when browsing this collection.", "useBrowseSearchFacetsFrom": "Use browse/search facets from", - "selectedFacets": "Selected" + "selectedFacets": "Selected", + "invalid": { + "minLength": "At least one facet must be selected." + } } }, "confirmResetModal": { diff --git a/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.module.scss b/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.module.scss index 252f11bf4..5c72cb488 100644 --- a/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.module.scss +++ b/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.module.scss @@ -1,7 +1,16 @@ +@import 'node_modules/@iqss/dataverse-design-system/src/lib/assets/styles/design-tokens/colors.module'; + .transfer-list-container { display: flex; justify-content: center; + .error-msg { + width: 100%; + margin-top: 0.25rem; + color: $dv-danger-color; + font-size: 0.875em; + } + @media (min-width: 768px) { justify-content: flex-start; } diff --git a/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.tsx b/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.tsx index 42cbf07e1..3939d5557 100644 --- a/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.tsx +++ b/src/sections/shared/form/EditCreateCollectionForm/collection-form/browse-search-facets-section/BrowseSearchFacetsSection.tsx @@ -128,15 +128,28 @@ export const BrowseSearchFacetsSection = ({ ( - handleOnChangeSelectedItems(selectedItems, onChange)} - availableItems={availableItems} - defaultSelected={defaultCollectionFacets} - rightLabel={t('fields.browseSearchFacets.selectedFacets')} - disabled={useBrowseSearchFacetsFromParentCheckedValue} - key={resetKey} - /> + rules={{ + validate: (facetsSelected: CollectionFormFacet[]) => { + if (facetsSelected.length === 0) { + return t('fields.browseSearchFacets.invalid.minLength') + } + return true + } + }} + render={({ field: { onChange }, fieldState: { invalid, error } }) => ( +
+ + handleOnChangeSelectedItems(selectedItems, onChange) + } + availableItems={availableItems} + defaultSelected={defaultCollectionFacets} + rightLabel={t('fields.browseSearchFacets.selectedFacets')} + disabled={useBrowseSearchFacetsFromParentCheckedValue} + key={resetKey} + /> + {invalid &&
{error?.message}
} +
)} /> diff --git a/src/sections/shared/form/EditCreateCollectionForm/collection-form/useSubmitCollection.ts b/src/sections/shared/form/EditCreateCollectionForm/collection-form/useSubmitCollection.ts index ef4dc16b4..65bfc6248 100644 --- a/src/sections/shared/form/EditCreateCollectionForm/collection-form/useSubmitCollection.ts +++ b/src/sections/shared/form/EditCreateCollectionForm/collection-form/useSubmitCollection.ts @@ -111,8 +111,6 @@ export function useSubmitCollection( shouldSendFacetIds }) - // TODO:ME Check what happens if sending facetsId as empty array - const newOrUpdatedCollection: CollectionDTO = { name: formData.name, alias: formData.alias,