Skip to content

Commit f0ff6fd

Browse files
author
Damian Finkelstein
committed
refactor: 💡 changed getMyBalance to getAssetsToWithdraw
1 parent 7b71450 commit f0ff6fd

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

‎kyasshu‎

‎src/App.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const App = () => {
3232
path="/:collectionId/nft/:id"
3333
element={<NFTView />}
3434
/>
35-
<Route path="/:collectionId/offers/:id" element={<OfferView />} />
35+
<Route
36+
path="/:collectionId/offers/:id"
37+
element={<OfferView />}
38+
/>
3639
<Route
3740
path="/activity/:id"
3841
element={<UserActivityView />}

‎src/hooks/use-assets-to-withdraw.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect } from 'react';
22
import { useSelector } from 'react-redux';
3+
import { useMatch } from 'react-router-dom';
34
import {
45
useAppDispatch,
56
RootState,
@@ -11,6 +12,9 @@ export const useAssetsToWithdraw = () => {
1112
const dispatch = useAppDispatch();
1213
const { isConnected, principalId: plugPrincipal } = usePlugStore();
1314

15+
const match = useMatch('/:collectionId');
16+
const collectionId = match?.params?.collectionId;
17+
1418
const recentlyFailedTransactions = useSelector(
1519
(state: RootState) =>
1620
state.marketplace.recentlyFailedTransactions,
@@ -21,11 +25,10 @@ export const useAssetsToWithdraw = () => {
2125
);
2226

2327
useEffect(() => {
24-
if (!isConnected || !plugPrincipal) return;
25-
28+
if (!isConnected || !plugPrincipal || !collectionId) return;
2629
dispatch(
2730
marketplaceActions.getAssetsToWithdraw({
28-
userPrincipalId: plugPrincipal,
31+
collectionId,
2932
}),
3033
);
3134
}, [
@@ -34,5 +37,7 @@ export const useAssetsToWithdraw = () => {
3437
plugPrincipal,
3538
recentlyWithdrawnAssets,
3639
recentlyFailedTransactions,
40+
collectionId,
3741
]);
3842
};
43+
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
1-
import { Principal } from '@dfinity/principal';
21
import { createAsyncThunk } from '@reduxjs/toolkit';
3-
import { actorInstanceHandler } from '../../../../integrations/actor';
42
import { marketplaceSlice } from '../marketplace-slice';
53
import { AppLog } from '../../../../utils/log';
64
import { parseBalanceResponse } from '../../../../utils/parser';
75
import { settingsActions } from '../../settings';
6+
import { jellyJsInstanceHandler } from '../../../../integrations/jelly-js';
7+
import { getJellyCollection } from '../../../../utils/jelly';
8+
import { getPrincipal } from '../../../../integrations/plug';
89

910
export type GetAssetsToWithdrawProps = {
10-
userPrincipalId: string;
11+
collectionId: string;
1112
};
1213

1314
export const getAssetsToWithdraw = createAsyncThunk<
1415
any | undefined,
1516
GetAssetsToWithdrawProps
1617
>('marketplace/balanceOf', async (params, thunkAPI) => {
17-
// Checks if an actor instance exists already
18-
// otherwise creates a new instance
19-
const actorInstance = await actorInstanceHandler({
18+
const { collectionId } = params;
19+
20+
const jellyInstance = await jellyJsInstanceHandler({
2021
thunkAPI,
21-
serviceName: 'marketplace',
22+
collectionId,
2223
slice: marketplaceSlice,
2324
});
2425

25-
const { userPrincipalId } = params;
26-
2726
try {
28-
const userPrincipalAddress = Principal.fromText(userPrincipalId);
27+
const collection = await getJellyCollection({
28+
jellyInstance,
29+
collectionId,
30+
});
31+
32+
if (!collection)
33+
throw Error(`Oops! collection ${collectionId} not found!`);
2934

30-
const balanceResponse = await actorInstance.balanceOf(
31-
userPrincipalAddress,
35+
const jellyCollection = await jellyInstance.getJellyCollection(
36+
collection,
3237
);
3338

39+
const assetsToWithdrawResponse =
40+
await jellyCollection.getAssetsToWithdraw({
41+
user: await getPrincipal(),
42+
});
43+
3444
const assetsToWithdraw =
35-
!Array.isArray(balanceResponse) || !balanceResponse.length
45+
!Array.isArray(assetsToWithdrawResponse) ||
46+
!assetsToWithdrawResponse.length
3647
? []
37-
: parseBalanceResponse(balanceResponse);
48+
: parseBalanceResponse(assetsToWithdrawResponse);
3849

3950
if (!assetsToWithdraw.length) return;
4051

@@ -45,3 +56,4 @@ export const getAssetsToWithdraw = createAsyncThunk<
4556
AppLog.error(err);
4657
}
4758
});
59+

0 commit comments

Comments
 (0)