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

VE-2740: error handling for post message events #250

Merged
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
4 changes: 3 additions & 1 deletion src/visualBuilder/components/FieldToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ function FieldToolbarComponent(
fieldMetadata.content_type_uid,
fieldMetadata.fieldPath
);
setFieldSchema(fieldSchema);
if(fieldSchema){
setFieldSchema(fieldSchema);
}
}
fetchFieldSchema();
}, [fieldMetadata]);
Expand Down
6 changes: 4 additions & 2 deletions src/visualBuilder/components/fieldLabelWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { uniqBy } from "lodash-es";
import { visualBuilderStyles } from "../visualBuilder.style";
import { VariantIcon } from "./icons/variant";
import { CslpError } from "./CslpError";
import { hasPostMessageError } from "../utils/errorHandling";


async function getFieldDisplayNames(fieldMetadata: CslpData[]) {
const result = await visualBuilderPostMessage?.send<{
Expand Down Expand Up @@ -65,13 +67,13 @@ function FieldLabelWrapperComponent(
"cslpValue"
);
const displayNames = await getFieldDisplayNames(allPaths);

const fieldSchema = await FieldSchemaMap.getFieldSchema(
props.fieldMetadata.content_type_uid,
props.fieldMetadata.fieldPath
);

if(!fieldSchema){
if(hasPostMessageError(displayNames) || !fieldSchema) {
setDisplayNamesLoading(false);
setError(true)
setDisplayNamesLoading(false)
return;
Expand Down
4 changes: 4 additions & 0 deletions src/visualBuilder/generators/generateEmptyBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export async function generateEmptyBlocks(
fieldMetadata.content_type_uid,
fieldMetadata.fieldPath
);

if(!fieldSchema){
return;
}

hydrate(
<EmptyBlock
Expand Down
20 changes: 11 additions & 9 deletions src/visualBuilder/listeners/mouseClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,17 @@ async function handleBuilderInteraction(
fieldPath
);

// after field schema is available re-add disabled overlay
const { isDisabled } = isFieldDisabled(fieldSchema, eventDetails);
if (isDisabled) {
addOverlay({
overlayWrapper: params.overlayWrapper,
resizeObserver: params.resizeObserver,
editableElement: editableElement,
isFieldDisabled: true,
});
if (fieldSchema) {
// after field schema is available re-add disabled overlay
const { isDisabled } = isFieldDisabled(fieldSchema, eventDetails);
if (isDisabled) {
addOverlay({
overlayWrapper: params.overlayWrapper,
resizeObserver: params.resizeObserver,
editableElement: editableElement,
isFieldDisabled: true,
});
}
}

// This is most probably redundant code, as the handleIndividualFields function
Expand Down
2 changes: 2 additions & 0 deletions src/visualBuilder/listeners/mouseHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async function handleMouseHover(params: HandleMouseHoverParams): Promise<void> {
*/
FieldSchemaMap.getFieldSchema(content_type_uid, fieldPath).then(
(fieldSchema) => {
if(!fieldSchema) return;
if (!params.customCursor) return;
const { isDisabled: fieldDisabled } = isFieldDisabled(
fieldSchema,
Expand Down Expand Up @@ -199,6 +200,7 @@ async function handleMouseHover(params: HandleMouseHoverParams): Promise<void> {
addOutline(editableElement);
FieldSchemaMap.getFieldSchema(content_type_uid, fieldPath).then(
(fieldSchema) => {
if(!fieldSchema) return;
const { isDisabled: fieldDisabled, reason } =
isFieldDisabled(fieldSchema, eventDetails);
addOutline(editableElement, fieldDisabled);
Expand Down
4 changes: 4 additions & 0 deletions src/visualBuilder/utils/errorHandling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function hasPostMessageError(obj: any): boolean {
return obj?.error === true;
}
2 changes: 1 addition & 1 deletion src/visualBuilder/utils/fieldSchemaMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class FieldSchemaMap {
FieldSchemaMap.fieldSchema[contentTypeUid] = data.fieldSchemaMap;
}

return FieldSchemaMap.fieldSchema[contentTypeUid][fieldCslp];
return FieldSchemaMap?.fieldSchema?.[contentTypeUid]?.[fieldCslp] || null;
}

static hasFieldSchema(contentTypeUid: string, fieldCslp: string): boolean {
Expand Down
5 changes: 5 additions & 0 deletions src/visualBuilder/utils/getDiscussionIdByFieldMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import visualBuilderPostMessage from "./visualBuilderPostMessage";
import { ISchemaFieldMap } from "./types/index.types";
import { VisualBuilderPostMessageEvents } from "./types/postMessage.types";
import { IActiveDiscussion } from "../components/CommentIcon";
import { hasPostMessageError } from "./errorHandling";

// Define an interface for the argument structure
interface GetDiscussionIdParams {
Expand All @@ -28,5 +29,9 @@ export async function getDiscussionIdByFieldMetaData(
{ fieldMetadata, fieldSchema }
)) ?? null;

if(hasPostMessageError(discussion)){
return null
}

return discussion;
}
5 changes: 5 additions & 0 deletions src/visualBuilder/utils/getFieldData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CslpData } from "../../cslp/types/cslp.types";
import visualBuilderPostMessage from "./visualBuilderPostMessage";
import { VisualBuilderPostMessageEvents } from "./types/postMessage.types";
import { hasPostMessageError } from "./errorHandling";

/**
* Retrieves the expected field data based on the provided field metadata.
Expand All @@ -17,6 +18,10 @@ export async function getFieldData(
VisualBuilderPostMessageEvents.GET_FIELD_DATA,
{ fieldMetadata, entryPath: entryPath ?? "" }
);

if(hasPostMessageError(data)){
return "";
}

// toString from lodash
// return toString(data?.fieldData);
Expand Down
18 changes: 0 additions & 18 deletions src/visualBuilder/utils/getFieldPathWithUid.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/visualBuilder/utils/getFieldType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ISchemaFieldMap, FieldDataType } from "./types/index.types";

export function getFieldType(fieldSchema: ISchemaFieldMap): FieldDataType {
if(!fieldSchema) return;
if (Object.hasOwnProperty.call(fieldSchema, "extension_uid")) {
return FieldDataType.CUSTOM_FIELD;
}
Expand Down
10 changes: 5 additions & 5 deletions src/visualBuilder/utils/handleIndividualFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export async function handleIndividualFields(
);

if (
// @ts-ignore
fieldSchema?.multiple ||
(fieldSchema.data_type === "reference" &&
// @ts-ignore
fieldSchema.field_metadata.ref_multiple)
fieldSchema &&
(fieldSchema?.multiple ||
(fieldSchema?.data_type === "reference" &&
// @ts-ignore
fieldSchema?.field_metadata.ref_multiple))
) {
if (lastEditedField !== editableElement) {
const addButtonLabel =
Expand Down
Loading