|
|
|
@ -5,8 +5,11 @@ const PostValidator = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
id: z.string(),
|
|
|
|
title: z.string(),
|
|
|
|
title: z.string(),
|
|
|
|
description: z.string(),
|
|
|
|
description: z.string(),
|
|
|
|
created_at: z.string(),
|
|
|
|
created_at: z.string().datetime(),
|
|
|
|
updated_at: z.string(),
|
|
|
|
updated_at: z.string(),
|
|
|
|
|
|
|
|
user: z.object({
|
|
|
|
|
|
|
|
name: z.string(),
|
|
|
|
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const newPostValidator = z.object({
|
|
|
|
const newPostValidator = z.object({
|
|
|
|
@ -33,12 +36,12 @@ const createNewPost = async (data: unknown) => {
|
|
|
|
|
|
|
|
|
|
|
|
if (!parsedNewPost.success) return
|
|
|
|
if (!parsedNewPost.success) return
|
|
|
|
|
|
|
|
|
|
|
|
const { data: post } = await useCustomFetch('/api/posts', {
|
|
|
|
await useCustomFetch('/api/posts', {
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
body: parsedNewPost.data,
|
|
|
|
body: parsedNewPost.data,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
await fetchPosts()
|
|
|
|
await fetchPosts('new')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const responseValidator = z.object({
|
|
|
|
const responseValidator = z.object({
|
|
|
|
@ -46,23 +49,31 @@ const responseValidator = z.object({
|
|
|
|
data: z.unknown().array(),
|
|
|
|
data: z.unknown().array(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const fetchPosts = async (refresh: Boolean = false) => {
|
|
|
|
const fetchPosts = async (
|
|
|
|
|
|
|
|
order: 'new' | 'old' = 'old',
|
|
|
|
|
|
|
|
refresh: Boolean = false
|
|
|
|
|
|
|
|
) => {
|
|
|
|
const posts = usePosts()
|
|
|
|
const posts = usePosts()
|
|
|
|
const lastPost = refresh
|
|
|
|
const lastPost = refresh
|
|
|
|
? 0
|
|
|
|
? 0
|
|
|
|
: posts.value.length
|
|
|
|
: posts.value.length
|
|
|
|
? posts.value[posts.value.length - 1].sequence
|
|
|
|
? posts.value[posts.value.length - 1].sequence
|
|
|
|
: 0
|
|
|
|
: 0
|
|
|
|
|
|
|
|
const firstPost = posts.value.length ? posts.value[0].sequence : 0
|
|
|
|
|
|
|
|
|
|
|
|
const { data } = await useCustomFetch('/api/posts', {
|
|
|
|
const { data } = await useCustomFetch('/api/posts', {
|
|
|
|
params: { lastPost: lastPost },
|
|
|
|
params: { lastPost: lastPost, firstPost: firstPost, order: order },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const response = responseValidator.safeParse(data.value)
|
|
|
|
const response = responseValidator.safeParse(data.value)
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.success) return
|
|
|
|
if (!response.success) return
|
|
|
|
|
|
|
|
|
|
|
|
posts.value = [...posts.value, ...parsePosts(response.data.data)]
|
|
|
|
if (order == 'old') {
|
|
|
|
|
|
|
|
posts.value = [...posts.value, ...parsePosts(response.data.data)]
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
posts.value = [...parsePosts(response.data.data), ...posts.value]
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
export {
|
|
|
|
|