@ic-reactor/react 0.0.10 → 0.1.1

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.
package/dist/hooks.d.ts CHANGED
@@ -1,13 +1,6 @@
1
- import { CallMethod, ExtractReActorMethodArgs, ReActorState } from "@ic-reactor/store";
2
- import { StoreApi } from "zustand";
1
+ import { CallMethod, ExtractReActorMethodArgs } from "@ic-reactor/store";
3
2
  import { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "./types";
4
- export declare const getStateHooks: <A extends unknown>(store: StoreApi<ReActorState<A>>, callMethod: CallMethod<A>) => {
5
- useReActor: () => ReActorState<A>;
6
- useLoading: () => boolean;
7
- useError: () => Error | undefined;
8
- useInitialized: () => boolean;
9
- useInitializing: () => boolean;
10
- useActorState: () => import("@ic-reactor/store").ReActorActorState<A>;
3
+ export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>) => {
11
4
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
12
5
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
13
6
  error: Error | undefined;
package/dist/hooks.js CHANGED
@@ -20,34 +20,9 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.getStateHooks = void 0;
23
+ exports.getCallHooks = void 0;
24
24
  const react_1 = require("react");
25
- const zustand_1 = require("zustand");
26
- const getStateHooks = (store, callMethod) => {
27
- const useReActor = () => {
28
- const state = (0, zustand_1.useStore)(store);
29
- return state;
30
- };
31
- const useLoading = () => {
32
- const loading = (0, zustand_1.useStore)(store, (state) => state.loading);
33
- return loading;
34
- };
35
- const useError = () => {
36
- const error = (0, zustand_1.useStore)(store, (state) => state.error);
37
- return error;
38
- };
39
- const useInitialized = () => {
40
- const initialized = (0, zustand_1.useStore)(store, (state) => state.initialized);
41
- return initialized;
42
- };
43
- const useInitializing = () => {
44
- const initializing = (0, zustand_1.useStore)(store, (state) => state.initializing);
45
- return initializing;
46
- };
47
- const useActorState = () => {
48
- const actorState = (0, zustand_1.useStore)(store, (state) => state.actorState);
49
- return actorState;
50
- };
25
+ const getCallHooks = (callMethod) => {
51
26
  const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
52
27
  const [state, setState] = (0, react_1.useState)({
53
28
  data: undefined,
@@ -66,6 +41,7 @@ const getStateHooks = (store, callMethod) => {
66
41
  return data;
67
42
  }
68
43
  catch (error) {
44
+ console.error("Error in call:", error);
69
45
  onError === null || onError === void 0 ? void 0 : onError(error);
70
46
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
71
47
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
@@ -98,14 +74,8 @@ const getStateHooks = (store, callMethod) => {
98
74
  return useReActorCall(args);
99
75
  };
100
76
  return {
101
- useReActor,
102
- useLoading,
103
- useError,
104
- useInitialized,
105
- useInitializing,
106
- useActorState,
107
77
  useQueryCall,
108
78
  useUpdateCall,
109
79
  };
110
80
  };
111
- exports.getStateHooks = getStateHooks;
81
+ exports.getCallHooks = getCallHooks;
package/dist/index.d.ts CHANGED
@@ -1,16 +1,10 @@
1
- /// <reference types="react" />
2
- import type { HttpAgent, HttpAgentOptions } from "@dfinity/agent";
1
+ import type { HttpAgent } from "@dfinity/agent";
3
2
  import { AuthClientLoginOptions } from "@dfinity/auth-client";
4
- import type { ActorSubclass, ReActorStore } from "@ic-reactor/store";
5
- export type ReActorContextType<A = ActorSubclass<any>> = ReActorStore<A>;
6
- export interface CreateReActorOptions extends HttpAgentOptions {
7
- initializeOnMount?: boolean;
8
- }
9
- export declare const createReActor: <A extends unknown>(actorInitializer: (agent: HttpAgent) => A, options?: CreateReActorOptions) => {
10
- initializeActor: (agentOptions?: HttpAgentOptions | undefined, identity?: import("@dfinity/agent").Identity | undefined) => void;
11
- useReActor: () => import("@ic-reactor/store").ReActorState<A>;
12
- useLoading: () => boolean;
13
- useError: () => Error | undefined;
3
+ import type { ActorSubclass, ReActorAuthStore, ReActorOptions } from "@ic-reactor/store";
4
+ export type ReActorContextType<A = ActorSubclass<any>> = ReActorAuthStore<A>;
5
+ export declare const createReActor: <A extends unknown>(actorInitializer: (agent: HttpAgent) => A, options?: ReActorOptions) => {
6
+ useActorStore: () => import("@ic-reactor/store").ReActorActorState<A>;
7
+ useAuthStore: () => import("@ic-reactor/store").ReActorAuthState<A>;
14
8
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: import("./types").ReActorUseQueryArgs<A, M>) => {
15
9
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
16
10
  error: Error | undefined;
@@ -36,15 +30,4 @@ export declare const createReActor: <A extends unknown>(actorInitializer: (agent
36
30
  loginLoading: boolean;
37
31
  loginError: unknown;
38
32
  };
39
- useActorState: () => import("@ic-reactor/store").ReActorActorState<A>;
40
- useInitialized: () => boolean;
41
- useInitializing: () => boolean;
42
- initialize: (identity?: import("@dfinity/agent").Identity | undefined) => void;
43
- authenticate: () => Promise<void>;
44
- resetState: () => void;
45
- updateState: (newState: Partial<import("@ic-reactor/store").ReActorState<A>>) => void;
46
- callMethod: import("@ic-reactor/store").CallMethod<A>;
47
- ReActorProvider: import("react").FC<{
48
- children: React.ReactNode;
49
- }>;
50
33
  };
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createReActor = void 0;
13
13
  const store_1 = require("@ic-reactor/store");
14
14
  const react_1 = require("react");
15
+ const zustand_1 = require("zustand");
15
16
  const hooks_1 = require("./hooks");
16
17
  const defaultCreateReActorOptions = {
17
18
  initializeOnMount: true,
@@ -21,28 +22,19 @@ const defaultCreateReActorOptions = {
21
22
  };
22
23
  const createReActor = (actorInitializer, options = {}) => {
23
24
  const optionsWithDefaults = Object.assign(Object.assign({}, defaultCreateReActorOptions), options);
24
- const Context = (0, react_1.createContext)(undefined);
25
- const { actions, initializeActor, store } = (0, store_1.createReActorStore)((agent) => actorInitializer(agent), optionsWithDefaults);
26
- if (optionsWithDefaults.initializeOnMount) {
27
- try {
28
- initializeActor();
29
- }
30
- catch (e) {
31
- console.error(e);
32
- }
33
- }
34
- const ReActorProvider = ({ children, }) => {
35
- (0, react_1.useEffect)(() => actions.resetState, [actions.resetState]);
36
- return (0, react_1.createElement)(Context.Provider, { value: store }, children);
25
+ const { callMethod, authenticate, authStore, actorStore } = (0, store_1.createReActorStore)((agent) => actorInitializer(agent), optionsWithDefaults);
26
+ const useActorStore = () => {
27
+ const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
28
+ return actorState;
29
+ };
30
+ const useAuthStore = () => {
31
+ const authState = (0, zustand_1.useStore)(authStore, (state) => state);
32
+ return authState;
37
33
  };
38
- const { useReActor, useLoading, useError, useQueryCall, useUpdateCall, useInitialized, useInitializing, useActorState, } = (0, hooks_1.getStateHooks)(store, actions.callMethod);
39
34
  const useAuthClient = () => {
40
35
  const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
41
36
  const [loginError, setLoginError] = (0, react_1.useState)(null);
42
- const { authClient, authenticated, authenticating, identity } = useReActor();
43
- const authenticate = () => __awaiter(void 0, void 0, void 0, function* () {
44
- yield actions.authenticate();
45
- });
37
+ const { authClient, authenticated, authenticating, identity } = useAuthStore();
46
38
  const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
47
39
  setLoginLoading(true);
48
40
  setLoginError(null);
@@ -85,15 +77,13 @@ const createReActor = (actorInitializer, options = {}) => {
85
77
  loginError,
86
78
  };
87
79
  };
88
- return Object.assign(Object.assign({ ReActorProvider }, actions), { initializeActor,
89
- useReActor,
90
- useLoading,
91
- useError,
80
+ const { useQueryCall, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod);
81
+ return {
82
+ useActorStore,
83
+ useAuthStore,
92
84
  useQueryCall,
93
85
  useUpdateCall,
94
86
  useAuthClient,
95
- useActorState,
96
- useInitialized,
97
- useInitializing });
87
+ };
98
88
  };
99
89
  exports.createReActor = createReActor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.0.10",
3
+ "version": "0.1.1",
4
4
  "description": "A React library for interacting with Dfinity actors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,12 +40,13 @@
40
40
  "@dfinity/candid": "0.20",
41
41
  "@dfinity/identity": "^0.20",
42
42
  "@dfinity/principal": "^0.20",
43
- "@ic-reactor/store": "^0.0.6",
44
- "zustand": "4.4"
43
+ "@ic-reactor/store": "^0.1.1",
44
+ "zustand": "4.4",
45
+ "zustand-utils": "^1.3"
45
46
  },
46
47
  "peerDependencies": {
47
48
  "@peculiar/webcrypto": "1.4",
48
49
  "react": ">=16.8"
49
50
  },
50
- "gitHead": "948b40598e08c2aa905da470d1952367ccf223ff"
51
+ "gitHead": "4e63567f41291e0368febc1e19d9eda570828d67"
51
52
  }