Frontend: removed title from zod post schema

main
TZGyn 2 years ago
parent fadc2ee071
commit 586c03c935
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -1,30 +1,29 @@
import { z } from 'zod' import { z } from 'zod'
const PostValidator = z.object({ const PostValidator = z.object({
sequence: z.number(), sequence: z.number(),
id: z.string(), id: z.string(),
title: z.string(), description: z.string(),
description: z.string(), created_at: z.string().datetime(),
created_at: z.string().datetime(), updated_at: z.string(),
updated_at: z.string(), user: z.object({
user: z.object({ name: z.string(),
name: z.string(), }),
}),
}) })
const newPostValidator = z.object({ const newPostValidator = z.object({
title: z.string(), title: z.string(),
description: z.string(), description: z.string(),
}) })
const parsePost = (data: unknown) => { const parsePost = (data: unknown) => {
return PostValidator.parse(data) return PostValidator.parse(data)
} }
const PostsValidator = PostValidator.array() const PostsValidator = PostValidator.array()
const parsePosts = (data: unknown[]) => { const parsePosts = (data: unknown[]) => {
return PostsValidator.parse(data) return PostsValidator.parse(data)
} }
type Posts = z.infer<typeof PostsValidator> type Posts = z.infer<typeof PostsValidator>
@ -32,55 +31,55 @@ type Posts = z.infer<typeof PostsValidator>
const usePosts = () => useState<Posts>(() => []) const usePosts = () => useState<Posts>(() => [])
const createNewPost = async (data: unknown) => { const createNewPost = async (data: unknown) => {
const parsedNewPost = newPostValidator.safeParse(data) const parsedNewPost = newPostValidator.safeParse(data)
if (!parsedNewPost.success) return if (!parsedNewPost.success) return
await useCustomFetch('/api/posts', { await useCustomFetch('/api/posts', {
method: 'POST', method: 'POST',
body: parsedNewPost.data, body: parsedNewPost.data,
}) })
await fetchPosts('new') await fetchPosts('new')
} }
const responseValidator = z.object({ const responseValidator = z.object({
status: z.number(), status: z.number(),
data: z.unknown().array(), data: z.unknown().array(),
}) })
const fetchPosts = async ( const fetchPosts = async (
order: 'new' | 'old' = 'old', order: 'new' | 'old' = 'old',
refresh: Boolean = false 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 firstPost = posts.value.length ? posts.value[0].sequence : 0
const { data } = await useCustomFetch('/api/posts', { const { data } = await useCustomFetch('/api/posts', {
params: { lastPost: lastPost, firstPost: firstPost, order: order }, 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
if (order == 'old') { if (order == 'old') {
posts.value = [...posts.value, ...parsePosts(response.data.data)] posts.value = [...posts.value, ...parsePosts(response.data.data)]
} else { } else {
posts.value = [...parsePosts(response.data.data), ...posts.value] posts.value = [...parsePosts(response.data.data), ...posts.value]
} }
} }
export { export {
createNewPost, createNewPost,
fetchPosts, fetchPosts,
parsePost, parsePost,
parsePosts, parsePosts,
PostValidator, PostValidator,
usePosts, usePosts,
} }

Loading…
Cancel
Save