@explorins/pers-sdk-react-native 1.3.2 → 1.5.2
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/README.md +276 -123
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useAnalytics.d.ts +96 -0
- package/dist/hooks/useAnalytics.d.ts.map +1 -0
- package/dist/hooks/useAnalytics.js +73 -0
- package/dist/hooks/useAuth.d.ts +52 -7
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +250 -3
- package/dist/hooks/useBusiness.d.ts +45 -3
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +182 -28
- package/dist/hooks/useCampaigns.d.ts +11 -4
- package/dist/hooks/useCampaigns.d.ts.map +1 -1
- package/dist/hooks/useCampaigns.js +101 -39
- package/dist/hooks/useDonations.d.ts +33 -0
- package/dist/hooks/useDonations.d.ts.map +1 -0
- package/dist/hooks/useDonations.js +62 -0
- package/dist/hooks/useFiles.d.ts +50 -0
- package/dist/hooks/useFiles.d.ts.map +1 -0
- package/dist/hooks/useFiles.js +140 -0
- package/dist/hooks/usePurchases.d.ts +9 -0
- package/dist/hooks/usePurchases.d.ts.map +1 -0
- package/dist/hooks/usePurchases.js +56 -0
- package/dist/hooks/useRedemptions.d.ts +10 -5
- package/dist/hooks/useRedemptions.d.ts.map +1 -1
- package/dist/hooks/useRedemptions.js +118 -66
- package/dist/hooks/useTenants.d.ts +10 -0
- package/dist/hooks/useTenants.d.ts.map +1 -0
- package/dist/hooks/useTenants.js +68 -0
- package/dist/hooks/useTokens.d.ts +36 -2
- package/dist/hooks/useTokens.d.ts.map +1 -1
- package/dist/hooks/useTokens.js +180 -19
- package/dist/hooks/useTransactionSigner.d.ts +73 -0
- package/dist/hooks/useTransactionSigner.d.ts.map +1 -0
- package/dist/hooks/useTransactionSigner.js +216 -0
- package/dist/hooks/useTransactions.d.ts +45 -3
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +149 -39
- package/dist/hooks/useUserStatus.d.ts +9 -0
- package/dist/hooks/useUserStatus.d.ts.map +1 -0
- package/dist/hooks/useUserStatus.js +57 -0
- package/dist/hooks/useUsers.d.ts +17 -0
- package/dist/hooks/useUsers.d.ts.map +1 -0
- package/dist/hooks/useUsers.js +120 -0
- package/dist/hooks/useWeb3.d.ts +69 -6
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +177 -17
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31540 -794
- package/dist/index.js.map +1 -1
- package/dist/polyfills/index.d.ts +1 -1
- package/dist/polyfills/index.d.ts.map +1 -1
- package/dist/polyfills/index.js +155 -9
- package/dist/polyfills/web3-polyfills.d.ts +7 -0
- package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
- package/dist/polyfills/web3-polyfills.js +85 -0
- package/dist/providers/PersSDKProvider.d.ts +28 -18
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +63 -129
- package/dist/providers/react-native-auth-provider.d.ts +19 -64
- package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
- package/dist/providers/react-native-auth-provider.js +57 -175
- package/package.json +177 -157
- package/src/hooks/index.ts +13 -1
- package/src/hooks/useAnalytics.ts +150 -0
- package/src/hooks/useAuth.ts +286 -27
- package/src/hooks/useBusiness.ts +193 -30
- package/src/hooks/useCampaigns.ts +121 -43
- package/src/hooks/useDonations.ts +68 -0
- package/src/hooks/useFiles.ts +160 -0
- package/src/hooks/usePurchases.ts +69 -0
- package/src/hooks/useRedemptions.ts +135 -68
- package/src/hooks/useTenants.ts +77 -0
- package/src/hooks/useTokens.ts +189 -21
- package/src/hooks/useTransactionSigner.ts +287 -0
- package/src/hooks/useTransactions.ts +163 -41
- package/src/hooks/useUserStatus.ts +65 -0
- package/src/hooks/useUsers.ts +133 -0
- package/src/hooks/useWeb3.ts +201 -21
- package/src/index.ts +22 -17
- package/src/polyfills/index.ts +163 -10
- package/src/polyfills/web3-polyfills.ts +98 -0
- package/src/providers/PersSDKProvider.tsx +114 -178
- package/src/providers/react-native-auth-provider.ts +65 -208
- package/dist/index.esm.js +0 -1057
- package/dist/index.esm.js.map +0 -1
- package/dist/polyfills/index.simple.d.ts +0 -2
- package/dist/polyfills/index.simple.d.ts.map +0 -1
- package/dist/polyfills/index.simple.js +0 -17
- package/src/polyfills/index.simple.ts +0 -22
- package/src/types/external-modules.d.ts +0 -13
package/src/hooks/useAuth.ts
CHANGED
|
@@ -1,32 +1,284 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { RawUserData } from '@explorins/pers-sdk/core';
|
|
4
|
+
import type { UserDTO, AdminDTO, SessionAuthContextResponseDTO } from '@explorins/pers-shared';
|
|
3
5
|
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
6
|
+
// Re-export RawUserData for convenience
|
|
7
|
+
export type { RawUserData };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* React hook for authentication operations in the PERS SDK
|
|
11
|
+
*
|
|
12
|
+
* Provides methods for user login, logout, token management, and authentication state.
|
|
13
|
+
* Automatically manages authentication state and user data synchronization.
|
|
14
|
+
*
|
|
15
|
+
* @returns Authentication hook with methods and state
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* function LoginComponent() {
|
|
20
|
+
* const { login, logout, isAuthenticated, user } = useAuth();
|
|
21
|
+
*
|
|
22
|
+
* const handleLogin = async (token: string) => {
|
|
23
|
+
* try {
|
|
24
|
+
* await login(token, 'user');
|
|
25
|
+
* console.log('Login successful');
|
|
26
|
+
* } catch (error) {
|
|
27
|
+
* console.error('Login failed:', error);
|
|
28
|
+
* }
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <div>
|
|
33
|
+
* {isAuthenticated ? (
|
|
34
|
+
* <div>
|
|
35
|
+
* <p>Welcome, {user?.name}</p>
|
|
36
|
+
* <button onClick={logout}>Logout</button>
|
|
37
|
+
* </div>
|
|
38
|
+
* ) : (
|
|
39
|
+
* <button onClick={() => handleLogin('jwt-token')}>Login</button>
|
|
40
|
+
* )}
|
|
41
|
+
* </div>
|
|
42
|
+
* );
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export const useAuth = () => {
|
|
47
|
+
const {
|
|
48
|
+
sdk,
|
|
49
|
+
authProvider,
|
|
50
|
+
isInitialized,
|
|
51
|
+
isAuthenticated,
|
|
52
|
+
user,
|
|
53
|
+
accountAddress,
|
|
54
|
+
setAuthenticationState,
|
|
55
|
+
refreshUserData
|
|
28
56
|
} = usePersSDK();
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Authenticates a user with a JWT token
|
|
60
|
+
*
|
|
61
|
+
* @param jwtToken - The JWT token to authenticate with
|
|
62
|
+
* @param userType - The type of user ('user' | 'admin'), defaults to 'user'
|
|
63
|
+
* @returns Promise resolving to session auth context with user data
|
|
64
|
+
* @throws Error if SDK is not initialized or authentication fails
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const { login } = useAuth();
|
|
69
|
+
* const result = await login(jwtToken, 'user');
|
|
70
|
+
* console.log('Logged in user:', result.user);
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const login = useCallback(async (jwtToken: string, userType: 'user' | 'admin' = 'user'): Promise<SessionAuthContextResponseDTO> => {
|
|
74
|
+
if (!sdk || !authProvider) {
|
|
75
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
76
|
+
}
|
|
29
77
|
|
|
78
|
+
try {
|
|
79
|
+
console.log(`Logging in as ${userType}...`);
|
|
80
|
+
|
|
81
|
+
// Set token in auth provider
|
|
82
|
+
await authProvider.setToken(jwtToken);
|
|
83
|
+
|
|
84
|
+
// Perform login using the manager
|
|
85
|
+
const result = await sdk.auth.loginWithToken(jwtToken, userType);
|
|
86
|
+
|
|
87
|
+
const userData = result.user || result.admin;
|
|
88
|
+
|
|
89
|
+
if (userData) {
|
|
90
|
+
const userAccountAddress = ('accountAddress' in userData && userData.accountAddress) ||
|
|
91
|
+
('wallets' in userData && userData.wallets?.[0]?.address) ||
|
|
92
|
+
null;
|
|
93
|
+
|
|
94
|
+
setAuthenticationState(userData, userAccountAddress as string | null, true);
|
|
95
|
+
|
|
96
|
+
console.log('Login successful');
|
|
97
|
+
return result;
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error('No user data returned from login');
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('Login failed:', error);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}, [sdk, authProvider, setAuthenticationState]);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Logs in a user using raw user data (direct authentication bypass)
|
|
109
|
+
*
|
|
110
|
+
* @param rawUserData - Raw user data for direct authentication
|
|
111
|
+
* @throws Error if SDK is not initialized or authentication fails
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const { loginWithRawData } = useAuth();
|
|
116
|
+
* await loginWithRawData({ userId: '123', email: 'user@example.com' });
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
const loginWithRawData = useCallback(async (rawUserData: RawUserData): Promise<void> => {
|
|
120
|
+
if (!sdk || !authProvider) {
|
|
121
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
console.log('Logging in with raw user data...');
|
|
126
|
+
|
|
127
|
+
// Use the raw data login from the API client directly
|
|
128
|
+
const result = await sdk.api().loginUserWithRawData(rawUserData);
|
|
129
|
+
|
|
130
|
+
// Set token from result
|
|
131
|
+
if (result.accessToken) {
|
|
132
|
+
await authProvider.setToken(result.accessToken);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const userData = result.user;
|
|
136
|
+
|
|
137
|
+
if (userData) {
|
|
138
|
+
const userAccountAddress = userData.accountAddress ||
|
|
139
|
+
(userData as UserDTO & { wallets?: Array<{ address: string }> })?.wallets?.[0]?.address ||
|
|
140
|
+
null;
|
|
141
|
+
|
|
142
|
+
setAuthenticationState(userData, userAccountAddress, true);
|
|
143
|
+
|
|
144
|
+
console.log('Raw data login successful');
|
|
145
|
+
} else {
|
|
146
|
+
throw new Error('No user data returned from raw data login');
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error('Raw data login failed:', error);
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}, [sdk, authProvider, setAuthenticationState]);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Logs out the current user and clears authentication state
|
|
156
|
+
*
|
|
157
|
+
* @throws Error if logout process fails
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const { logout } = useAuth();
|
|
162
|
+
* await logout();
|
|
163
|
+
* console.log('User logged out');
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
const logout = useCallback(async (): Promise<void> => {
|
|
167
|
+
try {
|
|
168
|
+
console.log('Logging out...');
|
|
169
|
+
|
|
170
|
+
if (authProvider) {
|
|
171
|
+
await authProvider.setToken(null);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
setAuthenticationState(null, null, false);
|
|
175
|
+
|
|
176
|
+
console.log('Logout successful');
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error('Logout failed:', error);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
}, [authProvider, setAuthenticationState]);
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Retrieves the current authenticated user data
|
|
185
|
+
*
|
|
186
|
+
* @returns Promise resolving to current user data
|
|
187
|
+
* @throws Error if SDK is not initialized
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const { getCurrentUser } = useAuth();
|
|
192
|
+
* const user = await getCurrentUser();
|
|
193
|
+
* console.log('Current user:', user.email);
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
const getCurrentUser = useCallback(async (): Promise<UserDTO> => {
|
|
197
|
+
if (!sdk) {
|
|
198
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
199
|
+
}
|
|
200
|
+
return sdk.auth.getCurrentUser();
|
|
201
|
+
}, [sdk]);
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Checks if the current session is authenticated
|
|
205
|
+
*
|
|
206
|
+
* @returns Promise resolving to authentication status
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* const { checkIsAuthenticated } = useAuth();
|
|
211
|
+
* const isAuth = await checkIsAuthenticated();
|
|
212
|
+
* console.log('Is authenticated:', isAuth);
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
const checkIsAuthenticated = useCallback(async (): Promise<boolean> => {
|
|
216
|
+
if (!sdk) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return sdk.auth.isAuthenticated();
|
|
220
|
+
}, [sdk]);
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Refreshes authentication tokens
|
|
224
|
+
*
|
|
225
|
+
* @param refreshToken - Optional refresh token to use
|
|
226
|
+
* @returns Promise resolving to new session auth response
|
|
227
|
+
* @throws Error if SDK is not initialized
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const { refreshTokens } = useAuth();
|
|
232
|
+
* const tokens = await refreshTokens();
|
|
233
|
+
* console.log('Tokens refreshed:', tokens);
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
const refreshTokens = useCallback(async (refreshToken?: string) => {
|
|
237
|
+
if (!sdk) {
|
|
238
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
239
|
+
}
|
|
240
|
+
return sdk.auth.refreshTokens(refreshToken);
|
|
241
|
+
}, [sdk]);
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Clears all authentication data and resets auth state
|
|
245
|
+
*
|
|
246
|
+
* @throws Error if SDK is not initialized
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* const { clearAuth } = useAuth();
|
|
251
|
+
* await clearAuth();
|
|
252
|
+
* console.log('Auth data cleared');
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
const clearAuth = useCallback(async (): Promise<void> => {
|
|
256
|
+
if (!sdk) {
|
|
257
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
258
|
+
}
|
|
259
|
+
await sdk.auth.clearAuth();
|
|
260
|
+
setAuthenticationState(null, null, false);
|
|
261
|
+
}, [sdk, setAuthenticationState]);
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Checks if the current authentication is valid (non-expired)
|
|
265
|
+
*
|
|
266
|
+
* @returns Boolean indicating if auth is valid
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* const { hasValidAuth } = useAuth();
|
|
271
|
+
* const isValid = hasValidAuth();
|
|
272
|
+
* console.log('Auth is valid:', isValid);
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
const hasValidAuth = useCallback((): boolean => {
|
|
276
|
+
if (!sdk) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
return sdk.auth.hasValidAuth();
|
|
280
|
+
}, [sdk]);
|
|
281
|
+
|
|
30
282
|
return {
|
|
31
283
|
// State
|
|
32
284
|
isInitialized,
|
|
@@ -34,10 +286,17 @@ export const useAuth = (): AuthHook => {
|
|
|
34
286
|
user,
|
|
35
287
|
accountAddress,
|
|
36
288
|
|
|
37
|
-
//
|
|
38
|
-
initialize,
|
|
289
|
+
// Methods
|
|
39
290
|
login,
|
|
40
291
|
loginWithRawData,
|
|
41
292
|
logout,
|
|
293
|
+
refreshUserData,
|
|
294
|
+
getCurrentUser,
|
|
295
|
+
checkIsAuthenticated,
|
|
296
|
+
refreshTokens,
|
|
297
|
+
clearAuth,
|
|
298
|
+
hasValidAuth,
|
|
42
299
|
};
|
|
43
|
-
};
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export type AuthHook = ReturnType<typeof useAuth>;
|
package/src/hooks/useBusiness.ts
CHANGED
|
@@ -1,69 +1,232 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { BusinessDTO, BusinessTypeDTO, BusinessUpdateRequestDTO, BusinessToggleActiveRequestDTO } from '@explorins/pers-shared';
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* React hook for business operations in the PERS SDK
|
|
7
|
+
*
|
|
8
|
+
* Provides methods for fetching, creating, and managing businesses within the platform.
|
|
9
|
+
* Includes both public business queries and administrative operations.
|
|
10
|
+
*
|
|
11
|
+
* @returns Business hook with methods for business management
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* function BusinessesComponent() {
|
|
16
|
+
* const {
|
|
17
|
+
* getActiveBusinesses,
|
|
18
|
+
* getBusinessById,
|
|
19
|
+
* createBusiness
|
|
20
|
+
* } = useBusiness();
|
|
21
|
+
*
|
|
22
|
+
* const loadBusinesses = async () => {
|
|
23
|
+
* try {
|
|
24
|
+
* const businesses = await getActiveBusinesses();
|
|
25
|
+
* console.log('Active businesses:', businesses);
|
|
26
|
+
* } catch (error) {
|
|
27
|
+
* console.error('Failed to load businesses:', error);
|
|
28
|
+
* }
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <div>
|
|
33
|
+
* <button onClick={loadBusinesses}>Load Businesses</button>
|
|
34
|
+
* </div>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
4
39
|
export const useBusiness = () => {
|
|
5
|
-
const {
|
|
40
|
+
const { sdk, isInitialized } = usePersSDK();
|
|
6
41
|
|
|
7
|
-
|
|
8
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves all currently active businesses in the system
|
|
44
|
+
*
|
|
45
|
+
* @returns Promise resolving to array of active businesses
|
|
46
|
+
* @throws Error if SDK is not initialized
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const { getActiveBusinesses } = useBusiness();
|
|
51
|
+
* const businesses = await getActiveBusinesses();
|
|
52
|
+
* console.log('Active businesses:', businesses);
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const getActiveBusinesses = useCallback(async (): Promise<BusinessDTO[]> => {
|
|
56
|
+
if (!isInitialized || !sdk) {
|
|
9
57
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
10
58
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const result = await sdk.businesses.getActiveBusinesses();
|
|
62
|
+
console.log('Active businesses fetched successfully:', result);
|
|
63
|
+
return result;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('Failed to fetch active businesses:', error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}, [sdk, isInitialized]);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves all available business types/categories
|
|
72
|
+
*
|
|
73
|
+
* @returns Promise resolving to array of business types
|
|
74
|
+
* @throws Error if SDK is not initialized
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const { getBusinessTypes } = useBusiness();
|
|
79
|
+
* const types = await getBusinessTypes();
|
|
80
|
+
* console.log('Business types:', types);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
const getBusinessTypes = useCallback(async (): Promise<BusinessTypeDTO[]> => {
|
|
84
|
+
if (!isInitialized || !sdk) {
|
|
85
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const result = await sdk.businesses.getBusinessTypes();
|
|
90
|
+
console.log('Business types fetched successfully:', result);
|
|
91
|
+
return result;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error('Failed to fetch business types:', error);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}, [sdk, isInitialized]);
|
|
97
|
+
|
|
98
|
+
const getBusinessById = useCallback(async (businessId: string): Promise<BusinessDTO | null> => {
|
|
99
|
+
if (!isInitialized || !sdk) {
|
|
100
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const result = await sdk.businesses.getBusinessById(businessId);
|
|
105
|
+
console.log('Business fetched successfully:', result);
|
|
106
|
+
return result;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.error('Failed to fetch business:', error);
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}, [sdk, isInitialized]);
|
|
112
|
+
|
|
113
|
+
const getBusinesses = useCallback(async (): Promise<BusinessDTO[]> => {
|
|
114
|
+
if (!isInitialized || !sdk) {
|
|
115
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
14
116
|
}
|
|
15
117
|
|
|
16
118
|
try {
|
|
17
|
-
const result = await
|
|
18
|
-
console.log('
|
|
119
|
+
const result = await sdk.businesses.getBusinesses();
|
|
120
|
+
console.log('All businesses fetched successfully:', result);
|
|
19
121
|
return result;
|
|
20
122
|
} catch (error) {
|
|
21
|
-
console.error('
|
|
123
|
+
console.error('Failed to fetch all businesses:', error);
|
|
22
124
|
throw error;
|
|
23
125
|
}
|
|
24
|
-
}, [
|
|
126
|
+
}, [sdk, isInitialized]);
|
|
25
127
|
|
|
26
|
-
const
|
|
27
|
-
if (!isInitialized) {
|
|
128
|
+
const getBusinessByAccount = useCallback(async (accountAddress: string): Promise<BusinessDTO> => {
|
|
129
|
+
if (!isInitialized || !sdk) {
|
|
28
130
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
29
131
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const result = await sdk.businesses.getBusinessByAccount(accountAddress);
|
|
135
|
+
console.log('Business by account fetched successfully:', result);
|
|
136
|
+
return result;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error('Failed to fetch business by account:', error);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}, [sdk, isInitialized]);
|
|
142
|
+
|
|
143
|
+
const getBusinessesByType = useCallback(async (typeId: string): Promise<BusinessDTO[]> => {
|
|
144
|
+
if (!isInitialized || !sdk) {
|
|
145
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const result = await sdk.businesses.getBusinessesByType(typeId);
|
|
150
|
+
console.log('Businesses by type fetched successfully:', result);
|
|
151
|
+
return result;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error('Failed to fetch businesses by type:', error);
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}, [sdk, isInitialized]);
|
|
157
|
+
|
|
158
|
+
// Admin methods
|
|
159
|
+
/**
|
|
160
|
+
* Creates a new business (Admin operation)
|
|
161
|
+
*
|
|
162
|
+
* @param displayName - The display name for the new business
|
|
163
|
+
* @returns Promise resolving to the created business data
|
|
164
|
+
* @throws Error if SDK is not initialized or user lacks permissions
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const { createBusiness } = useBusiness();
|
|
169
|
+
* const business = await createBusiness('My Coffee Shop');
|
|
170
|
+
* console.log('Business created:', business);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
const createBusiness = useCallback(async (displayName: string): Promise<BusinessDTO> => {
|
|
174
|
+
if (!isInitialized || !sdk) {
|
|
175
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
33
176
|
}
|
|
34
177
|
|
|
35
178
|
try {
|
|
36
|
-
const result = await
|
|
37
|
-
console.log('
|
|
179
|
+
const result = await sdk.businesses.createBusiness(displayName);
|
|
180
|
+
console.log('Business created successfully:', result);
|
|
38
181
|
return result;
|
|
39
182
|
} catch (error) {
|
|
40
|
-
console.error('
|
|
183
|
+
console.error('Failed to create business:', error);
|
|
41
184
|
throw error;
|
|
42
185
|
}
|
|
43
|
-
}, [
|
|
186
|
+
}, [sdk, isInitialized]);
|
|
44
187
|
|
|
45
|
-
const
|
|
46
|
-
if (!isInitialized) {
|
|
188
|
+
const updateBusiness = useCallback(async (businessId: string, businessData: BusinessUpdateRequestDTO): Promise<BusinessDTO> => {
|
|
189
|
+
if (!isInitialized || !sdk) {
|
|
47
190
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
48
191
|
}
|
|
49
|
-
|
|
50
|
-
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
const result = await sdk.businesses.updateBusiness(businessId, businessData);
|
|
195
|
+
console.log('Business updated successfully:', result);
|
|
196
|
+
return result;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
console.error('Failed to update business:', error);
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}, [sdk, isInitialized]);
|
|
202
|
+
|
|
203
|
+
const toggleBusinessStatus = useCallback(async (businessId: string, toggleData: BusinessToggleActiveRequestDTO): Promise<BusinessDTO> => {
|
|
204
|
+
if (!isInitialized || !sdk) {
|
|
205
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
51
206
|
}
|
|
52
207
|
|
|
53
208
|
try {
|
|
54
|
-
const result = await
|
|
55
|
-
console.log('
|
|
209
|
+
const result = await sdk.businesses.toggleBusinessStatus(businessId, toggleData);
|
|
210
|
+
console.log('Business status toggled successfully:', result);
|
|
56
211
|
return result;
|
|
57
212
|
} catch (error) {
|
|
58
|
-
console.error('
|
|
213
|
+
console.error('Failed to toggle business status:', error);
|
|
59
214
|
throw error;
|
|
60
215
|
}
|
|
61
|
-
}, [
|
|
216
|
+
}, [sdk, isInitialized]);
|
|
62
217
|
|
|
63
218
|
return {
|
|
64
219
|
getActiveBusinesses,
|
|
65
|
-
|
|
220
|
+
getBusinessTypes,
|
|
221
|
+
getBusinesses,
|
|
66
222
|
getBusinessById,
|
|
67
|
-
|
|
223
|
+
getBusinessByAccount,
|
|
224
|
+
getBusinessesByType,
|
|
225
|
+
createBusiness,
|
|
226
|
+
updateBusiness,
|
|
227
|
+
toggleBusinessStatus,
|
|
228
|
+
isAvailable: isInitialized && !!sdk?.businesses,
|
|
68
229
|
};
|
|
69
|
-
};
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export type BusinessHook = ReturnType<typeof useBusiness>;
|