Skip to content

Commit 4568cd4

Browse files
authored
Merge pull request #100 from Intelligent-Advisor-Sem-4/fixing-fetching
fixing-fetching
2 parents fc494a1 + c95f6c0 commit 4568cd4

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use server';
2+
3+
import AxiosInstance from '@/lib/server-fetcher';
4+
5+
export async function fetchStockPrediction({
6+
starting_date,
7+
ending_date,
8+
ticker_symbol,
9+
}: {
10+
starting_date: string;
11+
ending_date: string;
12+
ticker_symbol: string;
13+
}) {
14+
const response = await AxiosInstance.post('/V2/get-predicted-prices', {
15+
starting_date,
16+
ending_date,
17+
ticker_symbol,
18+
});
19+
return response.data;
20+
}

src/app/(dashboard)/dashboard/stockmarketprediction/_components/stock-dashboard.tsx

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import StockDataTable from "./stock-data-table"
1515
import ModelMetadata from "./model-metadata"
1616
//import {getStockData, getPredictionData, getModelMetadata} from "@/lib/data"
1717
import {exportToCSV} from "@/lib/export"
18-
import {BACKEND_BASE_URL} from "@/lib/const"
1918
import {PredictionData, StockData, ModelMetadataType} from "@/lib/types/stock_prediction";
2019
import AxiosInstance from "@/lib/client-fetcher";
20+
import {AxiosError} from "axios";
21+
import {fetchStockPrediction} from "@/app/(dashboard)/dashboard/stockmarketprediction/_actions/action";
2122
// import { number } from "zod"
2223

2324
type companyType = {
@@ -34,7 +35,6 @@ const formatDate = (date: Date) => {
3435
return date.toLocaleDateString("en-CA"); // "YYYY-MM-DD" format
3536
};
3637

37-
const BASE_URL = BACKEND_BASE_URL
3838

3939
export default function StockDashboard() {
4040
const [selectedCompany, setSelectedCompany] = useState<string>("")
@@ -59,7 +59,7 @@ export default function StockDashboard() {
5959
const fetchCompanies = async () => {
6060
try {
6161
setIsLoading(true);
62-
const response = await AxiosInstance.get(`${BASE_URL}/get-active-symbols`);
62+
const response = await AxiosInstance.get(`/get-active-symbols`);
6363
const data = response.data;
6464
setCompanies(data.symbols);
6565
setSelectedCompany(data.symbols[0].value);
@@ -80,44 +80,40 @@ export default function StockDashboard() {
8080
const fetchStockData = async () => {
8181
setPredicting(true); // Show loading popup
8282
try {
83-
const response = await fetch(`${BASE_URL}/V2/get-predicted-prices`, {
84-
method: "POST",
85-
credentials: "include",
86-
headers: {
87-
"Content-Type": "application/json",
88-
},
89-
body: JSON.stringify({
90-
starting_date: formatDate(dateRange.from ?? new Date()),
91-
ending_date: formatDate(dateRange.to ?? new Date()),
92-
ticker_symbol: selectedCompany,
93-
}),
83+
const data = await fetchStockPrediction({
84+
starting_date: formatDate(dateRange.from ?? new Date()),
85+
ending_date: formatDate(dateRange.to ?? new Date()),
86+
ticker_symbol: selectedCompany,
9487
});
95-
if (response.ok) {
96-
const data = await response.json();
97-
setStockData(data.stockData);
98-
setModelMetadata(data.modelMetadata);
99-
setPredictionData(data.predictionData)
100-
if (data.predictionData && data.stockData && data.stockData.history.length > 0 && data.predictionData.predictions.length > 0) {
101-
const lastPrediction = data.predictionData.predictions[data.predictionData.predictions.length - 1].predicted;
102-
const lastPrice = data.stockData.history[data.stockData.history.length - 1].price;
103-
const percentageChange = ((lastPrediction - lastPrice) / lastPrice) * 100;
104-
setSevchange(percentageChange);
88+
setStockData(data.stockData);
89+
setModelMetadata(data.modelMetadata);
90+
setPredictionData(data.predictionData);
91+
if (
92+
data.predictionData &&
93+
data.stockData &&
94+
data.stockData.history.length > 0 &&
95+
data.predictionData.predictions.length > 0
96+
) {
97+
const lastPrediction = data.predictionData.predictions[data.predictionData.predictions.length - 1].predicted;
98+
const lastPrice = data.stockData.history[data.stockData.history.length - 1].price;
99+
const percentageChange = ((lastPrediction - lastPrice) / lastPrice) * 100;
100+
setSevchange(percentageChange);
101+
}
102+
} catch (error) {
103+
if (error instanceof AxiosError) {
104+
if (error.response && error.response.data && error.response.data.detail) {
105+
setDataerror(error.response.data.detail);
106+
} else {
107+
setDataerror("Resource not found");
105108
}
106-
107-
} else {
108-
const result = await response.json();
109-
setDataerror(result.detail || "Resource not found");
110-
return;
111109
}
112-
113-
} catch {
114-
console.error("Error fetching stock data");
110+
console.error("Error fetching stock data", error);
115111
} finally {
116112
setPredicting(false); // Hide loading popup
117113
}
118114
};
119115

120-
fetchStockData();
116+
fetchStockData().then();
121117

122118
// Fetch prediction data
123119
// const predictions = getPredictionData(selectedCompany, dateRange.to);

0 commit comments

Comments
 (0)