Skip to content

Commit

Permalink
Merge pull request #150 from pranavkparti/sidebar-pending-count-fix
Browse files Browse the repository at this point in the history
fix: sidebar shows correct count
  • Loading branch information
pranavkparti authored Nov 23, 2024
2 parents 4ab1406 + 89d2d3f commit a24705d
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions src/store/TrustRelationshipsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const TrustRelationshipsProvider = ({ children }) => {
const [refetch, setRefetch] = useState(false);
const [count, setCount] = useState(0);

// used to store all managed wallets
const [managedWallets, setManagedWallets] = useState([]);

// Loader
Expand Down Expand Up @@ -221,49 +222,62 @@ const TrustRelationshipsProvider = ({ children }) => {
filter,
sorting,
});
const walletsData = await getWallets(
authContext.token,
'',
{
pagination,
},
{ sorting }
);
setManagedWallets(walletsData);
const preparedRows = prepareRows(await data.trust_relationships);

setTableRows(preparedRows);
setTotalRowCount(data.total);
} catch (error) {
console.error(error);
setMessage('An error occurred while fetching the table data');
} finally {
setIsLoading(false);
setRefetch(false);
}
};

const loadPendingRelationshipsData = async () => {
try {
setIsLoading(true);

// get all managed wallets
const allWalletsData = await getWallets(authContext.token, '', {
pagination: { limit: 1000 },
});
setManagedWallets(allWalletsData);

// count number of pending trust relationships
let local_count = 0;
const pendingRelationships = await getPendingTrustRelationships(
authContext.token
);
for (const item of pendingRelationships.trust_relationships) {
if (item.state === 'requested' && wallet.name === item.target_wallet) {
if (wallet.name === item.target_wallet) {
local_count++;
}
if (
item.state === 'requested' &&
walletsData.wallets.some(
} else if (
allWalletsData.wallets.some(
(wallet) => wallet.name === item.target_wallet
)
) {
local_count++;
}
}
setCount(local_count);
const preparedRows = prepareRows(await data.trust_relationships);

setTableRows(preparedRows);
setTotalRowCount(data.total);
} catch (error) {
console.error(error);
setMessage('An error occurred while fetching the table data');
console.error(
'An error occured fetching the managed wallets and/or pending trust relationships',
error
);
} finally {
setIsLoading(false);
setRefetch(false);
}
7;
};

// loading managed wallets and pending trust relationships data only on refetch
// refetch is set to true when a trust relationship has an action performed on it
useEffect(() => {
loadPendingRelationshipsData();
}, [refetch]);

useEffect(() => {
loadData();
}, [pagination, filter, sorting, refetch]);
Expand Down

0 comments on commit a24705d

Please sign in to comment.