Skip to content

Commit

Permalink
Add onStart property in AutoButton
Browse files Browse the repository at this point in the history
  • Loading branch information
alanko0511 committed Dec 16, 2024
1 parent 3c80cbc commit 4be705f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/react/.changeset/shy-ways-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gadgetinc/react": minor
---

New `onStart` callback on `AutoButton` is available. This callback is called when the button is clicked and before the action is run.
10 changes: 8 additions & 2 deletions packages/react/src/auto/hooks/useAutoButtonController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export type AutoButtonProps<
action: ActionFunc;
/** The variables to pass to the action when run */
variables?: ActionFunc["variablesType"];
/**
* Callback function to run when the button is clicked. The action will run after this is called.
*/
onStart?: () => void;
/**
* Callback function to run when the button succeeded at running the action
* Overrides the default behavior of rendering a message to the user to display success
Expand All @@ -34,7 +38,7 @@ export const useAutoButtonController = <
>(
props: AutoButtonProps<GivenOptions, SchemaT, ActionFunc>
) => {
const { action, variables, onSuccess, onError, ...buttonProps } = props;
const { action, variables, onStart, onSuccess, onError, ...buttonProps } = props;
const { metadata, fetching: fetchingMetadata, error: metadataError } = useActionMetadata(action);

const [{ data: result, fetching: fetchingActionResult, error }, runAction] =
Expand All @@ -55,13 +59,15 @@ export const useAutoButtonController = <
}

const run = useCallback(async () => {
onStart?.();

const result = await runAction(variables);
if (result.error) {
onError?.(result.error, result);
} else {
onSuccess?.(result);
}
}, [runAction, variables, onSuccess, onError]) as () => void;
}, [onStart, runAction, variables, onError, onSuccess]) as () => void;

return {
result,
Expand Down

0 comments on commit 4be705f

Please sign in to comment.