Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering of selection fields in metadata bulk editing #887

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/slices/eventSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
prepareExtendedMetadataFieldsForPost,
prepareMetadataFieldsForPost,
transformMetadataCollection,
transformMetadataCollectionFields,
transformMetadataFields,
} from "../utils/resourceUtils";
import { makeTwoDigits } from "../utils/utils";
import { sourceMetadata } from "../configs/sourceConfig";
Expand Down Expand Up @@ -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,
Expand Down
67 changes: 24 additions & 43 deletions src/utils/resourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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"] }) => {
Expand Down
Loading