Skip to content

Commit

Permalink
Merge pull request stakwork#932 from aliraza556/fix/remove-redundant-…
Browse files Browse the repository at this point in the history
…proof-fetching

Remove redundant proof fetching from bounty card store
  • Loading branch information
humansinstitute authored Jan 15, 2025
2 parents e72c4d1 + de99f19 commit 3aed3a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 48 deletions.
22 changes: 6 additions & 16 deletions src/store/__test__/bountyCard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,15 @@ describe('BountyCardStore', () => {

describe('loadWorkspaceBounties', () => {
it('should handle successful bounty cards fetch', async () => {
const mockBounties = [{ id: 1, title: 'Test Bounty', pow: 0 }];
const mockBounties = [{ id: 1, title: 'Test Bounty', status: 'TODO' }];

fetchStub.onFirstCall().resolves({
fetchStub.resolves({
ok: true,
json: async () => mockBounties
} as Response);

fetchStub.onSecondCall().resolves({
ok: true,
json: async () => []
} as Response);

store = new BountyCardStore(mockWorkspaceId);
await waitFor(() => expect(store.bountyCards).toEqual([{ ...mockBounties[0] }]));
await waitFor(() => expect(store.bountyCards).toEqual(mockBounties));

expect(store.loading).toBe(false);
expect(store.error).toBeNull();
Expand All @@ -70,23 +65,18 @@ describe('BountyCardStore', () => {
describe('switchWorkspace', () => {
it('should switch workspace and reload bounties', async () => {
const newWorkspaceId = 'new-workspace-456';
const mockBounties = [{ id: 2, title: 'New Bounty', pow: 0 }];
const mockBounties = [{ id: 2, title: 'New Bounty', status: 'TODO' }];

fetchStub.onFirstCall().resolves({
fetchStub.resolves({
ok: true,
json: async () => mockBounties
} as Response);

fetchStub.onSecondCall().resolves({
ok: true,
json: async () => []
} as Response);

store = new BountyCardStore(mockWorkspaceId);

await waitFor(() => store.switchWorkspace(newWorkspaceId));
expect(store.currentWorkspaceId).toBe(newWorkspaceId);
expect(store.bountyCards).toEqual([{ ...mockBounties[0] }]);
expect(store.bountyCards).toEqual(mockBounties);
});

it('should not reload if workspace id is the same', async () => {
Expand Down
33 changes: 1 addition & 32 deletions src/store/bountyCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,8 @@ export class BountyCardStore {

const data = (await response.json()) as BountyCard[] | null;

// Fetch proof counts for each bounty
const bountyCardsWithProofs = await Promise.all(
(data || []).map(async (bounty: BountyCard) => {
try {
const proofsUrl = `${TribesURL}/gobounties/${bounty.id}/proofs`;
const proofsResponse = await fetch(proofsUrl, {
method: 'GET',
headers: {
'x-jwt': jwt,
'Content-Type': 'application/json'
}
});

if (!proofsResponse.ok) {
return { ...bounty, pow: 0 };
}

const proofs = await proofsResponse.json();
return {
...bounty,
pow: Array.isArray(proofs) ? proofs.length : 0
};
} catch (error) {
console.error(`Error fetching proofs for bounty ${bounty.id}:`, error);
return { ...bounty, pow: 0 };
}
})
);

runInAction(() => {
this.bountyCards = bountyCardsWithProofs.map((bounty: BountyCard) => ({
...bounty
}));
this.bountyCards = data || [];
});
} catch (error) {
console.error('Error loading bounties:', error);
Expand Down

0 comments on commit 3aed3a5

Please sign in to comment.