2023-10-09 17:21:27 -04:00
|
|
|
import { debounce } from "lodash-es";
|
2023-10-11 16:38:20 -04:00
|
|
|
import { message } from "ant-design-vue";
|
|
|
|
import { IStory } from "~/models/stories";
|
2023-10-09 17:21:27 -04:00
|
|
|
import { useAutoSaveStore } from "~/stores/autosaveStore";
|
|
|
|
|
2023-10-12 22:56:09 -04:00
|
|
|
export const autoSave = debounce(async (values: any) => {
|
2023-10-09 17:21:27 -04:00
|
|
|
const store = useAutoSaveStore();
|
2023-10-12 22:56:09 -04:00
|
|
|
const fid = store.$state.fetchId;
|
|
|
|
if (store.$state.fetchId !== fid) return;
|
|
|
|
store.$patch({ fetchId: store.$state.fetchId + 1 });
|
2023-10-09 17:21:27 -04:00
|
|
|
if (store.$state.draftId == undefined) {
|
2023-10-12 22:56:09 -04:00
|
|
|
let b = useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", {
|
2023-10-09 17:21:27 -04:00
|
|
|
method: "post",
|
|
|
|
body: values,
|
|
|
|
}).then(({ data, error }) => {
|
2023-10-12 22:56:09 -04:00
|
|
|
console.log("fibberty", data, error);
|
2023-10-09 17:21:27 -04:00
|
|
|
if (data.value) {
|
|
|
|
store.$patch({ draftId: data.value.draftId });
|
|
|
|
}
|
|
|
|
});
|
2023-10-12 22:56:09 -04:00
|
|
|
console.log("B", b);
|
2023-10-09 17:21:27 -04:00
|
|
|
} else {
|
|
|
|
useApiFetch<any>(`/drafts/${store.$state.draftId}`, {
|
|
|
|
method: "put",
|
|
|
|
body: values,
|
|
|
|
});
|
|
|
|
}
|
2023-10-12 22:56:09 -04:00
|
|
|
}, 20000);
|
2023-10-11 16:38:20 -04:00
|
|
|
|
|
|
|
export const autoEdit = debounce(
|
|
|
|
(values: any, endpoint: string, method: "put" | "post") => {
|
|
|
|
const [messageApi, contextHolder] = message.useMessage();
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
10000,
|
|
|
|
);
|