-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented autosave functionality, made as a composable for reusabil…
…ity, can be applied to any form with save draft capabilities
- Loading branch information
Showing
5 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ref, onMounted, onBeforeUnmount } from 'vue'; | ||
|
||
// autosave functionality for forms, saves on inactivity after the delay (default 10 seconds) | ||
export function useAutoSave(saveFunc: () => Promise<void>, delay: number = 10000) { | ||
const formUpdated = ref(false); | ||
let timeoutId: ReturnType<typeof setTimeout> | null = null; | ||
|
||
const startTimer = () => { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
timeoutId = setTimeout(async () => { | ||
if (formUpdated.value) { | ||
await saveFunc(); | ||
formUpdated.value = false; | ||
timeoutId = null; | ||
} | ||
}, delay); | ||
}; | ||
|
||
const onActivity = () => { | ||
startTimer(); | ||
}; | ||
onMounted(() => { | ||
window.addEventListener('keydown', onActivity); | ||
window.addEventListener('focus', onActivity); | ||
window.addEventListener('click', onActivity); | ||
}); | ||
|
||
onBeforeUnmount(() => { | ||
window.removeEventListener('keydown', onActivity); | ||
window.removeEventListener('focus', onActivity); | ||
window.removeEventListener('click', onActivity); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
}); | ||
|
||
return { | ||
formUpdated, | ||
stopAutoSave: () => { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters