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

handle multiple variant status case #302

Merged
merged 1 commit into from
Nov 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export const BASE_VARIANT_STATUS: IVariantStatus = {
};

export async function getFieldVariantStatus(
fieldPathWithIndex: string
fieldMetadata: CslpData
): Promise<IVariantStatus | undefined> {
try {
const result = await visualBuilderPostMessage?.send<IVariantStatus>(
"get-field-variant-status",
fieldPathWithIndex
fieldMetadata
srinad007 marked this conversation as resolved.
Show resolved Hide resolved
);
return result;
} catch (error) {
Expand Down
79 changes: 45 additions & 34 deletions src/visualBuilder/components/FieldToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ import {
IVariantStatus,
} from "./FieldRevert/FieldRevertComponent";

export type FieldDetails = Pick<VisualBuilderCslpEventDetails, "editableElement" | "fieldMetadata">;
export type FieldDetails = Pick<
VisualBuilderCslpEventDetails,
"editableElement" | "fieldMetadata"
>;

const TOOLTIP_TOP_EDGE_BUFFER = 96;

interface MultipleFieldToolbarProps {
eventDetails: VisualBuilderCslpEventDetails;
hideOverlay: () => void;
};
}

function handleReplaceAsset(fieldMetadata: CslpData) {
// TODO avoid sending whole fieldMetadata
Expand Down Expand Up @@ -90,17 +93,19 @@ function handleEdit(fieldMetadata: CslpData) {

function handleFormFieldFocus(eventDetails: VisualBuilderCslpEventDetails) {
const { editableElement, fieldMetadata, cslpData } = eventDetails;
visualBuilderPostMessage?.send(
VisualBuilderPostMessageEvents.TOGGLE_FORM,
{
visualBuilderPostMessage
?.send(VisualBuilderPostMessageEvents.TOGGLE_FORM, {
fieldMetadata,
cslpData,
}
).then(() => {
visualBuilderPostMessage?.send(VisualBuilderPostMessageEvents.FOCUS_FIELD, {
DOMEditStack: getDOMEditStack(editableElement),
})
.then(() => {
visualBuilderPostMessage?.send(
VisualBuilderPostMessageEvents.FOCUS_FIELD,
{
DOMEditStack: getDOMEditStack(editableElement),
}
);
});
});
}

function FieldToolbarComponent(
Expand Down Expand Up @@ -128,14 +133,11 @@ function FieldToolbarComponent(
let fieldType = null;
let isWholeMultipleField = false;

if(fieldSchema) {
const { isDisabled } = isFieldDisabled(
fieldSchema,
{
editableElement: targetElement,
fieldMetadata
}
);
if (fieldSchema) {
const { isDisabled } = isFieldDisabled(fieldSchema, {
editableElement: targetElement,
fieldMetadata,
});

// field is disabled, no actions needed
if (isDisabled) {
Expand All @@ -149,8 +151,9 @@ function FieldToolbarComponent(
Icon = fieldIcons[fieldType];

isMultiple = fieldSchema.multiple || false;
if(fieldType === FieldDataType.REFERENCE)
isMultiple = (fieldSchema as IReferenceContentTypeSchema).field_metadata.ref_multiple;
if (fieldType === FieldDataType.REFERENCE)
isMultiple = (fieldSchema as IReferenceContentTypeSchema)
.field_metadata.ref_multiple;

// field is multiple but an instance is not selected
// instead the whole field (all instances) is selected.
Expand All @@ -159,12 +162,16 @@ function FieldToolbarComponent(
// cannot rely on -1 index, as the non-negative index then refers to the index of
// the featured_blogs block in page_components
// It is not needed except taxanomy.
isWholeMultipleField = isMultiple &&
(fieldMetadata.fieldPathWithIndex ===
fieldMetadata.instance.fieldPathWithIndex ||
fieldMetadata.multipleFieldMetadata?.index === -1);

if (DEFAULT_MULTIPLE_FIELDS.includes(fieldType) && isWholeMultipleField) {
isWholeMultipleField =
isMultiple &&
(fieldMetadata.fieldPathWithIndex ===
fieldMetadata.instance.fieldPathWithIndex ||
fieldMetadata.multipleFieldMetadata?.index === -1);

if (
DEFAULT_MULTIPLE_FIELDS.includes(fieldType) &&
isWholeMultipleField
) {
return null;
}
}
Expand Down Expand Up @@ -302,21 +309,25 @@ function FieldToolbarComponent(
if (fieldSchema) {
setFieldSchema(fieldSchema);
}
const variantStatus = await getFieldVariantStatus(
fieldMetadata.fieldPathWithIndex
);
const variantStatus = await getFieldVariantStatus(fieldMetadata);
setFieldVariantStatus(variantStatus ?? BASE_VARIANT_STATUS);
}
fetchFieldSchema();
}, [fieldMetadata]);

useEffect(() => {
visualBuilderPostMessage?.on(VisualBuilderPostMessageEvents.DELETE_INSTANCE, (args: { data: { path: string } }) => {
if(args.data?.path === fieldMetadata.instance.fieldPathWithIndex){
props.hideOverlay()
visualBuilderPostMessage?.on(
VisualBuilderPostMessageEvents.DELETE_INSTANCE,
(args: { data: { path: string } }) => {
if (
args.data?.path ===
fieldMetadata.instance.fieldPathWithIndex
) {
props.hideOverlay();
}
}
})
}, [])
);
}, []);

const multipleFieldToolbarButtonClasses = classNames(
"visual-builder__button visual-builder__button--secondary",
Expand Down
Loading