news widget redesign 4
This commit is contained in:
parent
654cc641e2
commit
64abcfc1c2
@ -7,11 +7,15 @@ const API_URL = 'http://172.16.0.104:8000';
|
|||||||
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
||||||
try {
|
try {
|
||||||
const date = new Date(dateStr);
|
const date = new Date(dateStr);
|
||||||
|
|
||||||
|
// Format like "17 avr." to match the Duties widget style
|
||||||
const day = date.getDate();
|
const day = date.getDate();
|
||||||
const month = date.toLocaleString('fr-FR', { month: 'short' }).toLowerCase();
|
const month = date.toLocaleString('fr-FR', { month: 'short' })
|
||||||
|
.toLowerCase()
|
||||||
|
.replace('.', ''); // Remove the dot that comes with French locale
|
||||||
|
|
||||||
return {
|
return {
|
||||||
displayDate: `${day} ${month}.`,
|
displayDate: `${day} ${month}.`, // Add the dot back for consistent styling
|
||||||
timestamp: date.toLocaleString('fr-FR', {
|
timestamp: date.toLocaleString('fr-FR', {
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
month: 'short',
|
month: 'short',
|
||||||
@ -35,7 +39,7 @@ function truncateText(text: string, maxLength: number): string {
|
|||||||
|
|
||||||
// Helper function to format category
|
// Helper function to format category
|
||||||
function formatCategory(category: string): string {
|
function formatCategory(category: string): string {
|
||||||
if (!category) return 'GENERAL';
|
if (!category) return null; // Return null to match the interface
|
||||||
const categoryMap: { [key: string]: string } = {
|
const categoryMap: { [key: string]: string } = {
|
||||||
'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD',
|
'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD',
|
||||||
'UN NEWS - GLOBAL NEWS': 'UN NEWS',
|
'UN NEWS - GLOBAL NEWS': 'UN NEWS',
|
||||||
@ -58,6 +62,17 @@ function formatSource(source: string): string {
|
|||||||
return sourceName.charAt(0).toUpperCase() + sourceName.slice(1);
|
return sourceName.charAt(0).toUpperCase() + sourceName.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface NewsItem {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
displayDate: string;
|
||||||
|
timestamp: string;
|
||||||
|
source: string;
|
||||||
|
description: string | null;
|
||||||
|
category: string | null;
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
console.log('Fetching news from FastAPI server...');
|
console.log('Fetching news from FastAPI server...');
|
||||||
@ -75,12 +90,12 @@ export async function GET() {
|
|||||||
|
|
||||||
const articles = await response.json();
|
const articles = await response.json();
|
||||||
|
|
||||||
const formattedNews = articles.map((article: any) => {
|
const formattedNews: NewsItem[] = articles.map((article: any) => {
|
||||||
const { displayDate, timestamp } = formatDateTime(article.date);
|
const { displayDate, timestamp } = formatDateTime(article.date);
|
||||||
return {
|
return {
|
||||||
id: article.id,
|
id: article.id,
|
||||||
title: truncateText(article.title, 70),
|
title: truncateText(article.title, 70),
|
||||||
description: truncateText(article.description, 100),
|
description: article.description ? truncateText(article.description, 100) : null,
|
||||||
displayDate,
|
displayDate,
|
||||||
timestamp,
|
timestamp,
|
||||||
source: formatSource(article.source),
|
source: formatSource(article.source),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user