From 10fad65823a0464b0b600c00f39f921d4823ce5b 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: Mon, 9 Oct 2023 17:21:27 -0400 Subject: [PATCH] feat(client-side): add debounced autosave function for story form --- lib/client/utils.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/client/utils.ts diff --git a/lib/client/utils.ts b/lib/client/utils.ts new file mode 100644 index 0000000..20756aa --- /dev/null +++ b/lib/client/utils.ts @@ -0,0 +1,21 @@ +import { debounce } from "lodash-es"; +import { useAutoSaveStore } from "~/stores/autosaveStore"; + +export const autoSave = debounce((values: any) => { + const store = useAutoSaveStore(); + if (store.$state.draftId == undefined) { + useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", { + method: "post", + body: values, + }).then(({ data, error }) => { + if (data.value) { + store.$patch({ draftId: data.value.draftId }); + } + }); + } else { + useApiFetch(`/drafts/${store.$state.draftId}`, { + method: "put", + body: values, + }); + } +}, 3000);