Skip to content

Commit

Permalink
[core] NodeReference type for references to nodes (#720)
Browse files Browse the repository at this point in the history
* `NodeReference` type for references to nodes

* `NodeReference` type for references to nodes

* Abstract implementation details
  • Loading branch information
bharatkashyap authored Aug 2, 2022
1 parent f00a26b commit 5409576
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/toolpad-app/src/appDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { generateKeyBetween } from 'fractional-indexing';
import cuid from 'cuid';
import {
NodeId,
NodeReference,
ConstantAttrValue,
BindableAttrValue,
BindableAttrValues,
Expand Down Expand Up @@ -106,7 +107,7 @@ export interface QueryNode<Q = any, P = any> extends AppDomNodeBase {
readonly params?: BindableAttrValues<P>;
readonly attributes: {
readonly dataSource?: ConstantAttrValue<string>;
readonly connectionId: ConstantAttrValue<NodeId>;
readonly connectionId: ConstantAttrValue<NodeReference>;
readonly query: ConstantAttrValue<Q>;
readonly transform?: ConstantAttrValue<string>;
readonly transformEnabled?: ConstantAttrValue<boolean>;
Expand Down Expand Up @@ -803,3 +804,10 @@ export function createRenderTree(dom: AppDom): RenderTree {
nodes: filterValues(dom.nodes, (node) => frontendNodes.has(node.type)) as RenderTreeNodes,
};
}

export function ref(nodeId: NodeId): NodeReference {
return { $$ref: nodeId };
}
export function deref(nodeRef: NodeReference): NodeId {
return nodeRef.$$ref;
}
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function RenderedNodeContent({ node, childNodes, Component }: RenderedNodeConten
const handler = () => {
const { page } = action.value;
if (page) {
navigateToPage(page);
navigateToPage(appDom.deref(page));
}
};

Expand Down
5 changes: 4 additions & 1 deletion packages/toolpad-app/src/server/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ export async function execQuery<P, Q>(
);
}

const connectionParams = await getConnectionParams<P>(appId, query.attributes.connectionId.value);
const connectionParams = await getConnectionParams<P>(
appId,
appDom.deref(query.attributes.connectionId.value),
);

const transformEnabled = query.attributes.transformEnabled?.value;
const transform = query.attributes.transform?.value;
Expand Down
7 changes: 5 additions & 2 deletions packages/toolpad-app/src/toolpad/AppEditor/BindingEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ function NavigationActionEditor({ value, onChange }: NavigationActionEditorProps

const handlePageChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
onChange({ type: 'navigationAction', value: { page: event.target.value as NodeId } });
onChange({
type: 'navigationAction',
value: { page: appDom.ref(event.target.value as NodeId) },
});
},
[onChange],
);
Expand All @@ -206,7 +209,7 @@ function NavigationActionEditor({ value, onChange }: NavigationActionEditorProps
sx={{ my: 3 }}
label="page"
select
value={value?.value?.page || ''}
value={value?.value?.page ? appDom.deref(value.value.page) : ''}
onChange={handlePageChange}
disabled={!hasPagesAvailable}
helperText={hasPagesAvailable ? null : 'No other pages available'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function ConnectionSelectorDialog<Q>({ open, onCreated, onClose }: DataSourceSel
const queryNode = appDom.createNode(dom, 'query', {
attributes: {
query: appDom.createConst(dataSource.getInitialQueryValue()),
connectionId: appDom.createConst(connectionId),
connectionId: appDom.createConst(appDom.ref(connectionId)),
dataSource: appDom.createConst(dataSourceId),
},
});
Expand Down Expand Up @@ -180,7 +180,7 @@ function QueryNodeEditorDialog<Q, P>({
}
}, [open, node]);

const connectionId = input.attributes.connectionId.value;
const connectionId = appDom.deref(input.attributes.connectionId.value);
const connection = appDom.getMaybeNode(dom, connectionId, 'connection');
const dataSourceId = input.attributes.dataSource?.value;
const dataSource = (dataSourceId && dataSources[dataSourceId]) || null;
Expand All @@ -189,7 +189,9 @@ function QueryNodeEditorDialog<Q, P>({
setInput((existing) =>
update(existing, {
attributes: update(existing.attributes, {
connectionId: newConnectionId ? appDom.createConst(newConnectionId) : undefined,
connectionId: newConnectionId
? appDom.createConst(appDom.ref(newConnectionId))
: undefined,
}),
}),
);
Expand Down Expand Up @@ -380,7 +382,7 @@ function QueryNodeEditorDialog<Q, P>({
<NodeNameEditor node={node} />
<ConnectionSelect
dataSource={dataSourceId}
value={input.attributes.connectionId.value || null}
value={appDom.deref(input.attributes.connectionId.value) || null}
onChange={handleConnectionChange}
/>
</Stack>
Expand Down
6 changes: 5 additions & 1 deletion packages/toolpad-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export type NodeId = Branded<string, 'NodeId'>;

export type BindingAttrValueFormat = 'stringLiteral' | 'default';

export interface NodeReference {
$$ref: NodeId;
}

export interface BoundExpressionAttrValue {
type: 'boundExpression';
value: string;
Expand Down Expand Up @@ -40,7 +44,7 @@ export interface JsExpressionAction {
export interface NavigationAction<P = any> {
type: 'navigationAction';
value: {
page: NodeId;
page: NodeReference;
parameters?: BindableAttrValues<P>;
};
}
Expand Down

0 comments on commit 5409576

Please sign in to comment.