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' ;
44import { useGoals } from '../contexts/useGoals' ;
55import { useTasks } from '../contexts/useTasks' ;
66
77function 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 >
0 commit comments