MCPcopy Create free account
hub / github.com/code100x/cms / useAction

Function useAction

src/hooks/useAction.ts:17–66  ·  view source on GitHub ↗
(
  action: Action<TInput, TOutput>,
  options: UseActionOptions<TOutput> = {},
)

Source from the content-addressed store, hash-verified

15}
16
17export const useAction = <TInput, TOutput>(
18 action: Action<TInput, TOutput>,
19 options: UseActionOptions<TOutput> = {},
20) => {
21 const [fieldErrors, setFieldErrors] = useState<
22 FieldErrors<TInput> | undefined
23 >(undefined);
24 const [error, setError] = useState<string | undefined>(undefined);
25 const [data, setData] = useState<TOutput | undefined>(undefined);
26 const [isLoading, setIsLoading] = useState<boolean>(false);
27
28 const execute = useCallback(
29 async (input: TInput) => {
30 setIsLoading(true);
31
32 try {
33 const result = await action(input);
34
35 if (!result) {
36 return;
37 }
38
39 setFieldErrors(result.fieldErrors);
40
41 if (result.error) {
42 setError(result.error);
43 options.onError?.(result.error);
44 }
45
46 if (result.data) {
47 setData(result.data);
48 options.onSuccess?.(result.data);
49 }
50 } finally {
51 setIsLoading(false);
52 options.onComplete?.();
53 }
54 },
55 [action, options],
56 );
57
58 return {
59 execute,
60 fieldErrors,
61 error,
62 data,
63 isLoading,
64 setFieldErrors,
65 };
66};

Callers 14

PaymentMethodsDropdownFunction · 0.90
NewPayoutDialogFunction · 0.90
NewPostDialogFunction · 0.90
CommentVoteFormFunction · 0.90
CommentDeleteFormFunction · 0.90
CommentApproveFormFunction · 0.90
CommentInputFormFunction · 0.90
CommentPinFormFunction · 0.90
PostCardFunction · 0.90
DeleteQAFormFunction · 0.90
VoteFormFunction · 0.90
ApproveCommentFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected