fix: use goBack helper with canGoBack fallback and reset saving state (#21)

Replace all router.back() calls with a goBack() helper that checks
router.canGoBack() first and falls back to router.replace('/') when
there is no screen to return to. In the task edit screen, change
catch to finally so the save button always resets its disabled state
even if updateTask/setTagsForTask throws.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
medic-bot 2026-03-09 20:47:22 -04:00
parent d6a69d849b
commit 2296126ba4
2 changed files with 22 additions and 6 deletions

View file

@ -112,6 +112,14 @@ export default function TaskDetailScreen() {
setSubtasks(result as SubtaskData[]);
};
const goBack = () => {
if (router.canGoBack()) {
router.back();
} else {
router.replace('/');
}
};
const handleSave = async () => {
if (saving) return;
if (!task || !title.trim()) return;
@ -126,8 +134,8 @@ export default function TaskDetailScreen() {
listId: selectedListId,
});
await setTagsForTask(task.id, selectedTagIds);
router.back();
} catch {
goBack();
} finally {
setSaving(false);
}
};
@ -141,7 +149,7 @@ export default function TaskDetailScreen() {
onPress: async () => {
await deleteTask(id!);
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
router.back();
goBack();
},
},
]);
@ -185,7 +193,7 @@ export default function TaskDetailScreen() {
<View
className={`flex-row items-center justify-between border-b px-4 pb-3 pt-14 ${isDark ? 'border-[#3A3A3A]' : 'border-[#E5E7EB]'}`}
>
<Pressable onPress={() => router.back()} className="p-2.5" hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}>
<Pressable onPress={goBack} className="p-2.5" hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}>
<ArrowLeft size={24} color={isDark ? '#F5F5F5' : '#1A1A1A'} />
</Pressable>
<View className="flex-row items-center">

View file

@ -64,6 +64,14 @@ export default function NewTaskScreen() {
getAllTags().then(setAvailableTags);
}, []);
const goBack = () => {
if (router.canGoBack()) {
router.back();
} else {
router.replace('/');
}
};
const handleSave = async () => {
if (saving) return;
if (!title.trim()) return;
@ -83,7 +91,7 @@ export default function NewTaskScreen() {
for (const sub of pendingSubtasks) {
await createTask({ title: sub, listId: selectedListId, parentId: taskId });
}
router.back();
goBack();
} catch {
// FK constraint or other DB error — fallback to inbox
setSaving(false);
@ -120,7 +128,7 @@ export default function NewTaskScreen() {
isDark ? 'border-[#3A3A3A]' : 'border-[#E5E7EB]'
}`}
>
<Pressable onPress={() => router.back()} className="p-2.5" hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}>
<Pressable onPress={goBack} className="p-2.5" hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}>
<X size={24} color={isDark ? '#F5F5F5' : '#1A1A1A'} />
</Pressable>
<Text