Skip to content

Commit

Permalink
feat: add support for DataStore actions (#365)
Browse files Browse the repository at this point in the history
* test: move workflow test to example schemas

* feat: add support for DataStore actions
  • Loading branch information
dpilch authored and alharris-at committed Feb 24, 2022
1 parent 6ac08d2 commit 63a3067
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`amplify render tests actions DataStore DataStoreCreate action 1`] = `
exports[`amplify render tests actions DataStore DataStoreCreateItem 1`] = `
Object {
"componentText": "/* eslint-disable */
import React from \\"react\\";
Expand Down Expand Up @@ -29,7 +29,7 @@ export default function CreateCustomerButton(
return (
/* @ts-ignore: TS2322 */
<Button
children=\\"Save\\"
children=\\"Create\\"
onClick={() => {
action.run();
}}
Expand All @@ -44,6 +44,95 @@ export default function CreateCustomerButton(
}
`;

exports[`amplify render tests actions DataStore DataStoreDeleteItem 1`] = `
Object {
"componentText": "/* eslint-disable */
import React from \\"react\\";
import {
EscapeHatchProps,
getOverrideProps,
useDataStoreDeleteAction,
} from \\"@aws-amplify/ui-react/internal\\";
import { Button, ButtonProps } from \\"@aws-amplify/ui-react\\";
import { Customer } from \\"../models\\";

export type DeleteCustomerButtonProps = React.PropsWithChildren<
Partial<ButtonProps> & {
overrides?: EscapeHatchProps | undefined | null;
}
>;
export default function DeleteCustomerButton(
props: DeleteCustomerButtonProps
): React.ReactElement {
const { overrides: overridesProp, ...rest } = props;
const overrides = { ...overridesProp };
const action = useDataStoreDeleteAction({
model: Customer,
id: \\"d9887268-47dd-4899-9568-db5809218751\\",
});
return (
/* @ts-ignore: TS2322 */
<Button
children=\\"Delete\\"
onClick={() => {
action.run();
}}
{...rest}
{...getOverrideProps(overrides, \\"DeleteCustomerButton\\")}
></Button>
);
}
",
"declaration": undefined,
"renderComponentToFilesystem": [Function],
}
`;

exports[`amplify render tests actions DataStore DataStoreUpdateItem 1`] = `
Object {
"componentText": "/* eslint-disable */
import React from \\"react\\";
import {
EscapeHatchProps,
getOverrideProps,
useDataStoreUpdateAction,
} from \\"@aws-amplify/ui-react/internal\\";
import { Button, ButtonProps } from \\"@aws-amplify/ui-react\\";
import { Customer } from \\"../models\\";

export type UpdateCustomerButtonProps = React.PropsWithChildren<
Partial<ButtonProps> & {
overrides?: EscapeHatchProps | undefined | null;
}
>;
export default function UpdateCustomerButton(
props: UpdateCustomerButtonProps
): React.ReactElement {
const { overrides: overridesProp, ...rest } = props;
const overrides = { ...overridesProp };
const action = useDataStoreUpdateAction({
model: Customer,
id: \\"d9887268-47dd-4899-9568-db5809218751\\",
fields: { firstName: \\"Din\\", lastName: \\"Djarin\\" },
});
return (
/* @ts-ignore: TS2322 */
<Button
children=\\"Update\\"
onClick={() => {
action.run();
}}
{...rest}
{...getOverrideProps(overrides, \\"UpdateCustomerButton\\")}
></Button>
);
}
",
"declaration": undefined,
"renderComponentToFilesystem": [Function],
}
`;

exports[`amplify render tests actions auth signs out 1`] = `
Object {
"componentText": "/* eslint-disable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,16 @@ describe('amplify render tests', () => {
});

describe('DataStore', () => {
it('DataStoreCreate action', () => {
expect(generateWithAmplifyRenderer('workflow/dataStoreCreateAction')).toMatchSnapshot();
it('DataStoreCreateItem', () => {
expect(generateWithAmplifyRenderer('workflow/dataStoreCreateItem')).toMatchSnapshot();
});

it('DataStoreUpdateItem', () => {
expect(generateWithAmplifyRenderer('workflow/dataStoreUpdateItem')).toMatchSnapshot();
});

it('DataStoreDeleteItem', () => {
expect(generateWithAmplifyRenderer('workflow/dataStoreDeleteItem')).toMatchSnapshot();
});
});
});
Expand Down
4 changes: 4 additions & 0 deletions packages/codegen-ui-react/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import { ImportCollection, ImportSource } from './imports';
enum Action {
'Amplify.Navigate' = 'Amplify.Navigate',
'Amplify.DataStoreCreateItem' = 'Amplify.DataStoreCreateItem',
'Amplify.DataStoreUpdateItem' = 'Amplify.DataStoreUpdateItem',
'Amplify.DataStoreDeleteItem' = 'Amplify.DataStoreDeleteItem',
'Amplify.AuthSignOut' = 'Amplify.AuthSignOut',
'Amplify.AuthUpdateUserAttributes' = 'Amplify.AuthUpdateUserAttributes',
}
Expand All @@ -48,6 +50,8 @@ export default Action;
export const ActionNameMapping: Partial<Record<Action, string>> = {
[Action['Amplify.Navigate']]: 'useNavigateAction',
[Action['Amplify.DataStoreCreateItem']]: 'useDataStoreCreateAction',
[Action['Amplify.DataStoreUpdateItem']]: 'useDataStoreUpdateAction',
[Action['Amplify.DataStoreDeleteItem']]: 'useDataStoreDeleteAction',
[Action['Amplify.AuthSignOut']]: 'useAuthSignOutAction',
[Action['Amplify.AuthUpdateUserAttributes']]: 'updateUserAttributesAction',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"properties": {
"children": {
"value": "Save"
"value": "Create"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"componentType": "Button",
"name": "DeleteCustomerButton",
"properties": {},
"events": {
"click": {
"action": "Amplify.DataStoreDeleteItem",
"parameters": {
"model": "Customer",
"id": {
"value": "d9887268-47dd-4899-9568-db5809218751"
}
}
}
},
"properties": {
"children": {
"value": "Delete"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"componentType": "Button",
"name": "UpdateCustomerButton",
"properties": {},
"events": {
"click": {
"action": "Amplify.DataStoreUpdateItem",
"parameters": {
"model": "Customer",
"id": {
"value": "d9887268-47dd-4899-9568-db5809218751"
},
"fields": {
"firstName": {
"value": "Din"
},
"lastName": {
"value": "Djarin"
}
}
}
}
},
"properties": {
"children": {
"value": "Update"
}
}
}
37 changes: 36 additions & 1 deletion packages/codegen-ui/lib/types/studio-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,13 @@ export type BoundStudioComponentEvent = {
bindingEvent: string;
};

export type ActionStudioComponentEvent = NavigationAction | AuthSignOutAction | AuthUpdateUserAttributesAction;
export type ActionStudioComponentEvent =
| NavigationAction
| AuthSignOutAction
| AuthUpdateUserAttributesAction
| DataStoreCreateItemAction
| DataStoreUpdateItemAction
| DataStoreDeleteItemAction;

export type NavigationAction = {
action: 'Amplify.Navigation';
Expand Down Expand Up @@ -531,6 +537,35 @@ export type AuthUpdateUserAttributesAction = {
};
};

export type DataStoreCreateItemAction = {
action: 'Amplify.DataStoreCreateItemAction';
parameters: {
model: string;
fields: {
[propertyName: string]: StudioComponentProperty;
};
};
};

export type DataStoreUpdateItemAction = {
action: 'Amplify.DataStoreUpdateItemAction';
parameters: {
model: string;
id: StudioComponentProperty;
fields: {
[propertyName: string]: StudioComponentProperty;
};
};
};

export type DataStoreDeleteItemAction = {
action: 'Amplify.DataStoreDeleteItemAction';
parameters: {
model: string;
id: StudioComponentProperty;
};
};

export type StudioComponentEvents = {
[eventName: string]: StudioComponentEvent;
};
Expand Down

0 comments on commit 63a3067

Please sign in to comment.