From d696c72764eb580599a17b0522c5b1326387359a Mon Sep 17 00:00:00 2001 From: Julian Kniephoff Date: Wed, 28 Aug 2024 17:16:03 +0200 Subject: [PATCH] Fix rendering of selection fields in metadata bulk editing cf. for example the license field --- src/slices/eventSlice.ts | 5 +-- src/utils/resourceUtils.ts | 67 ++++++++++++++------------------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/slices/eventSlice.ts b/src/slices/eventSlice.ts index 274febe97f..88d6c3d833 100644 --- a/src/slices/eventSlice.ts +++ b/src/slices/eventSlice.ts @@ -8,7 +8,7 @@ import { prepareExtendedMetadataFieldsForPost, prepareMetadataFieldsForPost, transformMetadataCollection, - transformMetadataCollectionFields, + transformMetadataFields, } from "../utils/resourceUtils"; import { makeTwoDigits } from "../utils/utils"; import { sourceMetadata } from "../configs/sourceConfig"; @@ -312,7 +312,8 @@ export const postEditMetadata = createAppAsyncThunk('events/postEditMetadata', a let response = await data.data; // transform response - const metadata = transformMetadataCollectionFields(response.metadata); + let metadata = transformMetadataFields(response.metadata) + .map(field => ({ ...field, selected: false })); return { mergedMetadata: metadata, notFound: response.notFound, diff --git a/src/utils/resourceUtils.ts b/src/utils/resourceUtils.ts index d817540ad0..82c7799087 100644 --- a/src/utils/resourceUtils.ts +++ b/src/utils/resourceUtils.ts @@ -12,7 +12,7 @@ import { Recording } from "../slices/recordingSlice"; import { UserInfoState } from "../slices/userInfoSlice"; import { hasAccess, isJson } from "./utils"; import { RootState } from "../store"; -import { MetadataCatalog, MetadataField, MetadataFieldSelected } from "../slices/eventSlice"; +import { MetadataCatalog, MetadataField } from "../slices/eventSlice"; import { initialFormValuesNewGroup } from '../configs/modalConfig'; import { UpdateUser } from '../slices/userDetailsSlice'; import { TFunction } from 'i18next'; @@ -149,52 +149,33 @@ export const getInitialMetadataFieldValues = ( // transform collection of metadata into object with name and value export const transformMetadataCollection = (metadata: MetadataCatalog) => { - for (const [i, field] of metadata.fields.entries()) { - if (!!field.collection) { - metadata.fields[i].collection = Object.entries( - field.collection - ).map(([key, value]) => { - if (isJson(key)) { - let collectionParsed = JSON.parse(key); - return { - name: collectionParsed.label ? collectionParsed.label : key, - value: value, - ...collectionParsed, - }; - } else { - return { - name: key, - value: value, - }; - } - }); - } - } - + transformMetadataFields(metadata.fields); return metadata; -} - -// Same as above, but different! -export const transformMetadataCollectionFields = (metadata: MetadataFieldSelected[]) => { - for (const [i, field] of metadata.entries()) { - if (!!field) { - metadata[i].collection = Object.entries(field).map( - ([key, value]) => { - return { - name: key, - value: value, - }; - } - ); +}; + +export const transformMetadataFields = (metadata: MetadataField[]) => { + for (const field of metadata) { + if (field.collection) { + field.collection = Object.entries(field.collection) + .map(([key, value]) => { + if (isJson(key)) { + let collectionParsed = JSON.parse(key); + return { + name: collectionParsed.label || key, + value, + ...collectionParsed, + }; + } else { + return { + name: key, + value: value, + }; + } + }); } - metadata[i] = { - ...metadata[i], - selected: false, - }; } - return metadata; -} +}; // transform metadata catalog for update via post request export const transformMetadataForUpdate = (catalog: MetadataCatalog, values: { [key: string]: MetadataCatalog["fields"][0]["value"] }) => {