Skip to content

Commit

Permalink
Use updater function in DOM API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Dec 16, 2022
1 parent 8c10a84 commit 1f7779e
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ function ConnectionEditorContent<P>({
className,
connectionNode,
}: ConnectionEditorContentProps<P>) {
const { dom } = useDom();
const domApi = useDomApi();

const handleConnectionChange = React.useCallback(
(connectionParams: P | null) => {
const updatedDom = appDom.setNodeNamespacedProp(
dom,
connectionNode,
'attributes',
'params',
appDom.createSecret(connectionParams),
domApi.update((draft) =>
appDom.setNodeNamespacedProp(
draft,
connectionNode,
'attributes',
'params',
appDom.createSecret(connectionParams),
),
);
domApi.update(updatedDom);
},
[connectionNode, dom, domApi],
[connectionNode, domApi],
);

const dataSourceId = connectionNode.attributes.dataSource.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export default function CreateCodeComponentDialog({
});
const appNode = appDom.getApp(dom);

const updatedDom = appDom.addNode(dom, newNode, appNode, 'codeComponents');
domApi.update(updatedDom);
domApi.update((draft) => appDom.addNode(draft, newNode, appNode, 'codeComponents'));

onClose();
navigate(`/app/${appId}/codeComponents/${newNode.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ export default function CreateConnectionDialog({
});
const appNode = appDom.getApp(dom);

const updatedDom = appDom.addNode(dom, newNode, appNode, 'connections');
domApi.update(updatedDom);
domApi.update((draft) => appDom.addNode(draft, newNode, appNode, 'connections'));

onClose();
navigate(`/app/${appId}/connections/${newNode.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export default function CreatePageDialog({
});
const appNode = appDom.getApp(dom);

const updatedDom = appDom.addNode(dom, newNode, appNode, 'pages');
domApi.update(updatedDom);
domApi.update((draft) => appDom.addNode(draft, newNode, appNode, 'pages'));

onClose();
navigate(`/app/${appId}/pages/${newNode.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ export default function HierarchyExplorer({ appId, className }: HierarchyExplore
}
}

const updatedDom = appDom.removeNode(dom, nodeId);
domApi.update(updatedDom);
domApi.update((draft) => appDom.removeNode(draft, nodeId));

if (redirectAfterDelete) {
navigate(redirectAfterDelete);
Expand All @@ -250,15 +249,17 @@ export default function HierarchyExplorer({ appId, className }: HierarchyExplore
const handleDuplicateNode = React.useCallback(
(nodeId: NodeId) => {
const node = appDom.getNode(dom, nodeId);
invariant(
node.parentId && node.parentProp,
'Duplication should never be called on nodes that are not placed in the dom',
);

const fragment = appDom.cloneFragment(dom, nodeId);

const updatedDom = appDom.addFragment(dom, fragment, node.parentId, node.parentProp);
domApi.update(updatedDom);
domApi.update((draft) => {
invariant(
node.parentId && node.parentProp,
'Duplication should never be called on nodes that are not placed in the dom',
);

return appDom.addFragment(draft, fragment, node.parentId, node.parentProp);
});

const newNode = appDom.getNode(fragment, fragment.root);
const editorLink = getLinkToNodeEditor(appId, newNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { ArgTypeDefinition, BindableAttrValue } from '@mui/toolpad-core';
import { Alert } from '@mui/material';
import * as appDom from '../../../appDom';
import { useDom, useDomApi } from '../../DomLoader';
import { useDomApi } from '../../DomLoader';
import BindableEditor from './BindableEditor';
import { usePageEditorState } from './PageEditorProvider';
import { getDefaultControl } from '../../propertyControls';
Expand All @@ -22,15 +22,15 @@ export default function NodeAttributeEditor<P extends object>({
argType,
props,
}: NodeAttributeEditorProps<P>) {
const { dom } = useDom();
const domApi = useDomApi();

const handlePropChange = React.useCallback(
(newValue: BindableAttrValue<unknown> | null) => {
const updatedDom = appDom.setNodeNamespacedProp(dom, node, namespace as any, name, newValue);
domApi.update(updatedDom);
domApi.update((draft) =>
appDom.setNodeNamespacedProp(draft, node, namespace as any, name, newValue),
);
},
[dom, node, namespace, name, domApi],
[node, namespace, name, domApi],
);

const propValue: BindableAttrValue<unknown> | null = (node as any)[namespace]?.[name] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ function PageModuleEditorDialog({ pageNodeId, open, onClose }: PageModuleEditorD
const pretty = tryFormat(input);
setInput(pretty);

const updatedDom = appDom.setNodeNamespacedProp(
dom,
page,
'attributes',
'module',
appDom.createConst(pretty),
domApi.update((draft) =>
appDom.setNodeNamespacedProp(draft, page, 'attributes', 'module', appDom.createConst(pretty)),
);
domApi.update(updatedDom);
}, [dom, domApi, input, page]);
}, [domApi, input, page]);

const handleSaveButton = React.useCallback(() => {
handleSave();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ export default function QueryEditor() {
if (appDom.nodeExists(dom, node.id)) {
domApi.saveNode(node);
} else {
const updatedDom = appDom.addNode(dom, node, page, 'queries');
domApi.update(updatedDom);
domApi.update((draft) => appDom.addNode(draft, node, page, 'queries'));
}
setDialogState({ node, isDraft: false });
},
Expand All @@ -142,12 +141,11 @@ export default function QueryEditor() {

const handleDeleteNode = React.useCallback(
(nodeId: NodeId) => {
const updatedDom = appDom.removeNode(dom, nodeId);
domApi.update(updatedDom);
domApi.update((draft) => appDom.removeNode(draft, nodeId));

handleEditStateDialogClose();
},
[dom, domApi, handleEditStateDialogClose],
[domApi, handleEditStateDialogClose],
);

const handleRemove = React.useCallback(
Expand Down
Loading

0 comments on commit 1f7779e

Please sign in to comment.