@@ -79,43 +79,74 @@ export default function MyTasks() {
7979 setDialogMode ( 'edit' ) ;
8080 setEditingTaskId ( task . _id ) ;
8181 setSelectedTask ( task ) ;
82+
83+ // Helper to format date for HTML date input (YYYY-MM-DD)
84+ const formatDateForInput = ( dateValue : string | null | undefined ) : string => {
85+ if ( ! dateValue ) return '' ;
86+ try {
87+ const date = new Date ( dateValue ) ;
88+ if ( isNaN ( date . getTime ( ) ) ) return '' ;
89+ return date . toISOString ( ) . split ( 'T' ) [ 0 ] ;
90+ } catch {
91+ return '' ;
92+ }
93+ } ;
94+
8295 setForm ( {
8396 title : task . title || '' ,
8497 description : task . description || '' ,
8598 priority : task . priority || 'Medium' ,
8699 status : task . status || 'To Do' ,
87- startDate : task . startDate || '' ,
88- endDate : task . endDate || '' ,
100+ startDate : formatDateForInput ( task . startDate ) ,
101+ endDate : formatDateForInput ( task . endDate ) ,
89102 assignedTo : task . assignedTo || [ ] ,
90103 createdBy : task . createdBy || '' ,
91104 category : task . category || task . categoryId || '' ,
92105 projectId : ( task as Task & { projectId ?: string } ) . projectId || '' ,
93106 comments : '' ,
94107 } ) ;
95108 setOpen ( true ) ;
96- fetchAssignableUsers ( task . assignedTo ) ;
109+ // Pass the task's projectId directly since setForm is async and form.projectId won't be updated yet
110+ const taskProjectId = ( task as Task & { projectId ?: string } ) . projectId || '' ;
111+ fetchAssignableUsers ( task . assignedTo , taskProjectId ) ;
97112 fetchProjects ( ) ;
98113 }
99114
100- async function fetchAssignableUsers ( assignedToIds ?: string [ ] ) {
115+ async function fetchAssignableUsers ( assignedToIds ?: string [ ] , taskProjectId ?: string ) {
101116 setIsLoadingAssignableUsers ( true ) ;
102117 setAssignableUsersError ( null ) ;
103118 try {
104119 let unique : User [ ] = [ ] ;
105120
106121 // If a project is selected, fetch only project members
107- if ( form . projectId || ( selectedTask && ( selectedTask as Task & { projectId ?: string } ) . projectId ) ) {
108- const projectId = form . projectId || ( selectedTask && ( selectedTask as Task & { projectId ?: string } ) . projectId ) ;
109- if ( projectId ) {
122+ // Use taskProjectId if provided (for edit mode), otherwise fall back to form.projectId
123+ const projectId = taskProjectId || form . projectId || ( selectedTask && ( selectedTask as Task & { projectId ?: string } ) . projectId ) ;
124+ if ( projectId ) {
125+ try {
110126 const projectDetails = await projectsApi . getProjectById ( projectId ) ;
111127 const teamMemberIds = projectDetails . teamMembers || [ ] ;
112128
113- // Fetch all team member details
114- const memberPromises = teamMemberIds . map ( id =>
115- usersApi . getUserById ( id ) . catch ( ( ) => null )
116- ) ;
117- const members = await Promise . all ( memberPromises ) ;
118- unique = members . filter ( ( m ) : m is User => m !== null ) ;
129+ if ( teamMemberIds . length > 0 ) {
130+ // Fetch all team member details
131+ const memberPromises = teamMemberIds . map ( id =>
132+ usersApi . getUserById ( id ) . catch ( ( ) => null )
133+ ) ;
134+ const members = await Promise . all ( memberPromises ) ;
135+ unique = members . filter ( ( m ) : m is User => m !== null ) ;
136+ }
137+ } catch ( projectErr ) {
138+ console . error ( 'Failed to fetch project details:' , projectErr ) ;
139+ // Fall through to fetch all users
140+ }
141+ }
142+
143+ // If no project members were found, fetch all users as fallback
144+ if ( unique . length === 0 ) {
145+ try {
146+ const allUsers = await usersApi . getAllUsers ( ) ;
147+ unique = allUsers ;
148+ } catch ( usersErr ) {
149+ console . error ( 'Failed to fetch all users:' , usersErr ) ;
119150 }
120151 }
121152
@@ -146,21 +177,21 @@ export default function MyTasks() {
146177 if ( value === '' || ( Array . isArray ( value ) && value . length === 0 ) ) return ;
147178
148179 try {
149- // Build a safe typed payload for the patch API
150- // Build a typed payload object safely (avoid using `any`)
151- const payloadRec : Record < string , unknown > = { } ;
152- if ( typeof value === 'string' ) {
153- payloadRec [ fieldName ] = value ;
154- } else if ( Array . isArray ( value ) ) {
155- payloadRec [ fieldName ] = value as string [ ] ;
156- } else if ( typeof value === 'number' || typeof value === 'boolean' ) {
157- payloadRec [ fieldName ] = value ;
158- } else {
159- // unknown or unsupported type — skip
160- return ;
161- }
180+ // Build a safe typed payload for the patch API
181+ // Build a typed payload object safely (avoid using `any`)
182+ const payloadRec : Record < string , unknown > = { } ;
183+ if ( typeof value === 'string' ) {
184+ payloadRec [ fieldName ] = value ;
185+ } else if ( Array . isArray ( value ) ) {
186+ payloadRec [ fieldName ] = value as string [ ] ;
187+ } else if ( typeof value === 'number' || typeof value === 'boolean' ) {
188+ payloadRec [ fieldName ] = value ;
189+ } else {
190+ // unknown or unsupported type — skip
191+ return ;
192+ }
162193
163- await tasksApi . patchTask ( editingTaskId , payloadRec as Partial < CreateTaskData > ) ;
194+ await tasksApi . patchTask ( editingTaskId , payloadRec as Partial < CreateTaskData > ) ;
164195 // refresh data grid
165196 setRefreshKey ( k => k + 1 ) ;
166197 } catch ( err ) {
0 commit comments