@ic-reactor/react 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -90,9 +90,11 @@ function createActorContext(contextConfig = {}) {
90
90
  const config = react_1.default.useMemo(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
91
91
  const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
92
92
  return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, hooks === null
93
- ? (fetchError !== null && fetchError !== void 0 ? fetchError : authenticating)
94
- ? authenticatingComponent
95
- : loadingComponent
93
+ ? fetchError
94
+ ? fetchError
95
+ : authenticating
96
+ ? authenticatingComponent
97
+ : loadingComponent
96
98
  : children));
97
99
  };
98
100
  ActorProvider.displayName = "ActorProvider";
@@ -47,10 +47,10 @@ const DEFAULT_STATE = {
47
47
  * Each hook is designed to simplify the process of interacting with actors in IC projects by abstracting away the complexity of state management, error handling, and method invocation.
48
48
  */
49
49
  const actorHooks = (actorManager) => {
50
- const { actorStore, canisterId, visitFunction, extractInterface, extractMethodNames, callMethod, initialize, } = actorManager;
50
+ const { actorStore, canisterId, visitFunction, methodAttributes, extractMethodAttributes, extractInterface, callMethod, initialize, } = actorManager;
51
51
  const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
52
52
  const useMethodNames = () => {
53
- return react_1.default.useMemo(() => extractMethodNames(), []);
53
+ return react_1.default.useMemo(() => Object.keys(extractMethodAttributes()), []);
54
54
  };
55
55
  const useActorInterface = () => {
56
56
  return react_1.default.useMemo(() => extractInterface(), []);
@@ -111,10 +111,31 @@ const actorHooks = (actorManager) => {
111
111
  };
112
112
  const useUpdateCall = useSharedCall;
113
113
  const useMethod = (args) => {
114
- const _a = useSharedCall(args), { call } = _a, state = __rest(_a, ["call"]);
115
114
  const visit = useVisitMethod(args.functionName);
116
- return Object.assign({ call,
117
- visit }, state);
115
+ const attributes = methodAttributes[args.functionName];
116
+ let refetchOnMount = args.refetchOnMount;
117
+ let refetchInterval = args.refetchInterval;
118
+ let formRequired = true;
119
+ switch (attributes.type) {
120
+ case "query":
121
+ try {
122
+ if (attributes.numberOfArgs > 0 && args.args === undefined) {
123
+ throw new Error("Args required");
124
+ }
125
+ attributes.validate((args.args || []));
126
+ formRequired = false;
127
+ }
128
+ catch (error) {
129
+ refetchOnMount = false;
130
+ refetchInterval = false;
131
+ }
132
+ return Object.assign(Object.assign({ visit }, useQueryCall(Object.assign(Object.assign({}, args), { refetchOnMount,
133
+ refetchInterval }))), { formRequired });
134
+ case "update":
135
+ return Object.assign(Object.assign({ visit }, useUpdateCall(args)), { formRequired });
136
+ default:
137
+ throw new Error(`Method type ${attributes.type} not found`);
138
+ }
118
139
  };
119
140
  return {
120
141
  initialize,
@@ -71,10 +71,11 @@ export interface DynamicDataArgs<V = unknown> {
71
71
  label: string;
72
72
  value: V;
73
73
  }
74
- export interface UseMethodParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
74
+ export interface UseMethodParameters<A, M extends FunctionName<A>> extends UseQueryCallParameters<A, M> {
75
75
  }
76
76
  export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
77
77
  loading: boolean;
78
+ formRequired: boolean;
78
79
  error: Error | undefined;
79
80
  data: ActorMethodReturnType<A[M]> | undefined;
80
81
  visit: VisitService<A>[M];
@@ -88,7 +89,7 @@ export interface ActorHooksReturnType<A = BaseActor> {
88
89
  initialize: () => Promise<void>;
89
90
  useActorState: () => UseActorState;
90
91
  useActorInterface: () => ServiceClass;
91
- useMethodNames: () => FunctionName<A>[];
92
+ useMethodNames: <Actor = A>() => FunctionName<Actor>[];
92
93
  useMethod: UseMethod<A>;
93
94
  useQueryCall: UseQueryCall<A>;
94
95
  useUpdateCall: UseUpdateCall<A>;
@@ -2,6 +2,7 @@ export * from "./useMethod";
2
2
  export * from "./useActorState";
3
3
  export * from "./useQueryCall";
4
4
  export * from "./useUpdateCall";
5
+ export * from "./useMethodNames";
5
6
  export * from "./useVisitMethod";
6
7
  export * from "./useVisitService";
7
8
  export * from "./useActorInterface";
@@ -18,6 +18,7 @@ __exportStar(require("./useMethod"), exports);
18
18
  __exportStar(require("./useActorState"), exports);
19
19
  __exportStar(require("./useQueryCall"), exports);
20
20
  __exportStar(require("./useUpdateCall"), exports);
21
+ __exportStar(require("./useMethodNames"), exports);
21
22
  __exportStar(require("./useVisitMethod"), exports);
22
23
  __exportStar(require("./useVisitService"), exports);
23
24
  __exportStar(require("./useActorInterface"), exports);
@@ -1,6 +1,7 @@
1
+ import { BaseActor } from "../../types";
1
2
  /**
2
3
  * Hook for accessing the method names of an actor.
3
4
  *
4
5
  * @returns An array of method names for the actor.
5
6
  */
6
- export declare const useMethodNames: () => string[];
7
+ export declare const useMethodNames: <A = BaseActor>() => Extract<keyof A, string>[];
@@ -7,4 +7,5 @@ const hooks_1 = require("./hooks");
7
7
  *
8
8
  * @returns An array of method names for the actor.
9
9
  */
10
- exports.useMethodNames = hooks_1.ActorHooks.useMethodNames;
10
+ const useMethodNames = () => hooks_1.ActorHooks.useMethodNames();
11
+ exports.useMethodNames = useMethodNames;
@@ -135,6 +135,7 @@ const useActor = (config) => {
135
135
  setActorManager(() => actorManager);
136
136
  }
137
137
  else {
138
+ setActorManager(undefined);
138
139
  fetchCandid().then((idlFactory) => {
139
140
  if (!idlFactory)
140
141
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "description": "A React library for interacting with Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "node": ">=10"
36
36
  },
37
37
  "dependencies": {
38
- "@ic-reactor/core": "^1.2.3",
38
+ "@ic-reactor/core": "^1.3.0",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -47,5 +47,5 @@
47
47
  "react": ">=16.8",
48
48
  "zustand": "4.5"
49
49
  },
50
- "gitHead": "ae71ae0c7eee0ebe8a97751d4222054668a6b994"
50
+ "gitHead": "25043c2d1a78b97e327995abaf13835ac511e5d6"
51
51
  }