Skip to content

Commit

Permalink
handling on click feature for stock buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
eethansmith committed Dec 4, 2023
1 parent 38f94ce commit 0ff0e27
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import React, { useState } from 'react';
import './App.css';
import homeImage from './menu-con.jpg';
import StockHoldings from './StockHoldings'; // Make sure this path is correct
import StockHoldings from './StockHoldings';
import StockGraph from './StockGraphing';

function App() {
const [activeTab, setActiveTab] = useState('Overall Portfolio'); // default active tab
const [selectedTicker, setSelectedTicker] = useState(null);

return (
<div className="App">
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/StockGraphing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useState, useEffect } from 'react';
import { Line } from 'react-chartjs-2';
import 'chart.js/auto';

function StockGraph({ ticker }) { // Use destructuring to get the ticker prop
const [stockData, setStockData] = useState([]);

useEffect(() => {
// Ensure the ticker value is included in the fetch URL
fetch(`http://localhost:8000/api/graph_stock/${ticker}/`)
.then(response => response.json())
.then(data => setStockData(data))
.catch(error => console.error('Error fetching data:', error));
}, [ticker]); // Include ticker in the dependency array

const chartData = {
labels: stockData.map(data => data.date),
datasets: [{
label: `${ticker} Stock Value`, // Dynamic label based on ticker
data: stockData.map(data => data.value),
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
};

return <Line data={chartData} />;
}

export default StockGraph;
6 changes: 5 additions & 1 deletion frontend/src/StockHoldings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function StockHoldings() {
}
};

const handleStockClick = (ticker) => {
onStockSelect(ticker);
};

// useEffect to call the fetch function when the component mounts
useEffect(() => {
fetchStockHoldings();
Expand Down Expand Up @@ -87,7 +91,7 @@ function StockHoldings() {
<div className="stock-holdings">
<h2>Current Stock Holdings</h2>
{holdings.map((stock, index) => (
<button key={index} className="stock-button">
<button key={index} className="stock-button" onClick={() => handleStockClick(stock.ticker)}>
<img src={getImageUrl(stock.ticker)} alt={stock.name} className="stock-image" />
<div className="stock-details">
<div className="stock-name">{stock.name}</div>
Expand Down

0 comments on commit 0ff0e27

Please sign in to comment.