Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AstBuilder): add save method #90

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required in order to be compatible with JSON type.

In short, interface do not have index type, and raise issue when passed as a JSON value.

(more info here)

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, {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we need to have a type compatible with JSON type declaration.

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();
}}
>
Comment on lines +184 to +190
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer use Form component

{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,
};
}