Skip to content

Commit 970628d

Browse files
committed
Update API configuration in Portfolio and Watchlist components
1 parent 5251d93 commit 970628d

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/pages/Portfolio.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ import {
2626
HiChartBar
2727
} from 'react-icons/hi';
2828

29-
const API_BASE_URL = 'http://localhost:5000/api';
29+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://portfolio-tracker-backend-y7ne.onrender.com/api';
30+
31+
// Create axios instance with base URL
32+
const api = axios.create({
33+
baseURL: API_BASE_URL,
34+
timeout: 10000
35+
});
3036

3137
export default function Portfolio() {
3238
const [stocks, setStocks] = useState([]);
@@ -64,7 +70,9 @@ export default function Portfolio() {
6470

6571
const fetchStocks = async () => {
6672
try {
67-
const response = await axios.get(`${API_BASE_URL}/stocks`);
73+
console.log('Fetching stocks from:', API_BASE_URL);
74+
const response = await api.get('/stocks');
75+
console.log('Stocks fetched successfully:', response.data);
6876
setStocks(response.data);
6977
setError(null);
7078
} catch (err) {
@@ -78,7 +86,7 @@ export default function Portfolio() {
7886
const handleAddStock = async (e) => {
7987
e.preventDefault();
8088
try {
81-
const response = await axios.post(`${API_BASE_URL}/stocks`, {
89+
const response = await api.post('/stocks', {
8290
name: newStock.name,
8391
ticker: newStock.ticker,
8492
shares: parseFloat(newStock.shares),
@@ -98,7 +106,7 @@ export default function Portfolio() {
98106
const handleEditStock = async (e) => {
99107
e.preventDefault();
100108
try {
101-
const response = await axios.put(`${API_BASE_URL}/stocks/${editingStock.id}`, {
109+
const response = await api.put(`/stocks/${editingStock.id}`, {
102110
name: editingStock.name,
103111
ticker: editingStock.ticker,
104112
shares: parseFloat(editingStock.shares),
@@ -120,7 +128,7 @@ export default function Portfolio() {
120128
const handleDeleteStock = async (id) => {
121129
if (window.confirm('Are you sure you want to delete this stock?')) {
122130
try {
123-
await axios.delete(`${API_BASE_URL}/stocks/${id}`);
131+
await api.delete(`/stocks/${id}`);
124132
setStocks(stocks.filter(stock => stock.id !== id));
125133
setError(null);
126134
} catch (err) {

src/pages/Watchlist.jsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { HiOutlineEye, HiOutlineTrash, HiSearch, HiPlus, HiTrendingUp, HiTrendin
55
import { Card, Text, Metric, Badge, ProgressBar } from '@tremor/react';
66
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
77

8-
const API_BASE_URL = 'http://localhost:5000/api';
8+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://portfolio-tracker-backend-y7ne.onrender.com/api';
9+
10+
// Create axios instance with base URL
11+
const api = axios.create({
12+
baseURL: API_BASE_URL,
13+
timeout: 10000
14+
});
915

1016
const Watchlist = () => {
1117
const [watchlist, setWatchlist] = useState([]);
@@ -43,8 +49,8 @@ const Watchlist = () => {
4349

4450
const fetchWatchlist = async () => {
4551
try {
46-
console.log('Fetching watchlist...');
47-
const response = await axios.get(`${API_BASE_URL}/watchlist`);
52+
console.log('Fetching watchlist from:', API_BASE_URL);
53+
const response = await api.get('/watchlist');
4854
console.log('Watchlist response:', response.data);
4955
setWatchlist(response.data);
5056
setLoading(false);
@@ -58,7 +64,7 @@ const Watchlist = () => {
5864
const syncPortfolioStocks = async () => {
5965
try {
6066
console.log('Syncing portfolio stocks...');
61-
await axios.post(`${API_BASE_URL}/watchlist/sync-portfolio`);
67+
await api.post('/watchlist/sync-portfolio');
6268
await fetchWatchlist();
6369
} catch (error) {
6470
console.error('Error syncing portfolio stocks:', error);
@@ -70,7 +76,7 @@ const Watchlist = () => {
7076
e.preventDefault();
7177
try {
7278
console.log('Adding stock:', newStock);
73-
await axios.post(`${API_BASE_URL}/watchlist`, newStock);
79+
await api.post('/watchlist', newStock);
7480
setShowAddModal(false);
7581
setNewStock({ name: '', ticker: '', target_price: '' });
7682
await fetchWatchlist();
@@ -84,7 +90,7 @@ const Watchlist = () => {
8490
if (window.confirm('Are you sure you want to remove this stock from your watchlist?')) {
8591
try {
8692
console.log('Deleting stock:', id);
87-
await axios.delete(`${API_BASE_URL}/watchlist/${id}`);
93+
await api.delete(`/watchlist/${id}`);
8894
await fetchWatchlist();
8995
} catch (error) {
9096
console.error('Error deleting stock:', error);
@@ -97,7 +103,7 @@ const Watchlist = () => {
97103
e.preventDefault();
98104
try {
99105
console.log('Updating stock:', selectedStock);
100-
await axios.put(`${API_BASE_URL}/watchlist/${selectedStock.id}`, {
106+
await api.put(`/watchlist/${selectedStock.id}`, {
101107
target_price: selectedStock.target_price
102108
});
103109
setShowUpdateModal(false);
@@ -113,7 +119,7 @@ const Watchlist = () => {
113119
setSelectedStock(stock);
114120
setShowViewModal(true);
115121
try {
116-
const response = await axios.get(`${API_BASE_URL}/watchlist/${stock.id}/history`);
122+
const response = await api.get(`/watchlist/${stock.id}/history`);
117123
setPriceHistory(response.data.map(item => ({
118124
...item,
119125
date: new Date(item.timestamp).toLocaleDateString(),

0 commit comments

Comments
 (0)