feat(posts): wire community pages to backend posts API
This commit is contained in:
+67
-32
@@ -1,43 +1,78 @@
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
import { isApiError } from "@/lib/errors"
|
||||
import type { Post } from "@/lib/types"
|
||||
|
||||
export function listPosts() {
|
||||
return usePostStore.getState().posts
|
||||
}
|
||||
import { httpJson } from "./http"
|
||||
|
||||
export function getPostById(postId: string) {
|
||||
return usePostStore.getState().posts.find((post) => post.id === postId)
|
||||
}
|
||||
|
||||
export function listPostsByAuthor(userId: string) {
|
||||
return usePostStore.getState().posts.filter((post) => post.author.id === userId)
|
||||
}
|
||||
|
||||
export function togglePostLike(postId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny(401, "请先登录")
|
||||
type Paginated<T> = {
|
||||
items: T[]
|
||||
meta: {
|
||||
total: number
|
||||
offset: number
|
||||
limit: number
|
||||
}
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny(404, "帖子不存在")
|
||||
}
|
||||
type ListOptions = {
|
||||
offset?: number
|
||||
limit?: number
|
||||
}
|
||||
|
||||
const shouldNotify = !post.liked
|
||||
function withOffsetLimit(path: string, options?: ListOptions): string {
|
||||
const offset = options?.offset ?? 0
|
||||
const limit = options?.limit ?? 1000
|
||||
|
||||
usePostStore.getState().togglePostLike(postId)
|
||||
const searchParams = new URLSearchParams({
|
||||
offset: String(offset),
|
||||
limit: String(limit),
|
||||
})
|
||||
return `${path}?${searchParams.toString()}`
|
||||
}
|
||||
|
||||
if (shouldNotify) {
|
||||
addNotification({
|
||||
type: "community",
|
||||
title: "帖子收到点赞",
|
||||
content: `《${post.title}》有新的点赞`,
|
||||
link: `/post/${post.id}`,
|
||||
export async function listPosts(options?: ListOptions): Promise<Post[]> {
|
||||
const res = await httpJson<Paginated<Post>>(withOffsetLimit("/api/v1/posts", options), {
|
||||
cache: "no-store",
|
||||
})
|
||||
return res.items
|
||||
}
|
||||
|
||||
export async function getPostById(postId: string): Promise<Post | undefined> {
|
||||
try {
|
||||
return await httpJson<Post>(`/api/v1/posts/${encodeURIComponent(postId)}`, {
|
||||
cache: "no-store",
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === "UNAUTHORIZED") {
|
||||
throw error
|
||||
}
|
||||
if (isApiError(error) && error.code === 404) {
|
||||
return undefined
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function listPostsByAuthor(userId: string, options?: ListOptions): Promise<Post[]> {
|
||||
const res = await httpJson<Paginated<Post>>(
|
||||
withOffsetLimit(`/api/v1/users/${encodeURIComponent(userId)}/posts`, options),
|
||||
{
|
||||
cache: "no-store",
|
||||
},
|
||||
)
|
||||
return res.items
|
||||
}
|
||||
|
||||
export async function togglePostLike(postId: string, currentlyLiked: boolean): Promise<void> {
|
||||
const encodedId = encodeURIComponent(postId)
|
||||
if (currentlyLiked) {
|
||||
await httpJson<unknown>(`/api/v1/posts/${encodedId}/like`, {
|
||||
method: "DELETE",
|
||||
cache: "no-store",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
return allow()
|
||||
await httpJson<unknown>(`/api/v1/posts/${encodedId}/like`, {
|
||||
method: "POST",
|
||||
cache: "no-store",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user