Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 217fa56

Browse files
committed
Fix: in the new task modal, the parent task combo wouldn't change when I change a category
1 parent effce9e commit 217fa56

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

frontend/src/lib/AddTaskModal.svelte

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,88 @@
33
44
let { categories, defaultCategory, tasks, task, onSave, onClose } = $props();
55
6+
const API_BASE = '/api';
7+
68
let title = $state(task?.title || '');
79
let description = $state(task?.description || '');
810
let categoryId = $state(task?.category_id || defaultCategory?.id || 1);
911
let parentId = $state(task?.parent_id || null);
1012
let titleInput;
13+
let allCategoryTasks = $state(tasks); // Tasks for the currently selected category in modal
1114
1215
// If task is a subtask, it can't change category
1316
let isSubtask = $derived(task ? !!task.parent_id : false);
1417
15-
// Update category when parent changes (for new tasks with parent)
18+
// Track last known values to detect user-initiated changes
19+
let lastParentId = $state(null);
20+
let lastCategoryId = $state(null);
21+
22+
// Initialize tracking variables on first run
23+
$effect(() => {
24+
if (lastCategoryId === null) {
25+
lastCategoryId = categoryId;
26+
lastParentId = parentId;
27+
}
28+
});
29+
30+
// Handle parent selection change
31+
$effect(() => {
32+
// User changed parent selection
33+
if (parentId !== lastParentId) {
34+
const previousParent = lastParentId;
35+
lastParentId = parentId;
36+
37+
if (parentId) {
38+
// Parent was selected - update category to match
39+
const parent = allCategoryTasks.find(t => t.id === parentId);
40+
if (parent && parent.category_id !== categoryId) {
41+
categoryId = parent.category_id;
42+
lastCategoryId = categoryId;
43+
}
44+
}
45+
// Parent was deselected - keep current category
46+
}
47+
});
48+
49+
// Handle category change
50+
$effect(() => {
51+
// User changed category manually
52+
if (categoryId !== lastCategoryId && lastCategoryId !== null) {
53+
lastCategoryId = categoryId;
54+
55+
// If parent doesn't belong to new category, reset it
56+
if (parentId && !isSubtask) {
57+
const parent = allCategoryTasks.find(t => t.id === parentId);
58+
if (!parent || parent.category_id !== categoryId) {
59+
parentId = null;
60+
lastParentId = null;
61+
}
62+
}
63+
}
64+
});
65+
66+
// Fetch tasks for the selected category when it changes
1667
$effect(() => {
17-
if (parentId) {
18-
const parent = tasks.find(t => t.id === parentId);
19-
if (parent) {
20-
categoryId = parent.category_id;
68+
async function fetchCategoryTasks() {
69+
if (!isSubtask && categoryId) {
70+
const res = await fetch(`${API_BASE}/tasks?category_id=${categoryId}`);
71+
allCategoryTasks = await res.json();
2172
}
2273
}
74+
fetchCategoryTasks();
2375
});
2476
2577
// Helper to check if a task has subtasks
2678
function hasSubtasks(taskId) {
27-
return tasks.some(t => t.parent_id === taskId);
79+
return allCategoryTasks.some(t => t.parent_id === taskId);
2880
}
2981
3082
// Check if current task being edited has subtasks (can't become a subtask itself)
3183
let taskHasSubtasks = $derived(task ? hasSubtasks(task.id) : false);
3284
3385
// Get available parent tasks (top-level tasks only, excluding the task being edited)
3486
let availableParents = $derived(
35-
tasks.filter(t =>
87+
allCategoryTasks.filter(t =>
3688
!t.parent_id && (!task || t.id !== task.id) && t.category_id === categoryId
3789
)
3890
);
@@ -125,13 +177,13 @@
125177
id="category"
126178
class="form-select bg-dark text-light border-secondary"
127179
bind:value={categoryId}
128-
disabled={isSubtask || !!parentId}
180+
disabled={isSubtask}
129181
>
130182
{#each categories as category}
131183
<option value={category.id}>{category.name}</option>
132184
{/each}
133185
</select>
134-
{#if isSubtask || parentId}
186+
{#if isSubtask}
135187
<small class="text-muted d-block mt-1">Subtasks inherit their parent's category.</small>
136188
{/if}
137189
</div>

0 commit comments

Comments
 (0)