You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
const noteSchema = z.object({
|
|
title: z.string(),
|
|
description: z.string(),
|
|
})
|
|
|
|
const submitNote = async (note: unknown, noteId: string | string[]) => {
|
|
const parsedNote = noteSchema.safeParse(note)
|
|
|
|
if (!parsedNote.success) return
|
|
|
|
let response = {
|
|
status: 200,
|
|
message: 'Update',
|
|
error: null,
|
|
}
|
|
|
|
await useFetch('/api/note', {
|
|
method: 'POST',
|
|
query: { id: noteId },
|
|
body: parsedNote.data,
|
|
onResponse({ response }) {
|
|
console.log('POST:', response._data.message)
|
|
response = response._data
|
|
},
|
|
})
|
|
|
|
return response
|
|
}
|
|
|
|
const deleteNote = async (noteId: string | string[]) => {
|
|
let response
|
|
|
|
await useFetch('/api/note', {
|
|
method: 'DELETE',
|
|
query: {
|
|
id: noteId,
|
|
},
|
|
onResponse({ response }) {
|
|
console.log('DELETE:', response._data.message)
|
|
response = response._data
|
|
},
|
|
})
|
|
|
|
return response
|
|
}
|
|
|
|
type Note = z.infer<typeof noteSchema>
|
|
|
|
const useNote = () =>
|
|
useState<Note>('note', () => Object({ title: '', description: '' }))
|
|
|
|
const useDescription = () => useState<string>('description', () => '')
|
|
|
|
export { deleteNote, submitNote, useDescription, useNote }
|