Add v2 post cards and home feed logic

This commit is contained in:
LeonspaceX
2026-01-26 16:24:40 +08:00
parent 16b7f30e78
commit 6268ab1544
4 changed files with 464 additions and 2 deletions

View File

@@ -162,3 +162,52 @@ export const createPost = async (content: string): Promise<CreatePostResponse> =
throw error;
}
};
export interface Article {
id: number;
content: string;
upvotes: number;
downvotes: number;
created_at?: string;
comment_count?: number;
total_pages?: number;
}
export const fetchArticles = async (page: number, signal?: AbortSignal): Promise<Article[]> => {
try {
const response = await fetch(`/api/10_info?page=${page}`, { signal });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
if (json.code === 1000 && Array.isArray(json.data)) {
return json.data as Article[];
}
throw new Error('Invalid response code or missing data');
} catch (error) {
console.error('Failed to fetch articles:', error);
throw error;
}
};
export const voteArticle = async (id: number, type: 'up' | 'down'): Promise<void> => {
try {
const response = await fetch(`/api/${type}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id, type: 'submission' }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
if (json.code !== 1000) {
throw new Error('Vote failed');
}
} catch (error) {
console.error('Failed to vote:', error);
throw error;
}
};