next/lib/client/utils.ts

22 lines
569 B
TypeScript

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