@@ -4,11 +4,12 @@ import { mainLayoutStyles } from '../../components/styles/Styles'
44import TaskDialog from '../../components/dialogs/TaskDialog'
55import { useUser } from '../../hooks/useUser'
66import { tasksApi } from '../../components/apis/tasks'
7+ import { projectsApi , type Project } from '../../components/apis/projects'
78import { usersApi } from '../../components/apis/users'
89import type { User } from '../../components/apis/auth'
910import type { Task } from '../../types/MyTasksTypes'
1011import type { CreateTaskData } from '../../components/apis/tasks'
11- import { useState , useRef } from 'react'
12+ import { useState , useRef , useEffect } from 'react'
1213import type { ChangeEvent , FormEvent } from 'react'
1314
1415
@@ -34,8 +35,12 @@ export default function MyTasks() {
3435 assignedTo : user ?. id || '' ,
3536 createdBy : user ?. id || '' ,
3637 category : '' ,
38+ projectId : '' ,
3739 comments : '' ,
3840 } ) ;
41+ const [ projects , setProjects ] = useState < Project [ ] > ( [ ] ) ;
42+ const [ isLoadingProjects , setIsLoadingProjects ] = useState ( false ) ;
43+ const [ projectsError , setProjectsError ] = useState < string | null > ( null ) ;
3944 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
4045 const [ submitError , setSubmitError ] = useState < string | null > ( null ) ;
4146 const [ assignableUsers , setAssignableUsers ] = useState < User [ ] > ( [ ] ) ;
@@ -57,11 +62,13 @@ export default function MyTasks() {
5762 assignedTo : user ?. id || '' ,
5863 createdBy : user ?. id || '' ,
5964 category : '' ,
65+ projectId : '' ,
6066 comments : '' ,
6167 } ) ;
6268 setOpen ( true ) ;
6369 setSelectedTask ( null ) ;
6470 fetchAssignableUsers ( undefined ) ;
71+ fetchProjects ( ) ;
6572 }
6673
6774 // Called when DataGrid row is clicked
@@ -79,10 +86,12 @@ export default function MyTasks() {
7986 assignedTo : task . assignedTo || '' ,
8087 createdBy : task . createdBy || '' ,
8188 category : task . category || task . categoryId || '' ,
89+ projectId : ( task as any ) . projectId || '' ,
8290 comments : '' ,
8391 } ) ;
8492 setOpen ( true ) ;
8593 fetchAssignableUsers ( task . assignedTo ) ;
94+ fetchProjects ( ) ;
8695 }
8796
8897 async function fetchAssignableUsers ( assignedToId ?: string ) {
@@ -110,6 +119,9 @@ export default function MyTasks() {
110119 async function handleFieldUpdate ( fieldName : string , value : string ) {
111120 if ( ! editingTaskId ) return ;
112121
122+ // If the new value is empty, skip calling the backend to avoid 'No valid updatable fields provided.'
123+ if ( value === '' ) return ;
124+
113125 try {
114126 await tasksApi . patchTask ( editingTaskId , { [ fieldName ] : value } as Partial < CreateTaskData > ) ;
115127 // refresh data grid
@@ -144,6 +156,7 @@ export default function MyTasks() {
144156 try {
145157 const taskData = {
146158 category : form . category ,
159+ projectId : form . projectId ,
147160 assignedTo : form . assignedTo || user ?. id || '' ,
148161 title : form . title ,
149162 description : form . description ,
@@ -172,6 +185,7 @@ export default function MyTasks() {
172185 try {
173186 await tasksApi . updateTask ( editingTaskId , {
174187 category : form . category ,
188+ projectId : form . projectId ,
175189 assignedTo : form . assignedTo ,
176190 title : form . title ,
177191 description : form . description ,
@@ -193,6 +207,25 @@ export default function MyTasks() {
193207 }
194208 }
195209
210+ async function fetchProjects ( ) {
211+ setIsLoadingProjects ( true ) ;
212+ setProjectsError ( null ) ;
213+ try {
214+ const fetched = await projectsApi . getProjectsForSelect ( ) ;
215+ const normalized = fetched . map ( p => ( { ...( p as any ) , id : ( p as any ) . id || ( p as any ) . _id } ) ) ;
216+ setProjects ( normalized as Project [ ] ) ;
217+ } catch ( err : unknown ) {
218+ setProjectsError ( err instanceof Error ? err . message : 'Unable to load projects.' ) ;
219+ } finally {
220+ setIsLoadingProjects ( false ) ;
221+ }
222+ }
223+
224+ // Preload projects on mount for faster dropdown display
225+ useEffect ( ( ) => {
226+ fetchProjects ( ) ;
227+ } , [ user ?. id ] ) ;
228+
196229 async function handleDeleteTask ( ) {
197230 if ( ! editingTaskId ) return ;
198231 const confirmDelete = window . confirm ( 'Are you sure you want to delete this task?' ) ;
@@ -263,6 +296,9 @@ export default function MyTasks() {
263296 isLoadingAssignableUsers = { isLoadingAssignableUsers }
264297 assignableUsersError = { assignableUsersError }
265298 currentUser = { user }
299+ projects = { projects }
300+ isLoadingProjects = { isLoadingProjects }
301+ projectsError = { projectsError }
266302 />
267303 </ >
268304 )
0 commit comments