Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trust rel sidebar count fix #146

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/api/trust_relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ export const getTrustRelationships = async (
}
};

export const getPendingTrustRelationships = async (token) => {
const wallet = JSON.parse(localStorage.getItem('wallet') || '{}');
try {
const response = await apiClient
.setAuthHeader(token)
.get(`/wallets/${wallet.id}/trust_relationships?state=requested`);
return response.data;
} catch (error) {
console.error(error);
}
};

export const requestTrustRelationship = async (
token,
{ requestType, requestingWallet, targetWallet }
Expand Down Expand Up @@ -60,7 +72,7 @@ export const acceptTrustRelationship = async ({ id, token }) => {
headers: {
'Content-Type': 'application/json',
'TREETRACKER-API-KEY': secureLocalStorage.getItem('api-key') || '',
'Authorization': token ? `Bearer ${token}` : ''
Authorization: token ? `Bearer ${token}` : '',
},
}
);
Expand All @@ -79,7 +91,7 @@ export const declineTrustRelationship = async ({ id, token }) => {
headers: {
'Content-Type': 'application/json',
'TREETRACKER-API-KEY': secureLocalStorage.getItem('api-key') || '',
'Authorization': token ? `Bearer ${token}` : ''
Authorization: token ? `Bearer ${token}` : '',
},
}
);
Expand All @@ -89,9 +101,6 @@ export const declineTrustRelationship = async ({ id, token }) => {
}
};




export const deleteTrustRelationship = async ({ id, token }) => {
try {
const response = await fetch(
Expand All @@ -101,7 +110,7 @@ export const deleteTrustRelationship = async ({ id, token }) => {
headers: {
'Content-Type': 'application/json',
'TREETRACKER-API-KEY': secureLocalStorage.getItem('api-key') || '',
'Authorization': token ? `Bearer ${token}` : ''
Authorization: token ? `Bearer ${token}` : '',
},
}
);
Expand All @@ -110,7 +119,7 @@ export const deleteTrustRelationship = async ({ id, token }) => {
throw new Error(`HTTP error! Status: ${response.status}`);
}

return response.json();
return response.json();
} catch (error) {
console.error(error);
}
Expand Down
27 changes: 20 additions & 7 deletions src/store/TrustRelationshipsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import { createContext, useContext, useState, useEffect } from 'react';
import { getDateText } from '../utils/formatting';
import AuthContext from './auth-context';
import { getTrustRelationships } from '../api/trust_relationships';
import {
getPendingTrustRelationships,
getTrustRelationships,
} from '../api/trust_relationships';
import TrustRelationshipsFilter from '../models/TrustRelationShipFilter';
import { getWallets } from '../api/wallets';

Expand Down Expand Up @@ -196,7 +199,7 @@ const TrustRelationshipsProvider = ({ children }) => {
label: 'Deduct',
value: 'deduct',
color: 'black',
}
},
];

// error
Expand All @@ -209,7 +212,6 @@ const TrustRelationshipsProvider = ({ children }) => {

const authContext = useContext(AuthContext);


const loadData = async () => {
try {
setIsLoading(true);
Expand All @@ -227,13 +229,23 @@ const TrustRelationshipsProvider = ({ children }) => {
},
{ sorting }
);
setManagedWallets(walletsData);
setManagedWallets(walletsData);

// count number of pending trust relationships
let local_count = 0;
for (const item of data.trust_relationships) {
const pendingRelationships = await getPendingTrustRelationships(
authContext.token
);
for (const item of pendingRelationships.trust_relationships) {
if (item.state === 'requested' && wallet.name === item.target_wallet) {
local_count++;
}
if (item.state === 'requested' && walletsData.wallets.some(wallet => wallet.name === item.target_wallet)) {
if (
item.state === 'requested' &&
walletsData.wallets.some(
(wallet) => wallet.name === item.target_wallet
)
) {
local_count++;
}
}
Expand All @@ -249,6 +261,7 @@ const TrustRelationshipsProvider = ({ children }) => {
setIsLoading(false);
setRefetch(false);
}
7;
};

useEffect(() => {
Expand Down Expand Up @@ -279,7 +292,7 @@ const TrustRelationshipsProvider = ({ children }) => {
setSorting,
loadData,
managedWallets,
setManagedWallets
setManagedWallets,
};

return (
Expand Down
Loading