All files / src/components FeedList.tsx

94.44% Statements 17/18
83.33% Branches 10/12
100% Functions 6/6
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52            9x 9x 9x   9x 5x   3x     3x     3x 3x     1x 1x       9x 4x   3x               2x                        
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import type { Feed } from '../types';
import './FeedList.css';
 
export default function FeedList() {
    const [feeds, setFeeds] = useState<Feed[]>([]);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState('');
 
    useEffect(() => {
        fetch('/api/feed/')
            .then((res) => {
                Iif (!res.ok) {
                    throw new Error('Failed to fetch feeds');
                }
                return res.json();
            })
            .then((data) => {
                setFeeds(data);
                setLoading(false);
            })
            .catch((err) => {
                setError(err.message);
                setLoading(false);
            });
    }, []);
 
    if (loading) return <div className="feed-list-loading">Loading feeds...</div>;
    if (error) return <div className="feed-list-error">Error: {error}</div>;
 
    return (
        <div className="feed-list">
            <h2>Feeds</h2>
            {feeds.length === 0 ? (
                <p>No feeds found.</p>
            ) : (
                <ul className="feed-list-items">
                    {feeds.map((feed) => (
                        <li key={feed._id} className="feed-item">
                            <Link to={`/feed/${feed._id}`} className="feed-title">
                                {feed.title || feed.url}
                            </Link>
                            {feed.category && <span className="feed-category">{feed.category}</span>}
                        </li>
                    ))}
                </ul>
            )}
        </div>
    );
}