MCPcopy Create free account
hub / github.com/chokcoco/iCSS / fetchArticles

Function fetchArticles

website/app/page.tsx:36–81  ·  view source on GitHub ↗
(page: number = 1, append: boolean = false)

Source from the content-addressed store, hash-verified

34
35 // 获取文章数据
36 const fetchArticles = async (page: number = 1, append: boolean = false) => {
37 try {
38 const params = new URLSearchParams({
39 page: page.toString(),
40 per_page: '12'
41 });
42
43 if (selectedCategory !== '全部') {
44 params.append('category', selectedCategory);
45 }
46
47 if (searchTerm) {
48 params.append('search', searchTerm);
49 }
50
51 const response = await fetch(`/api/articles?${params}`);
52 const data = await response.json();
53 console.log('Articles API response:', data);
54
55 // 检查是否有错误信息
56 if (data.error) {
57 console.error('API returned error:', data.error);
58 setError(data.error);
59 setLoading(false);
60 return;
61 }
62
63 // 确保数据格式正确
64 const articles = Array.isArray(data.articles) ? data.articles : [];
65 const pagination = data.pagination || { hasNext: false };
66 console.log('Processed articles:', articles.length, 'pagination:', pagination);
67
68 if (append) {
69 setArticles(prev => [...prev, ...articles]);
70 } else {
71 setArticles(articles);
72 }
73
74 setHasMore(pagination.hasNext);
75 setLoading(false);
76 } catch (err) {
77 console.error('Failed to fetch articles:', err);
78 setError('网络连接失败,请检查网络后重试');
79 setLoading(false);
80 }
81 };
82
83 // 获取分类数据
84 const fetchCategories = async () => {

Callers 2

HomePageFunction · 0.85
loadMoreFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected