Skip to content

Commit 4c9d670

Browse files
authored
Merge pull request #514 from contentstack/VB-530-support-to-link-unlinked-ct-to-variant-group
2 parents 1b594a6 + 388370e commit 4c9d670

12 files changed

Lines changed: 909 additions & 148 deletions

src/visualBuilder/components/__test__/fieldLabelWrapper.test.tsx

Lines changed: 182 additions & 94 deletions
Large diffs are not rendered by default.

src/visualBuilder/components/fieldLabelWrapper.tsx

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { extractDetailsFromCslp } from "../../cslp";
44
import { CslpData } from "../../cslp/types/cslp.types";
55
import { VisualBuilderCslpEventDetails } from "../types/visualBuilder.types";
66
import { FieldSchemaMap } from "../utils/fieldSchemaMap";
7-
import { isFieldDisabled } from "../utils/isFieldDisabled";
7+
import { DisableReason, isFieldDisabled } from "../utils/isFieldDisabled";
88
import visualBuilderPostMessage from "../utils/visualBuilderPostMessage";
99
import { CaretIcon, CaretRightIcon, InfoIcon } from "./icons";
1010
import { LoadingIcon } from "./icons/loading";
@@ -18,6 +18,8 @@ import { ContentTypeIcon } from "./icons";
1818
import { ToolbarTooltip } from "./Tooltip";
1919
import { fetchEntryPermissionsAndStageDetails } from "../utils/fetchEntryPermissionsAndStageDetails";
2020
import { VariantIndicator } from "./VariantIndicator";
21+
import { handleRevalidateFieldData } from "../eventManager/useRevalidateFieldDataPostMessageEvent";
22+
import { RESULT_TYPES } from "../utils/constants";
2123

2224
interface ReferenceParentMap {
2325
[entryUid: string]: {
@@ -129,7 +131,7 @@ function FieldLabelWrapperComponent(
129131
getReferenceParentMap()
130132
]);
131133
const entryUid = props.fieldMetadata.entry_uid;
132-
134+
133135
const referenceData = referenceParentMap[entryUid];
134136
const isReference = !!referenceData;
135137

@@ -173,6 +175,38 @@ function FieldLabelWrapperComponent(
173175
entryWorkflowStageDetails,
174176
);
175177

178+
const handleLinkVariant = async () => {
179+
try {
180+
if (fieldSchema.field_metadata?.canLinkVariant) {
181+
const result = await visualBuilderPostMessage?.send<{
182+
type: typeof RESULT_TYPES.SUCCESS | typeof RESULT_TYPES.ERROR;
183+
message: string;
184+
}>(
185+
VisualBuilderPostMessageEvents.OPEN_LINK_VARIANT_MODAL,
186+
{
187+
contentTypeUid:
188+
props.fieldMetadata.content_type_uid,
189+
}
190+
);
191+
192+
// If the modal was closed or linking failed, do nothing
193+
if (!result || result.type === RESULT_TYPES.ERROR) {
194+
return;
195+
}
196+
197+
// If linking was successful and requires revalidation, revalidate
198+
if (result.type === RESULT_TYPES.SUCCESS) {
199+
await handleRevalidateFieldData();
200+
}
201+
}
202+
} catch (error) {
203+
console.error(
204+
"Error in link variant modal flow:",
205+
error
206+
);
207+
}
208+
};
209+
176210
const currentFieldDisplayName =
177211
displayNames?.[props.fieldMetadata.cslpValue] ??
178212
fieldSchema.display_name;
@@ -190,8 +224,30 @@ function FieldLabelWrapperComponent(
190224
"visual-builder__tooltip--persistent"
191225
]
192226
)}
193-
data-tooltip={reason}
227+
data-tooltip={!reason?.includes(DisableReason.CanLinkVariant)
228+
? reason
229+
: undefined}
194230
>
231+
{reason
232+
.includes(DisableReason.CanLinkVariant) && (
233+
<div
234+
className={visualBuilderStyles()["visual-builder__custom-tooltip"]}
235+
onClick={handleLinkVariant}
236+
>
237+
{(() => {
238+
const [before, after] = reason.split(
239+
DisableReason.UnderlinedAndClickableWord
240+
);
241+
return (
242+
<>
243+
{before}
244+
<span style={{ textDecoration: "underline" }}>{DisableReason.UnderlinedAndClickableWord}</span>
245+
{after}
246+
</>
247+
);
248+
})()}
249+
</div>
250+
)}
195251
<InfoIcon />
196252
</div>
197253
) : hasParentPaths ? (
@@ -305,11 +361,11 @@ function FieldLabelWrapperComponent(
305361
>
306362
{
307363
currentField.isReference && !dataLoading && !error ?
308-
<div
309-
className={classNames(
310-
"visual-builder__reference-icon-container",
364+
<div
365+
className={classNames(
366+
"visual-builder__reference-icon-container",
311367
visualBuilderStyles()["visual-builder__reference-icon-container"]
312-
)}
368+
)}
313369
>
314370
<div
315371
className={classNames(

0 commit comments

Comments
 (0)