@explorins/pers-sdk-react-native 1.3.2 → 1.5.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/README.md +276 -123
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +7 -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 +94 -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/useTransactions.d.ts +45 -3
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +152 -31
- 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 +12199 -796
- 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 +171 -157
- package/src/hooks/index.ts +11 -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 +109 -68
- package/src/hooks/useTenants.ts +77 -0
- package/src/hooks/useTokens.ts +189 -21
- package/src/hooks/useTransactions.ts +170 -34
- 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 +21 -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
|
@@ -1,85 +1,221 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type {
|
|
4
|
+
TransactionRequestDTO,
|
|
5
|
+
TransactionRequestResponseDTO,
|
|
6
|
+
TransactionDTO
|
|
7
|
+
} from '@explorins/pers-shared';
|
|
8
|
+
import type { TransactionPaginationParams } from '@explorins/pers-sdk/transaction';
|
|
3
9
|
|
|
10
|
+
interface TransactionFilters {
|
|
11
|
+
type?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* React hook for transaction operations in the PERS SDK
|
|
16
|
+
*
|
|
17
|
+
* Provides comprehensive transaction management including creation, retrieval, history,
|
|
18
|
+
* and administrative operations. Supports pagination and CSV export functionality.
|
|
19
|
+
*
|
|
20
|
+
* @returns Transaction hook with methods for transaction management
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* function TransactionsComponent() {
|
|
25
|
+
* const {
|
|
26
|
+
* createTransaction,
|
|
27
|
+
* getUserTransactionHistory
|
|
28
|
+
* } = useTransactions();
|
|
29
|
+
*
|
|
30
|
+
* const handleCreateTransaction = async (request) => {
|
|
31
|
+
* try {
|
|
32
|
+
* const result = await createTransaction(request);
|
|
33
|
+
* console.log('Transaction created:', result);
|
|
34
|
+
* // Handle signature URL if returned
|
|
35
|
+
* } catch (error) {
|
|
36
|
+
* console.error('Transaction failed:', error);
|
|
37
|
+
* }
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <div>
|
|
42
|
+
* <button onClick={() => handleCreateTransaction(transactionRequest)}>
|
|
43
|
+
* Create Transaction
|
|
44
|
+
* </button>
|
|
45
|
+
* </div>
|
|
46
|
+
* );
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
4
50
|
export const useTransactions = () => {
|
|
5
|
-
const {
|
|
51
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
6
52
|
|
|
7
53
|
if (!isAuthenticated && isInitialized) {
|
|
8
54
|
console.warn('SDK not authenticated. Some transaction operations may fail.');
|
|
9
55
|
}
|
|
10
56
|
|
|
11
|
-
|
|
12
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Creates a new transaction in the system
|
|
59
|
+
*
|
|
60
|
+
* Automatically handles signature URLs by opening them in the device's browser.
|
|
61
|
+
* The transaction may require user approval through external wallet applications.
|
|
62
|
+
*
|
|
63
|
+
* @param request - Transaction request data including amount, recipient, etc.
|
|
64
|
+
* @returns Promise resolving to transaction response with potential actionable items
|
|
65
|
+
* @throws Error if SDK is not initialized
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const { createTransaction } = useTransactions();
|
|
70
|
+
* const request = {
|
|
71
|
+
* amount: '100',
|
|
72
|
+
* recipient: '0x123...',
|
|
73
|
+
* tokenId: 'token-123'
|
|
74
|
+
* };
|
|
75
|
+
* const result = await createTransaction(request);
|
|
76
|
+
* console.log('Transaction created:', result);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
const createTransaction = useCallback(async (request: TransactionRequestDTO): Promise<TransactionRequestResponseDTO> => {
|
|
80
|
+
if (!isInitialized || !sdk) {
|
|
13
81
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
14
82
|
}
|
|
15
|
-
if (!transactions?.createTransaction) {
|
|
16
|
-
throw new Error('createTransaction method not available');
|
|
17
|
-
}
|
|
18
83
|
|
|
19
84
|
try {
|
|
20
|
-
console.log('
|
|
21
|
-
const result = await transactions.createTransaction(request);
|
|
85
|
+
console.log('Creating transaction with request:', request);
|
|
86
|
+
const result = await sdk.transactions.createTransaction(request);
|
|
22
87
|
|
|
23
|
-
//
|
|
88
|
+
// Cross-platform: Handle signature URLs
|
|
24
89
|
if (result?.actionable?.actionUrl) {
|
|
25
90
|
try {
|
|
26
91
|
const { Linking } = require('react-native');
|
|
27
|
-
console.log('
|
|
92
|
+
console.log('Opening signature URL:', result.actionable.actionUrl);
|
|
28
93
|
await Linking.openURL(result.actionable.actionUrl);
|
|
29
94
|
} catch (linkingError) {
|
|
30
|
-
console.error('
|
|
95
|
+
console.error('Failed to open signature URL:', linkingError);
|
|
31
96
|
}
|
|
32
97
|
}
|
|
33
98
|
|
|
34
|
-
console.log('
|
|
99
|
+
console.log('Transaction created successfully:', result);
|
|
100
|
+
return result;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('Failed to create transaction:', error);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}, [sdk, isInitialized]);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves a specific transaction by its ID
|
|
109
|
+
*
|
|
110
|
+
* @param transactionId - Unique identifier of the transaction
|
|
111
|
+
* @returns Promise resolving to transaction data or null if not found
|
|
112
|
+
* @throws Error if SDK is not initialized
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const { getTransactionById } = useTransactions();
|
|
117
|
+
* const transaction = await getTransactionById('txn-123');
|
|
118
|
+
* console.log('Transaction details:', transaction);
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
const getTransactionById = useCallback(async (transactionId: string): Promise<TransactionDTO | null> => {
|
|
122
|
+
if (!isInitialized || !sdk) {
|
|
123
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
const result = await sdk.transactions.getTransactionById(transactionId);
|
|
128
|
+
console.log('Transaction fetched successfully:', result);
|
|
35
129
|
return result;
|
|
36
130
|
} catch (error) {
|
|
37
|
-
console.error('
|
|
131
|
+
console.error('Failed to fetch transaction:', error);
|
|
38
132
|
throw error;
|
|
39
133
|
}
|
|
40
|
-
}, [
|
|
134
|
+
}, [sdk, isInitialized]);
|
|
41
135
|
|
|
42
|
-
|
|
43
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Retrieves transaction history for the authenticated user, filtered by type
|
|
138
|
+
*
|
|
139
|
+
* @param type - Transaction type filter (defaults to 'all')
|
|
140
|
+
* @returns Promise resolving to array of user's transactions
|
|
141
|
+
* @throws Error if SDK is not initialized
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const { getUserTransactionHistory } = useTransactions();
|
|
146
|
+
* const transactions = await getUserTransactionHistory('credit');
|
|
147
|
+
* console.log('Credit transactions:', transactions);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
const getUserTransactionHistory = useCallback(async (type: string = 'all'): Promise<TransactionDTO[]> => {
|
|
151
|
+
if (!isInitialized || !sdk) {
|
|
44
152
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
45
153
|
}
|
|
46
|
-
|
|
47
|
-
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const result = await sdk.transactions.getUserTransactionHistory(type);
|
|
157
|
+
console.log('Transaction history fetched successfully:', result);
|
|
158
|
+
return result;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.error('Failed to fetch transaction history:', error);
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}, [sdk, isInitialized]);
|
|
164
|
+
|
|
165
|
+
const getTenantTransactions = useCallback(async (): Promise<TransactionDTO[]> => {
|
|
166
|
+
if (!isInitialized || !sdk) {
|
|
167
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
48
168
|
}
|
|
49
169
|
|
|
50
170
|
try {
|
|
51
|
-
const result = await transactions.
|
|
52
|
-
console.log('
|
|
171
|
+
const result = await sdk.transactions.getTenantTransactions();
|
|
172
|
+
console.log('Tenant transactions fetched successfully:', result);
|
|
53
173
|
return result;
|
|
54
174
|
} catch (error) {
|
|
55
|
-
console.error('
|
|
175
|
+
console.error('Failed to fetch tenant transactions:', error);
|
|
56
176
|
throw error;
|
|
57
177
|
}
|
|
58
|
-
}, [
|
|
178
|
+
}, [sdk, isInitialized]);
|
|
59
179
|
|
|
60
|
-
const
|
|
61
|
-
if (!isInitialized) {
|
|
180
|
+
const getPaginatedTransactions = useCallback(async (params: TransactionPaginationParams): Promise<any> => {
|
|
181
|
+
if (!isInitialized || !sdk) {
|
|
62
182
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
63
183
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const result = await sdk.transactions.getPaginatedTransactions(params);
|
|
187
|
+
console.log('Paginated transactions fetched successfully:', result);
|
|
188
|
+
return result;
|
|
189
|
+
} catch (error) {
|
|
190
|
+
console.error('Failed to fetch paginated transactions:', error);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}, [sdk, isInitialized]);
|
|
194
|
+
|
|
195
|
+
const exportTransactionsCSV = useCallback(async (): Promise<Blob> => {
|
|
196
|
+
if (!isInitialized || !sdk) {
|
|
197
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
67
198
|
}
|
|
68
199
|
|
|
69
200
|
try {
|
|
70
|
-
const result = await transactions.
|
|
71
|
-
console.log('
|
|
201
|
+
const result = await sdk.transactions.exportTransactionsCSV();
|
|
202
|
+
console.log('Transactions CSV exported successfully');
|
|
72
203
|
return result;
|
|
73
204
|
} catch (error) {
|
|
74
|
-
console.error('
|
|
205
|
+
console.error('Failed to export transactions CSV:', error);
|
|
75
206
|
throw error;
|
|
76
207
|
}
|
|
77
|
-
}, [
|
|
208
|
+
}, [sdk, isInitialized]);
|
|
78
209
|
|
|
79
210
|
return {
|
|
80
211
|
createTransaction,
|
|
81
212
|
getTransactionById,
|
|
82
|
-
|
|
83
|
-
|
|
213
|
+
getUserTransactionHistory,
|
|
214
|
+
getTenantTransactions,
|
|
215
|
+
getPaginatedTransactions,
|
|
216
|
+
exportTransactionsCSV,
|
|
217
|
+
isAvailable: isInitialized && !!sdk?.transactions,
|
|
84
218
|
};
|
|
85
|
-
};
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
export type TransactionHook = ReturnType<typeof useTransactions>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { UserStatusTypeDTO } from '@explorins/pers-shared';
|
|
4
|
+
|
|
5
|
+
export const useUserStatus = () => {
|
|
6
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
7
|
+
|
|
8
|
+
const getUserStatusTypes = useCallback(async (): Promise<UserStatusTypeDTO[]> => {
|
|
9
|
+
if (!isInitialized || !sdk) {
|
|
10
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const result = await sdk.userStatus.getUserStatusTypes();
|
|
15
|
+
console.log('User status types fetched successfully:', result);
|
|
16
|
+
return result;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error('Failed to fetch user status types:', error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}, [sdk, isInitialized]);
|
|
22
|
+
|
|
23
|
+
const getEarnedUserStatus = useCallback(async (): Promise<UserStatusTypeDTO[]> => {
|
|
24
|
+
if (!isInitialized || !sdk) {
|
|
25
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
26
|
+
}
|
|
27
|
+
if (!isAuthenticated) {
|
|
28
|
+
throw new Error('SDK not authenticated. getEarnedUserStatus requires authentication.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const result = await sdk.userStatus.getEarnedUserStatus();
|
|
33
|
+
console.log('Earned user status fetched successfully:', result);
|
|
34
|
+
return result;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.error('Failed to fetch earned user status:', error);
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}, [sdk, isInitialized, isAuthenticated]);
|
|
40
|
+
|
|
41
|
+
// Admin methods
|
|
42
|
+
const createUserStatusType = useCallback(async (userStatusType: UserStatusTypeDTO): Promise<UserStatusTypeDTO> => {
|
|
43
|
+
if (!isInitialized || !sdk) {
|
|
44
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const result = await sdk.userStatus.createUserStatusType(userStatusType);
|
|
49
|
+
console.log('User status type created successfully:', result);
|
|
50
|
+
return result;
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error('Failed to create user status type:', error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}, [sdk, isInitialized]);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
getUserStatusTypes,
|
|
59
|
+
getEarnedUserStatus,
|
|
60
|
+
createUserStatusType,
|
|
61
|
+
isAvailable: isInitialized && !!sdk?.userStatus,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type UserStatusHook = ReturnType<typeof useUserStatus>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import type { UserDTO, UserCreateRequestDTO } from '@explorins/pers-shared';
|
|
4
|
+
import type { UserPublicProfileDTO } from '@explorins/pers-sdk/user';
|
|
5
|
+
|
|
6
|
+
export const useUsers = () => {
|
|
7
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
8
|
+
|
|
9
|
+
const getCurrentUser = useCallback(async (): Promise<UserDTO> => {
|
|
10
|
+
if (!isInitialized || !sdk) {
|
|
11
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
12
|
+
}
|
|
13
|
+
if (!isAuthenticated) {
|
|
14
|
+
throw new Error('SDK not authenticated. getCurrentUser requires authentication.');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const result = await sdk.users.getCurrentUser();
|
|
19
|
+
console.log('Current user fetched successfully:', result);
|
|
20
|
+
return result;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error('Failed to fetch current user:', error);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}, [sdk, isInitialized, isAuthenticated]);
|
|
26
|
+
|
|
27
|
+
const updateCurrentUser = useCallback(async (userData: UserCreateRequestDTO): Promise<UserDTO> => {
|
|
28
|
+
if (!isInitialized || !sdk) {
|
|
29
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
30
|
+
}
|
|
31
|
+
if (!isAuthenticated) {
|
|
32
|
+
throw new Error('SDK not authenticated. updateCurrentUser requires authentication.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const result = await sdk.users.updateCurrentUser(userData);
|
|
37
|
+
console.log('Current user updated successfully:', result);
|
|
38
|
+
return result;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('Failed to update current user:', error);
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}, [sdk, isInitialized, isAuthenticated]);
|
|
44
|
+
|
|
45
|
+
const getUserById = useCallback(async (userId: string): Promise<UserDTO> => {
|
|
46
|
+
if (!isInitialized || !sdk) {
|
|
47
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const result = await sdk.users.getUserById(userId);
|
|
52
|
+
console.log('User fetched successfully:', result);
|
|
53
|
+
return result;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error('Failed to fetch user:', error);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}, [sdk, isInitialized]);
|
|
59
|
+
|
|
60
|
+
const getAllUsersPublic = useCallback(async (filter?: { key: string; value: string }): Promise<UserPublicProfileDTO[]> => {
|
|
61
|
+
if (!isInitialized || !sdk) {
|
|
62
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const result = await sdk.users.getAllUsersPublic(filter);
|
|
67
|
+
console.log('Public users fetched successfully:', result);
|
|
68
|
+
return result;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error('Failed to fetch public users:', error);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}, [sdk, isInitialized]);
|
|
74
|
+
|
|
75
|
+
// Admin methods
|
|
76
|
+
const getAllUsers = useCallback(async (): Promise<UserDTO[]> => {
|
|
77
|
+
if (!isInitialized || !sdk) {
|
|
78
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const result = await sdk.users.getAllUsers();
|
|
83
|
+
console.log('All users fetched successfully:', result);
|
|
84
|
+
return result;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('Failed to fetch all users:', error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}, [sdk, isInitialized]);
|
|
90
|
+
|
|
91
|
+
const updateUser = useCallback(async (userId: string, userData: UserCreateRequestDTO): Promise<UserDTO> => {
|
|
92
|
+
if (!isInitialized || !sdk) {
|
|
93
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const result = await sdk.users.updateUser(userId, userData);
|
|
98
|
+
console.log('User updated successfully:', result);
|
|
99
|
+
return result;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error('Failed to update user:', error);
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}, [sdk, isInitialized]);
|
|
105
|
+
|
|
106
|
+
const toggleUserStatus = useCallback(async (user: UserDTO): Promise<UserDTO> => {
|
|
107
|
+
if (!isInitialized || !sdk) {
|
|
108
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
const result = await sdk.users.toggleUserStatus(user);
|
|
113
|
+
console.log('User status toggled successfully:', result);
|
|
114
|
+
return result;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('Failed to toggle user status:', error);
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}, [sdk, isInitialized]);
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
getCurrentUser,
|
|
123
|
+
updateCurrentUser,
|
|
124
|
+
getUserById,
|
|
125
|
+
getAllUsersPublic,
|
|
126
|
+
getAllUsers,
|
|
127
|
+
updateUser,
|
|
128
|
+
toggleUserStatus,
|
|
129
|
+
isAvailable: isInitialized && !!sdk?.users,
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export type UserHook = ReturnType<typeof useUsers>;
|