Skip to content

Commit d06900a

Browse files
committed
fix: optimize metadata update handling by tracking changed fields
1 parent 0b527d9 commit d06900a

File tree

1 file changed

+70
-42
lines changed
  • app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/components

1 file changed

+70
-42
lines changed

app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/components/EditMetadata.tsx

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -322,51 +322,79 @@ export function EditMetadata({ id }: { id: string }) {
322322
};
323323

324324
const handleSave = (updatedData: any) => {
325-
if (JSON.stringify(updatedData) !== JSON.stringify(previousFormData)) {
326-
setPreviousFormData(updatedData);
325+
const changedFields: any = {};
327326

328-
const transformedValues = Object.keys(updatedData)?.reduce(
329-
(acc: any, key) => {
330-
acc[key] = Array.isArray(updatedData[key])
331-
? updatedData[key]
332-
.map((item: any) => item?.value || item)
333-
.join(', ')
334-
: updatedData[key];
335-
return acc;
336-
},
337-
{}
338-
);
327+
for (const key in updatedData) {
328+
const newValue = updatedData[key];
329+
const prevValue = previousFormData[key];
339330

340-
updateMetadataMutation.mutate({
341-
UpdateMetadataInput: {
342-
dataset: id,
343-
metadata: [
344-
...Object.keys(transformedValues)
345-
.filter(
346-
(valueItem) =>
347-
![
348-
'sectors',
349-
'description',
350-
'tags',
351-
'isPublic',
352-
'license',
353-
].includes(valueItem) && transformedValues[valueItem] !== ''
354-
)
355-
.map((key) => {
356-
return {
357-
id: key,
358-
value: transformedValues[key],
359-
};
360-
}),
361-
],
362-
...(updatedData.license && { license: updatedData.license }),
363-
accessType: updatedData.accessType || 'PUBLIC',
364-
description: updatedData.description || '',
365-
tags: updatedData.tags?.map((item: any) => item.label) || [],
366-
sectors: updatedData.sectors?.map((item: any) => item.value) || [],
367-
},
368-
});
331+
const isArray = Array.isArray(newValue);
332+
333+
const normalize = (val: any) =>
334+
isArray ? val?.map((item: any) => item?.value || item) : val;
335+
336+
const newNormalized = normalize(newValue);
337+
const prevNormalized = normalize(prevValue);
338+
339+
const hasChanged = isArray
340+
? JSON.stringify(newNormalized) !== JSON.stringify(prevNormalized)
341+
: newNormalized !== prevNormalized;
342+
343+
if (hasChanged) {
344+
changedFields[key] = newValue;
345+
}
369346
}
347+
348+
// Exit early if nothing changed
349+
if (Object.keys(changedFields).length === 0) return;
350+
351+
setPreviousFormData(updatedData); // Update local copy
352+
353+
const transformedValues = Object.keys(changedFields).reduce(
354+
(acc: any, key) => {
355+
acc[key] = Array.isArray(changedFields[key])
356+
? changedFields[key]
357+
.map((item: any) => item?.value || item)
358+
.join(', ')
359+
: changedFields[key];
360+
return acc;
361+
},
362+
{}
363+
);
364+
365+
updateMetadataMutation.mutate({
366+
UpdateMetadataInput: {
367+
dataset: id,
368+
metadata: Object.keys(transformedValues)
369+
.filter(
370+
(key) =>
371+
![
372+
'sectors',
373+
'description',
374+
'tags',
375+
'isPublic',
376+
'license',
377+
].includes(key) && transformedValues[key] !== ''
378+
)
379+
.map((key) => ({
380+
id: key,
381+
value: transformedValues[key],
382+
})),
383+
...(changedFields.license && { license: changedFields.license }),
384+
...(changedFields.accessType && {
385+
accessType: changedFields.accessType,
386+
}),
387+
...(changedFields.description !== undefined && {
388+
description: changedFields.description,
389+
}),
390+
...(changedFields.tags && {
391+
tags: changedFields.tags.map((item: any) => item.label),
392+
}),
393+
...(changedFields.sectors && {
394+
sectors: changedFields.sectors.map((item: any) => item.value),
395+
}),
396+
},
397+
});
370398
};
371399

372400
function renderInputField(metadataFormItem: any) {

0 commit comments

Comments
 (0)