widget news fetch 3
This commit is contained in:
parent
96fbc11509
commit
5453b7ef15
@ -24,7 +24,48 @@ interface NewsItem {
|
|||||||
symbol: string | null;
|
symbol: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mock data for testing - remove this in production
|
||||||
|
const MOCK_NEWS = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
title: "New Project Management Features Released",
|
||||||
|
url: "#",
|
||||||
|
date: "2024-03-20",
|
||||||
|
source: "Internal",
|
||||||
|
description: "New features added to improve project management workflow",
|
||||||
|
category: "Update",
|
||||||
|
sentiment: { score: null, label: null },
|
||||||
|
symbols: null,
|
||||||
|
symbol: null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
title: "Team Meeting Schedule Changes",
|
||||||
|
url: "#",
|
||||||
|
date: "2024-03-19",
|
||||||
|
source: "Internal",
|
||||||
|
description: "Updates to the team meeting schedule",
|
||||||
|
category: "Announcement",
|
||||||
|
sentiment: { score: null, label: null },
|
||||||
|
symbols: null,
|
||||||
|
symbol: null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
title: "Upcoming Training Sessions",
|
||||||
|
url: "#",
|
||||||
|
date: "2024-03-18",
|
||||||
|
source: "Internal",
|
||||||
|
description: "Schedule for upcoming training sessions",
|
||||||
|
category: "Training",
|
||||||
|
sentiment: { score: null, label: null },
|
||||||
|
symbols: null,
|
||||||
|
symbol: null
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
export function News() {
|
export function News() {
|
||||||
|
console.log('News component mounted');
|
||||||
const [news, setNews] = useState<NewsItem[]>([]);
|
const [news, setNews] = useState<NewsItem[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
@ -32,28 +73,46 @@ export function News() {
|
|||||||
const [dbStatus, setDbStatus] = useState<'connecting' | 'connected' | 'error'>('connecting');
|
const [dbStatus, setDbStatus] = useState<'connecting' | 'connected' | 'error'>('connecting');
|
||||||
|
|
||||||
const fetchNews = async (isRefresh = false) => {
|
const fetchNews = async (isRefresh = false) => {
|
||||||
|
console.log('Fetching news...');
|
||||||
if (isRefresh) setRefreshing(true);
|
if (isRefresh) setRefreshing(true);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setDbStatus('connecting');
|
setDbStatus('connecting');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Making API request to /api/news');
|
||||||
const response = await fetch('/api/news');
|
const response = await fetch('/api/news');
|
||||||
|
console.log('API response status:', response.status);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
console.log('API request failed');
|
||||||
throw new Error('Failed to fetch news');
|
throw new Error('Failed to fetch news');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log('API response data:', data);
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
|
console.log('API returned error:', data.error);
|
||||||
throw new Error(data.error);
|
throw new Error(data.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
setNews(data.news);
|
// If no news items returned, use mock data for testing
|
||||||
|
if (!data.news || data.news.length === 0) {
|
||||||
|
console.log('No news items returned, using mock data');
|
||||||
|
setNews(MOCK_NEWS);
|
||||||
|
} else {
|
||||||
|
console.log('Setting news items from API');
|
||||||
|
setNews(data.news);
|
||||||
|
}
|
||||||
|
|
||||||
setError(null);
|
setError(null);
|
||||||
setDbStatus('connected');
|
setDbStatus('connected');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('Error details:', err);
|
||||||
setError('Failed to load news. Please try again later.');
|
setError('Failed to load news. Please try again later.');
|
||||||
setDbStatus('error');
|
setDbStatus('error');
|
||||||
console.error('Error fetching news:', err);
|
console.log('Using mock data due to error');
|
||||||
|
setNews(MOCK_NEWS);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user