Skip to content

Commit

Permalink
feat(AstBuilder): add save method
Browse files Browse the repository at this point in the history
  • Loading branch information
balzdur committed Aug 31, 2023
1 parent ec3b42a commit dda24da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/app-builder/src/models/ast-node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type NodeDto } from '@marble-api';
import * as R from 'remeda';

export interface AstNode {
export type AstNode = {
name: string | null;
constant?: ConstantType;
children: AstNode[];
namedChildren: Record<string, AstNode>;
}
};

export const undefinedAstNodeName = 'Undefined';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ export async function action({ request, params }: ActionArgs) {
try {
const ruleId = fromParams(params, 'ruleId');

const expression = (await request.json()) as {
astNode: AstNode;
};
const astNode = (await request.json()) as AstNode;

await editor.saveRule({
ruleId,
astNode: expression.astNode,
astNode,
});

setToastMessage(session, {
Expand All @@ -109,7 +107,7 @@ export async function action({ request, params }: ActionArgs) {
{
success: true as const,
error: null,
values: expression,
astNode,
},
{ headers: { 'Set-Cookie': await commitSession(session) } }
);
Expand All @@ -122,7 +120,7 @@ export async function action({ request, params }: ActionArgs) {
{
success: false as const,
error: null,
values: null,
astNode: null,
},
{ headers: { 'Set-Cookie': await commitSession(session) } }
);
Expand All @@ -146,7 +144,7 @@ export default function RuleEdit() {
identifiers,
operators,
onSave: (astNodeToSave: AstNode) => {
fetcher.submit(JSON.stringify(astNodeToSave), {
fetcher.submit(astNodeToSave, {
method: 'PATCH',
encType: 'application/json',
});
Expand Down Expand Up @@ -183,7 +181,13 @@ export default function RuleEdit() {
<Paper.Container scrollable={false}>
<AstBuilder builder={astEditor} />
<div className="flex flex-row justify-end">
<Button type="submit" className="w-fit">
<Button
type="submit"
className="w-fit"
onClick={() => {
astEditor.save();
}}
>
{t('common:save')}
</Button>
</div>
Expand Down
9 changes: 9 additions & 0 deletions packages/app-builder/src/services/editor/ast-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ export interface AstBuilder {
setOperator(nodeId: string, name: string): void;
appendChild(nodeId: string, childAst: AstNode): void;
remove(nodeId: string): void;
save(): void;
}

export function useAstBuilder({
ast,
validation,
identifiers,
operators,
onSave,
}: {
ast: AstNode;
validation: NodeEvaluation;
Expand Down Expand Up @@ -211,6 +213,12 @@ export function useAstBuilder({
// Todo: debonced save
}, []);

const save = useCallback(() => {
onSave(adaptAstNodeFromEditorViewModel(astViewModel));

// Todo: debonced save
}, [astViewModel, onSave]);

return {
astViewModel,
identifiers,
Expand All @@ -220,5 +228,6 @@ export function useAstBuilder({
setOperator,
appendChild,
remove,
save,
};
}

0 comments on commit dda24da

Please sign in to comment.