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);