From 08ce2175cd6e85ff57dd4c2620164b28ec55436a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 30 Dec 2023 17:20:32 -0500 Subject: [PATCH] refactor(client-side): make autoEdit and autoSave functions fully async --- lib/client/utils.ts | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/client/utils.ts b/lib/client/utils.ts index 80b55f3..5bd5cd6 100644 --- a/lib/client/utils.ts +++ b/lib/client/utils.ts @@ -9,36 +9,33 @@ export const autoSave = async (values: any) => { if (store.$state.fetchId !== fid) return; store.$patch({ fetchId: store.$state.fetchId + 1 }); if (store.$state.draftId == undefined) { - let b = useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", { + let { data, error } = await useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", { method: "post", body: values, - }).then(({ data, error }) => { - console.log("fibberty", data, error); - if (data.value) { - store.$patch({ draftId: data.value.draftId }); - } }); - console.log("B", b); + console.log("fibberty", data, error); + if (data.value) { + store.$patch({ draftId: data.value.draftId }); + } } else { - useApiFetch(`/drafts/${store.$state.draftId}`, { + await useApiFetch(`/drafts/${store.$state.draftId}`, { method: "put", body: values, }); } }; -export const autoEdit = (values: any, endpoint: string, method: "put" | "post") => { - const [messageApi, contextHolder] = message.useMessage(); - useApiFetch<{ success: boolean; data: IStory }>(endpoint, { +export const autoEdit = async (values: any, endpoint: string, method: "put" | "post") => { + const [messageApi] = message.useMessage(); + const { data, error } = await useApiFetch<{ success: boolean; data: IStory }>(endpoint, { method, body: values, - }).then(({ data, error }) => { - if (data.value?.success) { - messageApi.success("Your work has been saved successfully."); - } else if (error) { - messageApi.error("Error saving data."); - } }); + if (data.value?.success) { + messageApi.success("Your work has been saved successfully."); + } else if (error) { + messageApi.error("Error saving data."); + } }; export const debouncedAutoEdit = debounce(autoEdit, 5000);