refactor(client-side): make autoEdit and autoSave functions fully async

This commit is contained in:
parent aa5dc23dfa
commit 08ce2175cd
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -9,36 +9,33 @@ export const autoSave = async (values: any) => {
if (store.$state.fetchId !== fid) return; if (store.$state.fetchId !== fid) return;
store.$patch({ fetchId: store.$state.fetchId + 1 }); store.$patch({ fetchId: store.$state.fetchId + 1 });
if (store.$state.draftId == undefined) { if (store.$state.draftId == undefined) {
let b = useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", { let { data, error } = await useApiFetch<{ draftId: number; success: boolean }>("/drafts/new", {
method: "post", method: "post",
body: values, body: values,
}).then(({ data, error }) => {
console.log("fibberty", data, error);
if (data.value) {
store.$patch({ draftId: data.value.draftId });
}
}); });
console.log("B", b); console.log("fibberty", data, error);
if (data.value) {
store.$patch({ draftId: data.value.draftId });
}
} else { } else {
useApiFetch<any>(`/drafts/${store.$state.draftId}`, { await useApiFetch<any>(`/drafts/${store.$state.draftId}`, {
method: "put", method: "put",
body: values, body: values,
}); });
} }
}; };
export const autoEdit = (values: any, endpoint: string, method: "put" | "post") => { export const autoEdit = async (values: any, endpoint: string, method: "put" | "post") => {
const [messageApi, contextHolder] = message.useMessage(); const [messageApi] = message.useMessage();
useApiFetch<{ success: boolean; data: IStory }>(endpoint, { const { data, error } = await useApiFetch<{ success: boolean; data: IStory }>(endpoint, {
method, method,
body: values, 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.");
}
}); });
if (data.value?.success) {
messageApi.success("Your work has been saved successfully.");
} else if (error) {
messageApi.error("Error saving data.");
}
}; };
export const debouncedAutoEdit = debounce(autoEdit, 5000); export const debouncedAutoEdit = debounce(autoEdit, 5000);