feat(client-side): add debounced autosave function for story form

This commit is contained in:
parent 568c921251
commit 10fad65823
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

21
lib/client/utils.ts Normal file

@ -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<any>(`/drafts/${store.$state.draftId}`, {
method: "put",
body: values,
});
}
}, 3000);