refactor(client-side): add debounced versions of functions
split autoEdit and autoSave into debounced and non-debounced versions
This commit is contained in:
		
							parent
							
								
									1447960b1a
								
							
						
					
					
						commit
						dba62b4677
					
				| @ -3,7 +3,7 @@ import { message } from "ant-design-vue"; | |||||||
| import { IStory } from "@models/stories"; | import { IStory } from "@models/stories"; | ||||||
| import { useAutoSaveStore } from "~/stores/autosaveStore"; | import { useAutoSaveStore } from "~/stores/autosaveStore"; | ||||||
| 
 | 
 | ||||||
| export const autoSave = debounce(async (values: any) => { | export const autoSave = async (values: any) => { | ||||||
| 	const store = useAutoSaveStore(); | 	const store = useAutoSaveStore(); | ||||||
| 	const fid = store.$state.fetchId; | 	const fid = store.$state.fetchId; | ||||||
| 	if (store.$state.fetchId !== fid) return; | 	if (store.$state.fetchId !== fid) return; | ||||||
| @ -25,21 +25,25 @@ export const autoSave = debounce(async (values: any) => { | |||||||
| 			body: values, | 			body: values, | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| }, 20000); | }; | ||||||
| 
 | 
 | ||||||
| export const autoEdit = debounce( | export const autoEdit = ( | ||||||
| 	(values: any, endpoint: string, method: "put" | "post") => { | 	values: any, | ||||||
| 		const [messageApi, contextHolder] = message.useMessage(); | 	endpoint: string, | ||||||
| 		useApiFetch<{ success: boolean; data: IStory }>(endpoint, { | 	method: "put" | "post", | ||||||
| 			method, | ) => { | ||||||
| 			body: values, | 	const [messageApi, contextHolder] = message.useMessage(); | ||||||
| 		}).then(({ data, error }) => { | 	useApiFetch<{ success: boolean; data: IStory }>(endpoint, { | ||||||
| 			if (data.value?.success) { | 		method, | ||||||
| 				messageApi.success("Your work has been saved successfully."); | 		body: values, | ||||||
| 			} else if (error) { | 	}).then(({ data, 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."); | ||||||
| 	10000, | 		} | ||||||
| ); | 	}); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export const debouncedAutoEdit = debounce(autoEdit, 5000); | ||||||
|  | export const debouncedAutoSave = debounce(autoSave, 10000); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user