feat(news): implement news section with loading state and error handling in NoLoginView
All checks were successful
Deploy to production / deploy (push) Successful in 3m8s

This commit is contained in:
Torsten Schulz (local)
2026-09-03 14:18:14 +02:00
parent c4977ec826
commit d9a46dcb34
9 changed files with 168 additions and 9 deletions

View File

@@ -7,11 +7,12 @@ import newsService from '../services/newsService.js';
export default {
async getNews(req, res) {
const counter = Math.max(0, parseInt(req.query.counter, 10) || 0);
const count = Math.min(6, Math.max(1, parseInt(req.query.count, 10) || 1));
const language = (req.query.language || 'de').slice(0, 10);
const category = (req.query.category || 'top').slice(0, 50);
try {
const { results, nextPage } = await newsService.getNews({ counter, language, category });
const { results, nextPage } = await newsService.getNews({ counter, count, language, category });
res.json({ results, nextPage });
} catch (error) {
console.error('News getNews:', error);

View File

@@ -1,9 +1,10 @@
import { Router } from 'express';
import { authenticate } from '../middleware/authMiddleware.js';
import newsController from '../controllers/newsController.js';
const router = Router();
router.get('/', authenticate, newsController.getNews.bind(newsController));
// News are shown on the public landing page as well as in the dashboard.
// The endpoint only exposes cached third-party headlines, no user data.
router.get('/', newsController.getNews.bind(newsController));
export default router;

View File

@@ -104,20 +104,21 @@ async function getCachedNews({ language = 'de', category = 'top', minArticles =
* @param {number} options.counter - Index des Artikels (0 = erster, 1 = zweiter, …)
* @param {string} [options.language]
* @param {string} [options.category]
* @param {number} [options.count] - Anzahl aufeinanderfolgender Artikel
* @returns {Promise<{ results: Array, nextPage: string|null }>}
*/
async function getNews({ counter = 0, language = 'de', category = 'top' }) {
async function getNews({ counter = 0, count = 1, language = 'de', category = 'top' }) {
const neededIndex = Math.max(0, counter);
const requestedCount = Math.min(6, Math.max(1, Number.parseInt(count, 10) || 1));
// Mindestens so viele Artikel laden wie benötigt
const articles = await getCachedNews({
language,
category,
minArticles: neededIndex + 1
minArticles: neededIndex + requestedCount
});
const single = articles[neededIndex] ? [articles[neededIndex]] : [];
return { results: single, nextPage: null };
return { results: articles.slice(neededIndex, neededIndex + requestedCount), nextPage: null };
}
export default { getNews };