Aller au contenu
login
arrow_backRetour aux issues
grantFoxin/SentientFi #66

fix: usePortfolio hook uses relative API paths and bypasses retry/timeout logic

ecoDébutant bug help wanted Maybe Rewarded GrantFox OSS frontend Third Campaign

descriptionDescription

## Problem In `frontend/src/hooks/usePortfolio.ts`, both `fetchPortfolio` and `executeRebalance` call `fetch()` directly with relative paths like `/api/portfolio/${portfolioId}`. This bypasses: 1. The centralized `API_CONFIG.BASE_URL` from `frontend/src/config/api.ts` — needed for non-same-origin deployments (e.g., the production Render URL) 2. The `apiRequest` wrapper's retry logic (3 attempts with exponential backoff) 3. The 15-second timeout via `AbortController` In production, if the API is on a different origin, the hook's calls silently fail or hit the wrong endpoint. This was partially addressed in issue #47 but the hook was not updated — only `Dashboard.tsx` and `Landing.tsx` use `API_CONFIG.BASE_URL`. ## Proposed Fix ### 1. Use `API_CONFIG.BASE_URL` Import `API_CONFIG` from `../config/api` and prefix all fetch URLs: ```typescript const response = await fetch(`${API_CONFIG.BASE_URL}/api/portfolio/${portfolioId}`); ``` ### 2. Replace with `apiRequest` Use the existing `apiRequest` wrapper for automatic retry, timeout, and error parsing: ```typescript import { apiRequest, API_CONFIG } from '../config/api'; const data = await apiRequest(`${API_CONFIG.BASE_URL}/api/portfolio/${portfolioId}`); ``` ### 3. Verify response parsing `apiRequest` returns parsed JSON. The `GET /portfolio/:id` route returns `{ success: true, portfolio: {...} }`, so make sure `setPortfolio(data.portfolio)` still works correctly. ### Files to modify - `frontend/src/hooks/usePortfolio.ts` — update `fetchPortfolio` and `executeRebalance` ## Acceptance Criteria - [ ] Import `API_CONFIG` from `../config/api` - [ ] All fetch URLs prefixed with `API_CONFIG.BASE_URL` - [ ] Replace direct `fetch()` with `apiRequest` wrapper - [ ] `setPortfolio(data.portfolio)` still works correctly with parsed JSON response - [ ] Hook works when `VITE_API_URL` is set to a remote URL - [ ] Network errors show error state and retry per `apiRequest` config ## Affected Area Frontend
codeOuvre sur GitHub