diff --git a/composables/note.ts b/composables/note.ts new file mode 100644 index 0000000..c9ee26a --- /dev/null +++ b/composables/note.ts @@ -0,0 +1,22 @@ +export const submitNote = async ( + note: Note, + query: NoteQuery +): Promise => { + let response: ApiResponse = { + status: 200, + message: 'Update', + error: null, + }; + + await useFetch('/api/note', { + method: 'POST', + query, + body: note, + onResponse({ response }) { + console.log('POST:', response._data.message); + response = response._data; + }, + }); + + return response; +}; diff --git a/pages/notes/[id].vue b/pages/notes/[id].vue index f3ad8bb..ff0f9a3 100644 --- a/pages/notes/[id].vue +++ b/pages/notes/[id].vue @@ -67,20 +67,8 @@ const submit = async (note: Note) => { isSubmitting.value = true; - await useFetch('/api/note', { - method: 'POST', - query: { - id: route.params.id, - }, - body: { - title: note.title, - description: note.description, - }, - onResponse({ response }) { - isSubmitting.value = false; - console.log('POST:', response._data.message); - }, - }); + await submitNote(note, { id: route.params.id }); + isSubmitting.value = false; }; const deleteNote = async () => { diff --git a/types/global.d.ts b/types/global.d.ts index 3d75b17..4bf1d98 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -8,6 +8,10 @@ declare global { description: string; } + interface NoteQuery { + id: number | string | string[]; + } + interface ApiResponse { status: number; message: string;