fixes: multi-user updates: adding or removing

This commit is contained in:
arpitnath 2023-11-02 04:39:20 +05:30
parent d3aa1c4899
commit 965882d31c
2 changed files with 38 additions and 4 deletions

View file

@ -110,6 +110,7 @@ const EditorComponent = (props) => {
showLeftSidebar,
queryConfirmationList,
currentPageId,
currentSessionId,
} = useEditorState();
const dataQueries = useDataQueries();
@ -142,7 +143,7 @@ const EditorComponent = (props) => {
const [showPageDeletionConfirmation, setShowPageDeletionConfirmation] = useState(null);
const [isDeletingPage, setIsDeletingPage] = useState(false);
const [currentSessionId, setCurrentSessionId] = useState(null);
// const [currentSessionId, setCurrentSessionId] = useState(null);
const [undoStack, setUndoStack] = useState([]);
const [redoStack, setRedoStack] = useState([]);
@ -204,7 +205,7 @@ const EditorComponent = (props) => {
$componentDidMount();
setCurrentSessionId(() => uuid());
// setCurrentSessionId(() => uuid());
// 6. Unsubscribe from the observable when the component is unmounted
return () => {
@ -369,6 +370,8 @@ const EditorComponent = (props) => {
skipAutoSave: true,
skipYmapUpdate: true,
currentSessionId: ymapUpdates.currentSessionId,
componentAdding: ymapUpdates.componentAdding,
componentDeleting: ymapUpdates.componentDeleting,
});
});
};
@ -791,7 +794,36 @@ const EditorComponent = (props) => {
const copyOfAppDefinition = JSON.parse(JSON.stringify(appDefinition));
if (opts?.skipYmapUpdate && opts?.currentSessionId !== currentSessionId) {
updatedAppDefinition = newDefinition;
updatedAppDefinition = produce(copyOfAppDefinition, (draft) => {
const _currentPageId = useEditorStore.getState().currentPageId;
if (opts?.componentDeleting) {
const currentPageComponentIds = Object.keys(copyOfAppDefinition.pages[_currentPageId]?.components);
const newComponentIds = Object.keys(newDefinition.pages[_currentPageId]?.components);
const finalComponents = _.omit(
draft?.pages[_currentPageId]?.components,
_.difference(currentPageComponentIds, newComponentIds)
);
console.log('---arpit appDefinitionChanged--- ymap only', {
finalComponents,
});
draft.pages[_currentPageId].components = finalComponents;
} else if (opts?.componentAdding) {
const currentPageComponentIds = Object.keys(copyOfAppDefinition.pages[_currentPageId]?.components);
const newComponentIds = Object.keys(newDefinition.pages[_currentPageId]?.components);
const finalComponents = _.pick(
newDefinition?.pages[_currentPageId]?.components,
_.difference(newComponentIds, currentPageComponentIds)
);
draft.pages[_currentPageId].components = finalComponents;
} else {
Object.assign(draft, newDefinition);
}
});
} else {
updatedAppDefinition = produce(copyOfAppDefinition, (draft) => {
if (_.isEmpty(draft)) return;
@ -868,6 +900,7 @@ const EditorComponent = (props) => {
editingVersionId: editingVersion?.id,
currentSessionId,
areOthersOnSameVersionAndPage,
opts,
});
}
};

View file

@ -1,5 +1,5 @@
import { create, zustandDevTools } from './utils';
import { v4 as uuid } from 'uuid';
const STORE_NAME = 'Editor';
const ACTIONS = {
@ -39,6 +39,7 @@ const initialState = {
showLeftSidebar: true,
queryConfirmationList: [],
currentPageId: null,
currentSessionId: uuid(),
};
export const useEditorStore = create(