|
1 | 1 | import React, { useState } from 'react';
|
2 | 2 | import { motion } from 'framer-motion';
|
3 |
| -import { Link, useLocation } from 'react-router-dom'; |
| 3 | +import { Link, useLocation, useNavigate } from 'react-router-dom'; |
| 4 | +import { useAuth } from '../../context/AuthContext'; |
4 | 5 | import axios from 'axios';
|
5 | 6 | import {
|
6 | 7 | HiHome,
|
7 | 8 | HiChartBar,
|
8 | 9 | HiStar,
|
| 10 | + HiNewspaper, |
| 11 | + HiCalendar, |
9 | 12 | HiChevronLeft,
|
10 | 13 | HiChevronRight,
|
11 | 14 | HiSun,
|
@@ -33,134 +36,41 @@ const LoadingOverlay = ({ message }) => (
|
33 | 36 |
|
34 | 37 | export default function Sidebar({ theme }) {
|
35 | 38 | const location = useLocation();
|
| 39 | + const navigate = useNavigate(); |
| 40 | + const { signOut } = useAuth(); |
36 | 41 | const [isExpanded, setIsExpanded] = useState(true);
|
37 | 42 | const [isLoading, setIsLoading] = useState(false);
|
38 | 43 | const [loadingMessage, setLoadingMessage] = useState('');
|
39 | 44 |
|
40 | 45 | const navItems = [
|
41 |
| - { path: '/', icon: HiHome, label: 'Dashboard' }, |
| 46 | + { path: '/dashboard', icon: HiHome, label: 'Dashboard' }, |
42 | 47 | { path: '/portfolio', icon: HiChartBar, label: 'Portfolio' },
|
43 |
| - { path: '/watchlist', icon: HiStar, label: 'Watchlist' } |
| 48 | + { path: '/watchlist', icon: HiStar, label: 'Watchlist' }, |
| 49 | + { path: '/news', icon: HiNewspaper, label: 'News' }, |
| 50 | + { path: '/calendar', icon: HiCalendar, label: 'Calendar' } |
44 | 51 | ];
|
45 | 52 |
|
46 |
| - const randomStocks = [ |
47 |
| - { name: 'Apple Inc.', ticker: 'AAPL', buy_price: 175.50, targetPrice: 200.00, current_price: 175.50 }, |
48 |
| - { name: 'Microsoft Corporation', ticker: 'MSFT', buy_price: 340.20, targetPrice: 380.00, current_price: 340.20 }, |
49 |
| - { name: 'Amazon.com Inc.', ticker: 'AMZN', buy_price: 125.30, targetPrice: 150.00, current_price: 125.30 }, |
50 |
| - { name: 'Alphabet Inc.', ticker: 'GOOGL', buy_price: 135.60, targetPrice: 160.00, current_price: 135.60 }, |
51 |
| - { name: 'NVIDIA Corporation', ticker: 'NVDA', buy_price: 450.80, targetPrice: 500.00, current_price: 450.80 }, |
52 |
| - { name: 'Meta Platforms Inc.', ticker: 'META', buy_price: 290.40, targetPrice: 320.00, current_price: 290.40 }, |
53 |
| - { name: 'Tesla Inc.', ticker: 'TSLA', buy_price: 240.50, targetPrice: 280.00, current_price: 240.50 }, |
54 |
| - { name: 'Netflix Inc.', ticker: 'NFLX', buy_price: 385.70, targetPrice: 420.00, current_price: 385.70 }, |
55 |
| - { name: 'Adobe Inc.', ticker: 'ADBE', buy_price: 420.30, targetPrice: 460.00, current_price: 420.30 }, |
56 |
| - { name: 'Salesforce Inc.', ticker: 'CRM', buy_price: 210.90, targetPrice: 240.00, current_price: 210.90 } |
57 |
| - ]; |
| 53 | + |
58 | 54 |
|
59 | 55 | const handleLogout = async () => {
|
60 |
| - if (window.confirm('Are you sure you want to logout? This will reset your portfolio..')) { |
| 56 | + if (window.confirm('Are you sure you want to logout?')) { |
61 | 57 | try {
|
62 | 58 | setIsLoading(true);
|
63 |
| - setLoadingMessage('Resetting your portfolio...'); |
64 |
| - |
65 |
| - // Get all current stocks |
66 |
| - const portfolioResponse = await api.get('/stocks'); |
67 |
| - const portfolioStocks = portfolioResponse.data || []; |
| 59 | + setLoadingMessage('Logging out...'); |
68 | 60 |
|
69 |
| - console.log('Current portfolio stocks:', portfolioStocks); |
70 |
| - |
71 |
| - // Delete all current stocks if there are any |
72 |
| - if (portfolioStocks.length > 0) { |
73 |
| - await Promise.all(portfolioStocks.map(stock => |
74 |
| - api.delete(`/stocks/${stock.id}`) |
75 |
| - )); |
76 |
| - console.log('Successfully deleted all portfolio stocks'); |
| 61 | + const { error } = await signOut(); |
| 62 | + if (error) { |
| 63 | + throw error; |
77 | 64 | }
|
78 |
| - |
79 |
| - // Shuffle and pick 5 random stocks |
80 |
| - const shuffled = [...randomStocks].sort(() => 0.5 - Math.random()); |
81 |
| - const selectedStocks = shuffled.slice(0, 5); |
82 |
| - console.log('Selected stocks to add:', selectedStocks); |
83 |
| - |
84 |
| - // Keep track of successful and failed additions |
85 |
| - const results = { |
86 |
| - successful: [], |
87 |
| - failed: [] |
88 |
| - }; |
89 |
| - |
90 |
| - // Add the random stocks one by one |
91 |
| - for (const stock of selectedStocks) { |
92 |
| - try { |
93 |
| - console.log('Adding stock:', stock); |
94 |
| - |
95 |
| - // Add to portfolio with all required fields |
96 |
| - const portfolioStock = { |
97 |
| - name: stock.name, |
98 |
| - ticker: stock.ticker, |
99 |
| - shares: 1, |
100 |
| - buy_price: parseFloat(stock.buy_price), |
101 |
| - current_price: parseFloat(stock.current_price), |
102 |
| - target_price: parseFloat(stock.targetPrice) |
103 |
| - }; |
104 |
| - |
105 |
| - // Add to portfolio |
106 |
| - const response = await api.post('/stocks', portfolioStock); |
107 |
| - console.log(`Successfully added stock ${stock.ticker}`); |
108 |
| - results.successful.push(stock.ticker); |
109 |
| - } catch (error) { |
110 |
| - console.error(`Error adding stock ${stock.ticker}:`, error); |
111 |
| - results.failed.push(stock.ticker); |
112 |
| - |
113 |
| - // If a stock fails, try to add a different one from our list |
114 |
| - const remainingStocks = randomStocks.filter(s => |
115 |
| - !selectedStocks.includes(s) && |
116 |
| - !results.successful.includes(s.ticker) && |
117 |
| - !results.failed.includes(s.ticker) |
118 |
| - ); |
119 |
| - |
120 |
| - if (remainingStocks.length > 0) { |
121 |
| - const replacementStock = remainingStocks[0]; |
122 |
| - console.log(`Trying replacement stock: ${replacementStock.ticker}`); |
123 |
| - try { |
124 |
| - const portfolioStock = { |
125 |
| - name: replacementStock.name, |
126 |
| - ticker: replacementStock.ticker, |
127 |
| - shares: 1, |
128 |
| - buy_price: parseFloat(replacementStock.buy_price), |
129 |
| - current_price: parseFloat(replacementStock.current_price), |
130 |
| - target_price: parseFloat(replacementStock.targetPrice) |
131 |
| - }; |
132 |
| - |
133 |
| - const response = await api.post('/stocks', portfolioStock); |
134 |
| - console.log(`Successfully added replacement stock ${replacementStock.ticker}`); |
135 |
| - results.successful.push(replacementStock.ticker); |
136 |
| - } catch (retryError) { |
137 |
| - console.error(`Error adding replacement stock ${replacementStock.ticker}:`, retryError); |
138 |
| - results.failed.push(replacementStock.ticker); |
139 |
| - } |
140 |
| - } |
141 |
| - } |
142 |
| - } |
143 |
| - |
144 |
| - // Check if we have 5 successful additions |
145 |
| - if (results.successful.length === 5) { |
146 |
| - console.log('Successfully added all 5 stocks:', results.successful); |
147 |
| - } else { |
148 |
| - console.warn(`Only added ${results.successful.length} stocks successfully:`, results.successful); |
149 |
| - console.warn('Failed stocks:', results.failed); |
150 |
| - throw new Error(`Could only add ${results.successful.length} out of 5 stocks`); |
151 |
| - } |
152 |
| - |
153 |
| - // Small delay before refreshing |
154 |
| - await new Promise(resolve => setTimeout(resolve, 1000)); |
155 | 65 |
|
156 |
| - // Refresh the page |
157 |
| - window.location.reload(); |
| 66 | + // Navigate to homepage |
| 67 | + navigate('/'); |
158 | 68 | } catch (error) {
|
159 | 69 | console.error('Error during logout:', error);
|
160 | 70 | setLoadingMessage('Error occurred. Please try again.');
|
161 | 71 | await new Promise(resolve => setTimeout(resolve, 2000));
|
162 | 72 | setIsLoading(false);
|
163 |
| - alert(error.message || 'Failed to logout. Please try again.'); |
| 73 | + alert('Failed to logout. Please try again.'); |
164 | 74 | }
|
165 | 75 | }
|
166 | 76 | };
|
|
0 commit comments