-
Notifications
You must be signed in to change notification settings - Fork 1
feat: adding post messages to link unlinked ct to variant group #514
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
Open
karancs06
wants to merge
5
commits into
develop_v4
Choose a base branch
from
VB-530-support-to-link-unlinked-ct-to-variant-group
base: develop_v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30975d3
feat: adding post messages to link unlinked ct to variant group
karancs06 0817211
fix: updated console
karancs06 6936fef
feat: test cases added
karancs06 17e8f5d
fix: string and focus unfocus on variant link
karancs06 8f2da6e
fix: addressed changes
karancs06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
265 changes: 174 additions & 91 deletions
265
src/visualBuilder/components/__test__/fieldLabelWrapper.test.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
src/visualBuilder/eventManager/__test__/useRevalidateFieldDataPostMessageEvent.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import { vi, describe, it, expect, beforeEach } from "vitest"; | ||
import { VisualBuilder } from "../.."; | ||
import { FieldSchemaMap } from "../../utils/fieldSchemaMap"; | ||
import { getFieldData } from "../../utils/getFieldData"; | ||
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage"; | ||
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types"; | ||
import { useRevalidateFieldDataPostMessageEvent } from "../useRevalidateFieldDataPostMessageEvent"; | ||
|
||
// Mock dependencies | ||
vi.mock("../../utils/fieldSchemaMap", () => ({ | ||
FieldSchemaMap: { | ||
clearContentTypeSchema: vi.fn(), | ||
clear: vi.fn(), | ||
getFieldSchema: vi.fn(), | ||
}, | ||
})); | ||
|
||
vi.mock("../../utils/getFieldData", () => ({ | ||
getFieldData: vi.fn(), | ||
})); | ||
|
||
vi.mock("../../utils/visualBuilderPostMessage", () => ({ | ||
default: { | ||
on: vi.fn(), | ||
}, | ||
})); | ||
|
||
vi.mock("../../../cslp", () => ({ | ||
extractDetailsFromCslp: vi.fn(), | ||
})); | ||
|
||
// Mock window.location.reload | ||
Object.defineProperty(window, "location", { | ||
value: { | ||
reload: vi.fn(), | ||
}, | ||
writable: true, | ||
}); | ||
|
||
describe("useRevalidateFieldDataPostMessageEvent", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
|
||
// Reset VisualBuilder global state | ||
VisualBuilder.VisualBuilderGlobalState = { | ||
// @ts-expect-error mocking only required properties | ||
value: { | ||
previousHoveredTargetDOM: null, | ||
previousSelectedEditableDOM: null, | ||
}, | ||
}; | ||
}); | ||
|
||
it("should register post message event listener", () => { | ||
useRevalidateFieldDataPostMessageEvent(); | ||
|
||
expect(visualBuilderPostMessage.on).toHaveBeenCalledWith( | ||
VisualBuilderPostMessageEvents.REVALIDATE_FIELD_DATA, | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
describe("handleRevalidateFieldData", () => { | ||
let mockHandleRevalidateFieldData: any; | ||
|
||
beforeEach(() => { | ||
useRevalidateFieldDataPostMessageEvent(); | ||
mockHandleRevalidateFieldData = (visualBuilderPostMessage.on as any) | ||
.mock.calls[0][1]; | ||
}); | ||
|
||
it("should revalidate specific field when hovered element exists", async () => { | ||
const mockElement = document.createElement("div"); | ||
mockElement.setAttribute("data-cslp", "content_type.entry.field"); | ||
|
||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
mockElement; | ||
|
||
const mockExtractDetailsFromCslp = await import("../../../cslp"); | ||
vi.mocked( | ||
mockExtractDetailsFromCslp.extractDetailsFromCslp | ||
).mockReturnValue({ | ||
content_type_uid: "test_content_type", | ||
entry_uid: "test_entry", | ||
locale: "en-us", | ||
fieldPath: "test_field", | ||
fieldPathWithIndex: "test_field", | ||
}); | ||
|
||
vi.mocked(FieldSchemaMap.getFieldSchema).mockResolvedValue({ | ||
test: "schema", | ||
}); | ||
vi.mocked(getFieldData).mockResolvedValue({ test: "data" }); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clearContentTypeSchema).toHaveBeenCalledWith( | ||
"test_content_type" | ||
); | ||
expect(FieldSchemaMap.getFieldSchema).toHaveBeenCalledWith( | ||
"test_content_type", | ||
"test_field" | ||
); | ||
expect(getFieldData).toHaveBeenCalledWith( | ||
{ | ||
content_type_uid: "test_content_type", | ||
entry_uid: "test_entry", | ||
locale: "en-us", | ||
}, | ||
"test_field" | ||
); | ||
expect(window.location.reload).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fallback to focused element when no hovered element", async () => { | ||
const mockElement = document.createElement("div"); | ||
mockElement.setAttribute("data-cslp", "content_type.entry.field"); | ||
|
||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
null; | ||
VisualBuilder.VisualBuilderGlobalState.value.previousSelectedEditableDOM = | ||
mockElement; | ||
|
||
const mockExtractDetailsFromCslp = await import("../../../cslp"); | ||
vi.mocked( | ||
mockExtractDetailsFromCslp.extractDetailsFromCslp | ||
).mockReturnValue({ | ||
content_type_uid: "test_content_type", | ||
entry_uid: "test_entry", | ||
locale: "en-us", | ||
fieldPath: "test_field", | ||
fieldPathWithIndex: "test_field", | ||
}); | ||
|
||
vi.mocked(FieldSchemaMap.getFieldSchema).mockResolvedValue({ | ||
test: "schema", | ||
}); | ||
vi.mocked(getFieldData).mockResolvedValue({ test: "data" }); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clearContentTypeSchema).toHaveBeenCalledWith( | ||
"test_content_type" | ||
); | ||
}); | ||
|
||
it("should clear all field schema cache when no target element", async () => { | ||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
null; | ||
VisualBuilder.VisualBuilderGlobalState.value.previousSelectedEditableDOM = | ||
null; | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clear).toHaveBeenCalled(); | ||
expect(window.location.reload).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should refresh iframe when field schema validation fails", async () => { | ||
const mockElement = document.createElement("div"); | ||
mockElement.setAttribute("data-cslp", "content_type.entry.field"); | ||
|
||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
mockElement; | ||
|
||
const mockExtractDetailsFromCslp = await import("../../../cslp"); | ||
vi.mocked( | ||
mockExtractDetailsFromCslp.extractDetailsFromCslp | ||
).mockReturnValue({ | ||
content_type_uid: "test_content_type", | ||
entry_uid: "test_entry", | ||
locale: "en-us", | ||
fieldPath: "test_field", | ||
fieldPathWithIndex: "test_field", | ||
}); | ||
|
||
vi.mocked(FieldSchemaMap.getFieldSchema).mockResolvedValue(null); | ||
vi.mocked(getFieldData).mockResolvedValue(null); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clear).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should refresh iframe when clearing cache fails", async () => { | ||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
null; | ||
VisualBuilder.VisualBuilderGlobalState.value.previousSelectedEditableDOM = | ||
null; | ||
|
||
vi.mocked(FieldSchemaMap.clear).mockImplementation(() => { | ||
throw new Error("Cache clear failed"); | ||
}); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clear).toHaveBeenCalled(); | ||
expect(window.location.reload).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should refresh iframe when any error occurs", async () => { | ||
const mockElement = document.createElement("div"); | ||
mockElement.setAttribute("data-cslp", "content_type.entry.field"); | ||
|
||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
mockElement; | ||
|
||
const mockExtractDetailsFromCslp = await import("../../../cslp"); | ||
vi.mocked( | ||
mockExtractDetailsFromCslp.extractDetailsFromCslp | ||
).mockImplementation(() => { | ||
throw new Error("CSLP parsing failed"); | ||
}); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(window.location.reload).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle elements without data-cslp attribute", async () => { | ||
const mockElement = document.createElement("div"); | ||
// No data-cslp attribute | ||
|
||
VisualBuilder.VisualBuilderGlobalState.value.previousHoveredTargetDOM = | ||
mockElement; | ||
|
||
// Reset the clear mock to not throw error for this test | ||
vi.mocked(FieldSchemaMap.clear).mockReset(); | ||
vi.mocked(FieldSchemaMap.clear).mockImplementation(() => { | ||
// Successful clear - no error | ||
}); | ||
|
||
await mockHandleRevalidateFieldData(); | ||
|
||
expect(FieldSchemaMap.clear).toHaveBeenCalled(); | ||
expect(window.location.reload).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
src/visualBuilder/eventManager/useRevalidateFieldDataPostMessageEvent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { VisualBuilder } from ".."; | ||
import { extractDetailsFromCslp } from "../../cslp"; | ||
import { FieldSchemaMap } from "../utils/fieldSchemaMap"; | ||
import { getFieldData } from "../utils/getFieldData"; | ||
import visualBuilderPostMessage from "../utils/visualBuilderPostMessage"; | ||
import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types"; | ||
|
||
/** | ||
* Revalidates field data and schema after variant linking operations. | ||
* First tries to revalidate specific hovered field, then falls back to clearing all schemas, | ||
* and finally refreshes the iframe if all else fails. | ||
*/ | ||
async function handleRevalidateFieldData(): Promise<void> { | ||
try { | ||
// Get the currently hovered or focused field | ||
const hoveredElement = | ||
VisualBuilder.VisualBuilderGlobalState.value | ||
.previousHoveredTargetDOM; | ||
const focusedElement = | ||
VisualBuilder.VisualBuilderGlobalState.value | ||
.previousSelectedEditableDOM; | ||
|
||
// Prefer hovered element, fallback to focused element | ||
const targetElement = hoveredElement || focusedElement; | ||
|
||
if (targetElement) { | ||
const cslp = targetElement.getAttribute("data-cslp"); | ||
if (cslp) { | ||
const fieldMetadata = extractDetailsFromCslp(cslp); | ||
|
||
// Try to revalidate specific field schema and data | ||
try { | ||
// Clear the entire content type schema from cache to force fresh fetch | ||
FieldSchemaMap.clearContentTypeSchema( | ||
fieldMetadata.content_type_uid | ||
); | ||
|
||
// Fetch fresh field schema and data | ||
const [fieldSchema, fieldData] = await Promise.all([ | ||
FieldSchemaMap.getFieldSchema( | ||
fieldMetadata.content_type_uid, | ||
fieldMetadata.fieldPath | ||
), | ||
csAyushDubey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
getFieldData( | ||
{ | ||
content_type_uid: | ||
fieldMetadata.content_type_uid, | ||
entry_uid: fieldMetadata.entry_uid, | ||
locale: fieldMetadata.locale, | ||
}, | ||
fieldMetadata.fieldPathWithIndex | ||
), | ||
]); | ||
|
||
if (fieldSchema && fieldData) { | ||
return; | ||
} | ||
} catch (fieldError) { | ||
console.warn( | ||
"Failed to revalidate content type:", | ||
fieldMetadata.content_type_uid, | ||
fieldError | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Fallback 1: Clear all field schema cache | ||
try { | ||
FieldSchemaMap.clear(); | ||
return; | ||
} catch (clearError) { | ||
console.error("Failed to clear field schema cache:", clearError); | ||
} | ||
|
||
// Fallback 2: Refresh the entire iframe | ||
window.location.reload(); | ||
} catch (error) { | ||
console.error("Error handling revalidate field data:", error); | ||
// Final fallback - refresh the page | ||
window.location.reload(); | ||
} | ||
} | ||
|
||
export function useRevalidateFieldDataPostMessageEvent(): void { | ||
visualBuilderPostMessage?.on( | ||
VisualBuilderPostMessageEvents.REVALIDATE_FIELD_DATA, | ||
handleRevalidateFieldData | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.