Skip to content

Commit c8b0fbf

Browse files
committed
goal creation works
1 parent 0abce87 commit c8b0fbf

3 files changed

Lines changed: 47 additions & 34 deletions

File tree

plan.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The application's state management for tasks and categories has been refactored
5353
- Goal View: The dashboard renders a dynamic list of goals.
5454
- Goal Progress: The dashboard shows the progress of each goal based on the completion of its associated tasks.
5555
- Goal and Task Linking: Tasks can be linked to goals.
56+
- Task Creation from Goal: Users can now create new tasks directly from the goal creation page.
5657

5758
🎯 Next Steps
5859
- Implement state management for Habits using React Context.

src/components/CreateGoalPage.tsx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
// src/components/CreateGoalPage.tsx
2-
import { useState } from 'react';
3-
import { useNavigate } from 'react-router-dom';
2+
import { useState, useEffect } from 'react';
3+
import { useNavigate, useLocation } from 'react-router-dom';
44
import { useGoals } from '../contexts/useGoals';
55
import { useTasks } from '../contexts/useTasks';
66

77
function CreateGoalPage() {
88
const navigate = useNavigate();
9+
const location = useLocation();
910
const { addGoal } = useGoals();
10-
const { tasks, addTask, categories } = useTasks();
11+
const { tasks } = useTasks();
1112

1213
const [title, setTitle] = useState('');
1314
const [description, setDescription] = useState('');
1415
const [targetDate, setTargetDate] = useState('');
1516
const [selectedTaskIds, setSelectedTaskIds] = useState<number[]>([]);
16-
const [newTaskText, setNewTaskText] = useState('');
1717

1818
const incompleteTasks = tasks.filter(task => !task.isCompleted);
1919

20+
useEffect(() => {
21+
setTimeout(() => {
22+
if (location.state?.newTaskId) {
23+
const newSelectedTaskIds = [...(location.state.goalData.selectedTaskIds || []), location.state.newTaskId];
24+
setSelectedTaskIds(newSelectedTaskIds);
25+
}
26+
if (location.state?.goalData) {
27+
setTitle(location.state.goalData.title || '');
28+
setDescription(location.state.goalData.description || '');
29+
setTargetDate(location.state.goalData.targetDate || '');
30+
setSelectedTaskIds(location.state.goalData.selectedTaskIds || []);
31+
}
32+
}, 0);
33+
}, [location.state]);
34+
35+
2036
const handleCreateGoal = () => {
2137
if (!title.trim()) {
2238
alert('Please enter a goal title.');
@@ -38,24 +54,13 @@ function CreateGoalPage() {
3854
);
3955
};
4056

41-
const handleAddNewTask = (e: React.KeyboardEvent<HTMLInputElement>) => {
42-
if (e.key === 'Enter' && newTaskText.trim()) {
43-
e.preventDefault(); // Prevent form submission
44-
const defaultCategory = categories[0];
45-
if (!defaultCategory) {
46-
alert('Cannot add task: No categories found.');
47-
return;
48-
}
49-
const newTaskId = addTask({
50-
text: newTaskText.trim(),
51-
time: 'Anytime',
52-
tag: defaultCategory.name,
53-
tagColor: defaultCategory.color,
54-
isCompleted: false,
55-
});
56-
setSelectedTaskIds((prev) => [...prev, newTaskId]);
57-
setNewTaskText('');
58-
}
57+
const handleAddNewTaskClick = () => {
58+
navigate('/create-task', {
59+
state: {
60+
from: '/create-goal',
61+
goalData: { title, description, targetDate, selectedTaskIds },
62+
},
63+
});
5964
};
6065

6166
return (
@@ -128,15 +133,13 @@ function CreateGoalPage() {
128133
</div>
129134
))}
130135
<div className="flex items-center gap-x-3 p-3">
131-
<span className="material-symbols-outlined text-text-light-secondary dark:text-text-dark-secondary">add</span>
132-
<input
133-
className="flex-1 border-0 bg-transparent p-0 text-base text-text-light-primary placeholder:text-text-light-secondary/70 focus:ring-0 dark:text-text-dark-primary dark:placeholder:text-text-dark-secondary/70"
134-
placeholder="Add a new task"
135-
type="text"
136-
value={newTaskText}
137-
onChange={(e) => setNewTaskText(e.target.value)}
138-
onKeyDown={handleAddNewTask}
139-
/>
136+
<button
137+
onClick={handleAddNewTaskClick}
138+
className="flex w-full items-center gap-x-3 text-text-light-primary dark:text-text-dark-primary"
139+
>
140+
<span className="material-symbols-outlined text-text-light-secondary dark:text-text-dark-secondary">add</span>
141+
<span className="text-base">Add a new task</span>
142+
</button>
140143
</div>
141144
</div>
142145
</div>

src/components/CreateTaskPage.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// src/components/CreateTaskPage.tsx
22
import { useState } from 'react';
3-
import { useNavigate } from 'react-router-dom';
3+
import { useNavigate, useLocation } from 'react-router-dom';
44
import { useTasks } from '../contexts/useTasks';
55

66
function CreateTaskPage() {
77
const navigate = useNavigate();
8+
const location = useLocation();
89
const { addTask, categories } = useTasks();
910

1011
const [text, setText] = useState('');
@@ -25,15 +26,23 @@ function CreateTaskPage() {
2526
return;
2627
}
2728

28-
addTask({
29+
const newTaskId = addTask({
2930
text,
3031
time: time || 'Anytime',
3132
startDate,
3233
tag: selectedCategory.name,
3334
tagColor: selectedCategory.color,
35+
description,
3436
});
3537

36-
navigate('/');
38+
const from = location.state?.from;
39+
const goalData = location.state?.goalData;
40+
41+
if (from === '/create-goal') {
42+
navigate(from, { state: { newTaskId, goalData } });
43+
} else {
44+
navigate('/');
45+
}
3746
};
3847

3948
return (

0 commit comments

Comments
 (0)