[appdef] - copy associated events for cloned components (#7634)

* fixes: Copying  component is not coping the events associated with the component.

* cleanup
This commit is contained in:
Arpit 2023-10-06 15:28:07 +05:30 committed by GitHub
parent d0895eb92c
commit be36d23c64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 4 deletions

View file

@ -79,7 +79,8 @@ const EditorComponent = (props) => {
const { socket } = createWebsocketConnection(props?.params?.id);
const mounted = useMounted();
const { updateState, updateAppDefinitionDiff, updateAppVersion, setIsSaving } = useAppDataActions();
const { updateState, updateAppDefinitionDiff, updateAppVersion, setIsSaving, createAppVersionEventHandlers } =
useAppDataActions();
const { updateEditorState, updateQueryConfirmationList } = useEditorActions();
const { setAppVersions } = useAppVersionActions();
@ -822,6 +823,49 @@ const EditorComponent = (props) => {
}
};
const cloneEventsForClonedComponents = (componentUpdateDiff, operation, componentMap) => {
function getKeyFromComponentMap(componentMap, newItem) {
for (const key in componentMap) {
if (componentMap.hasOwnProperty(key) && componentMap[key] === newItem) {
return key;
}
}
return null;
}
if (operation !== 'create') return;
const newComponentIds = Object.keys(componentUpdateDiff);
const mappedEvents = [];
newComponentIds.forEach((componentId) => {
const sourceComponentId = getKeyFromComponentMap(componentMap, componentId);
if (!sourceComponentId) return;
const componentEvents = events.filter((event) => event.sourceId === sourceComponentId);
mappedEvents.push(...componentEvents);
});
if (mappedEvents.length === 0) return;
return Promise.all(
mappedEvents.map((event) => {
const newEvent = {
event: {
...event?.event,
},
eventType: event?.target,
attachedTo: componentMap[event?.sourceId],
index: event?.index,
};
createAppVersionEventHandlers(newEvent);
})
);
};
const saveEditingVersion = (isUserSwitchedVersion = false) => {
if (isVersionReleased && !isUserSwitchedVersion) {
updateEditorState({
@ -863,6 +907,15 @@ const EditorComponent = (props) => {
isUpdatingEditorStateInProcess: false,
});
toast.error('App could not save.');
})
.finally(() => {
if (appDiffOptions?.cloningComponent) {
cloneEventsForClonedComponents(
updateDiff.updateDiff,
updateDiff.operation,
appDiffOptions?.cloningComponent
);
}
});
}

View file

@ -1317,7 +1317,7 @@ export const getComponentName = (currentState, id) => {
}
};
const updateNewComponents = (pageId, appDefinition, newComponents, updateAppDefinition) => {
const updateNewComponents = (pageId, appDefinition, newComponents, updateAppDefinition, componentMap, isCut) => {
const newAppDefinition = JSON.parse(JSON.stringify(appDefinition));
newAppDefinition.pages[pageId].components = {
@ -1325,7 +1325,16 @@ const updateNewComponents = (pageId, appDefinition, newComponents, updateAppDefi
...newComponents,
};
updateAppDefinition(newAppDefinition, { componentAdded: true, containerChanges: true });
const opts = {
componentAdded: true,
containerChanges: true,
};
if (!isCut) {
opts.cloningComponent = componentMap;
}
updateAppDefinition(newAppDefinition, opts);
};
export const cloneComponents = (
@ -1489,7 +1498,7 @@ export const addComponents = (pageId, appDefinition, appDefinitionChanged, paren
!isCloning && updateComponentLayout(pastedComponents, parentId, isCut);
updateNewComponents(pageId, appDefinition, finalComponents, appDefinitionChanged);
updateNewComponents(pageId, appDefinition, finalComponents, appDefinitionChanged, componentMap, isCut);
!isCloning && toast.success('Component pasted succesfully');
};