Skip to content

Commit

Permalink
Allow useGlobalAction callback to take no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
MillanWangGadget committed Sep 24, 2024
1 parent 577a5b9 commit 246f9cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/react/src/useGlobalAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get, globalActionOperation, namespaceDataPath } from "@gadgetinc/api-cl
import { useCallback, useEffect, useMemo } from "react";
import type { OperationContext, UseMutationState } from "urql";
import { useGadgetMutation } from "./useGadgetMutation.js";
import type { ActionHookResult } from "./utils.js";
import type { ActionHookResultWithOptionalCallbackVariables } from "./utils.js";
import { ErrorWrapper } from "./utils.js";

/**
Expand All @@ -29,7 +29,7 @@ import { ErrorWrapper } from "./utils.js";
*/
export const useGlobalAction = <F extends GlobalActionFunction<any>>(
action: F
): ActionHookResult<any, Exclude<F["variablesType"], null | undefined>> => {
): ActionHookResultWithOptionalCallbackVariables<any, Exclude<F["variablesType"], null | undefined>> => {
useEffect(() => {
if (action.type === ("stubbedAction" as string)) {
const stubbedAction = action as unknown as StubbedActionFunction<any>;
Expand Down Expand Up @@ -63,8 +63,8 @@ export const useGlobalAction = <F extends GlobalActionFunction<any>>(
return [
transformedResult,
useCallback(
async (variables: F["variablesType"], context?: Partial<OperationContext>) => {
const result = await runMutation(variables, context);
async (variables?: F["variablesType"], context?: Partial<OperationContext>) => {
const result = await runMutation(variables ?? {}, context);
return processResult({ fetching: false, ...result }, action);
},
[action, runMutation]
Expand Down
20 changes: 12 additions & 8 deletions packages/react/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ export type RequiredKeysOf<BaseType> = Exclude<
* Includes the data result object and a function for running the mutation.
**/
export type ActionHookResult<Data = any, Variables extends AnyVariables = AnyVariables> = RequiredKeysOf<Variables> extends never
? [
ActionHookState<Data, Variables>,
(variables?: Variables, context?: Partial<OperationContext>) => Promise<ActionHookState<Data, Variables>>
]
: [
ActionHookState<Data, Variables>,
(variables: Variables, context?: Partial<OperationContext>) => Promise<ActionHookState<Data, Variables>>
];
? ActionHookResultWithOptionalCallbackVariables<Data, Variables>
: ActionHookResultWithRequiredCallbackVariables<Data, Variables>;

export type ActionHookResultWithOptionalCallbackVariables<Data = any, Variables extends AnyVariables = AnyVariables> = [
ActionHookState<Data, Variables>,
(variables?: Variables, context?: Partial<OperationContext>) => Promise<ActionHookState<Data, Variables>>
];

export type ActionHookResultWithRequiredCallbackVariables<Data = any, Variables extends AnyVariables = AnyVariables> = [
ActionHookState<Data, Variables>,
(variables: Variables, context?: Partial<OperationContext>) => Promise<ActionHookState<Data, Variables>>
];

/**
* The inner result object returned from a mutation result
Expand Down

0 comments on commit 246f9cd

Please sign in to comment.