Added fronted newPostValidator and usePosts composables
parent
67d39b0478
commit
4526e9fae9
@ -1,22 +1,41 @@
|
|||||||
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(),
|
title: z.string(),
|
||||||
description: z.string(),
|
description: z.string(),
|
||||||
created_at: z.string(),
|
created_at: z.string(),
|
||||||
updated_at: z.string(),
|
updated_at: z.string(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const newPostValidator = z.object({
|
||||||
|
title: 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>
|
||||||
|
|
||||||
|
const usePosts = () => useState<Posts>(() => [])
|
||||||
|
|
||||||
|
const createNewPost = async (data: unknown) => {
|
||||||
|
const parsedNewPost = newPostValidator.safeParse(data)
|
||||||
|
|
||||||
|
if (!parsedNewPost.success) return
|
||||||
|
|
||||||
|
const res = await request.post('/api/posts', parsedNewPost.data)
|
||||||
|
|
||||||
|
console.log(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
export { parsePost, parsePosts, PostsValidator, PostValidator }
|
export { parsePost, parsePosts, PostValidator, createNewPost, usePosts }
|
||||||
|
|||||||
Loading…
Reference in New Issue