Skip to content

Commit

Permalink
Fix for collection editor get code dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
fgatti675 committed Oct 25, 2024
1 parent 112132e commit 82505c7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityCollection, useSnackbarController } from "@firecms/core";
import { EntityCollection, isEmptyObject, useSnackbarController } from "@firecms/core";
import { Button, ContentCopyIcon, Dialog, DialogActions, DialogContent, Typography, } from "@firecms/ui";
import React from "react";
import JSON5 from "json5";
Expand Down Expand Up @@ -78,24 +78,34 @@ export function GetCodeDialog({

function collectionToCode(collection: EntityCollection): object {

const propertyCleanup = (property: any) => {

const updatedProperty = {
...property
};
const propertyCleanup = (value: any): any => {
if (typeof value === "function") {
return value;
}
if (Array.isArray(value)) {
return value.map((v: any) => propertyCleanup(v));
}
if (typeof value === "object") {
if (value === null)
return value;
Object.keys(value).forEach((key) => {
if (!isEmptyObject(value)) {
const childRes = propertyCleanup(value[key]);
if (childRes !== null && childRes !== undefined && childRes !== false && !isEmptyObject(childRes)) {
value[key] = childRes;
} else {
delete value[key];
}
}
});
}

delete updatedProperty.fromBuilder;
delete updatedProperty.resolved;
delete updatedProperty.propertiesOrder;
delete updatedProperty.editable;
delete value.fromBuilder;
delete value.resolved;
delete value.propertiesOrder;
delete value.editable;

if (updatedProperty.type === "map") {
return {
...updatedProperty,
properties: updatedProperty.properties.map(propertyCleanup)
}
}
return updatedProperty;
return value;
}

return {
Expand All @@ -111,7 +121,9 @@ function collectionToCode(collection: EntityCollection): object {
customId: collection.customId,
initialFilter: collection.initialFilter,
initialSort: collection.initialSort,
properties: Object.entries(collection.properties ?? {})
properties: Object.entries({
...(collection.properties ?? {})
})
.map(([key, value]) => ({
[key]: propertyCleanup(value)
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ConfirmationDialog,
DEFAULT_FIELD_CONFIGS,
getFieldConfig,
getFieldId,
getFieldId, isEmptyObject,
isPropertyBuilder,
isValidRegExp,
mergeDeep,
Expand Down Expand Up @@ -380,7 +380,7 @@ function PropertyEditFormFields({
}, [deferredValues, includeIdAndTitle, propertyNamespace]);

useEffect(() => {
if (values?.id && onError) {
if (values?.id && onError && !isEmptyObject(errors)) {
onError(values?.id, propertyNamespace, errors);
}
}, [errors, propertyNamespace, values?.id]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function BlockPropertyField({
? undefined
: onPropertyMove}/>

{!disabled && !values.oneOf?.propertiesOrder?.length &&
{!disabled && (values.oneOf?.propertiesOrder?.length === 0)&&
<div className="h-full flex items-center justify-center p-4">
Add the first property to this block
</div>}
Expand Down
3 changes: 1 addition & 2 deletions packages/firecms_core/src/hooks/data/save.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DataSource,
Entity,
EntityCallbacks,
EntityCollection,
EntityValues,
FireCMSContext,
Expand Down Expand Up @@ -108,7 +107,7 @@ export async function saveEntityWithCallbacks<M extends Record<string, any>, Use
updatedValues = values;
}

console.log("Saving entity", {
console.debug("Saving entity", {
entityId,
updatedValues,
collection
Expand Down
20 changes: 20 additions & 0 deletions packages/firecms_core/src/util/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ export function removeUndefined(value: any, removeEmptyStrings?: boolean): any {
return value;
}

export function removeNulls(value: any): any {
if (typeof value === "function") {
return value;
}
if (Array.isArray(value)) {
return value.map((v: any) => removeNulls(v));
}
if (typeof value === "object") {
const res: object = {};
if (value === null)
return value;
Object.keys(value).forEach((key) => {
if (value[key] !== null)
(res as any)[key] = removeNulls(value[key]);
});
return res;
}
return value;
}

export function isEmptyObject(obj: object) {
return obj &&
Object.getPrototypeOf(obj) === Object.prototype &&
Expand Down

0 comments on commit 82505c7

Please sign in to comment.