feat(task): autosave description on route leave (#1140)

This commit is contained in:
kolaente 2025-07-18 18:18:19 +02:00 committed by GitHub
parent 0ac4b2b9c9
commit f1641a1847
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -291,6 +291,29 @@ describe('Task', () => {
.should('exist')
})
it('autosaves the description when leaving the task view', () => {
TaskFactory.create(1, {
id: 1,
project_id: projects[0].id,
description: 'Old Description',
})
cy.visit('/tasks/1')
cy.get('.task-view .details.content.description .tiptap button.done-edit', {timeout: 30_000})
.click()
cy.get('.task-view .details.content.description .tiptap__editor .tiptap.ProseMirror')
.type('{selectall}New Description')
cy.get('.task-view h6.subtitle a')
.first()
.click()
cy.visit('/tasks/1')
cy.get('.task-view .details.content.description')
.should('contain.text', 'New Description')
})
it('Shows an empty editor when the description of a task is empty', () => {
const tasks = TaskFactory.create(1, {
id: 1,

View File

@ -38,7 +38,8 @@
</template>
<script setup lang="ts">
import {ref, computed, watchEffect, onBeforeUnmount} from 'vue'
import {ref, computed, watchEffect, onMounted, onBeforeUnmount} from 'vue'
import {onBeforeRouteLeave} from 'vue-router'
import CustomTransition from '@/components/misc/CustomTransition.vue'
import Editor from '@/components/input/AsyncEditor'
@ -73,6 +74,10 @@ const loading = computed(() => taskStore.isLoading)
const changeTimeout = ref<ReturnType<typeof setTimeout> | null>(null)
onMounted(() => {
window.addEventListener('beforeunload', save)
})
async function saveWithDelay() {
if (changeTimeout.value !== null) {
clearTimeout(changeTimeout.value)
@ -87,8 +92,11 @@ onBeforeUnmount(() => {
if (changeTimeout.value !== null) {
clearTimeout(changeTimeout.value)
}
window.removeEventListener('beforeunload', save)
})
onBeforeRouteLeave(() => save())
async function save() {
if (changeTimeout.value !== null) {
clearTimeout(changeTimeout.value)
@ -107,6 +115,13 @@ async function save() {
setTimeout(() => {
saved.value = false
}, 2000)
} catch (error) {
// If the task was deleted (404), silently skip saving
if (error?.response?.status === 404) {
return
}
// Re-throw other errors
throw error
} finally {
saving.value = false
}