@@ -31,7 +31,10 @@ import {
3131  MODULE_ISSUES_WITH_PARAMS , 
3232  VIEW_ISSUES , 
3333  PROJECT_DRAFT_ISSUES_LIST_WITH_PARAMS , 
34+   CYCLE_DETAILS , 
35+   MODULE_DETAILS , 
3436}  from  "constants/fetch-keys" ; 
37+ import  modulesService  from  "services/modules.service" ; 
3538
3639interface  IssuesModalProps  { 
3740  data ?: IIssue  |  null ; 
@@ -56,18 +59,21 @@ interface IssuesModalProps {
5659  onSubmit ?: ( data : Partial < IIssue > )  =>  Promise < void >  |  void ; 
5760} 
5861
59- export  const  CreateUpdateDraftIssueModal : React . FC < IssuesModalProps >  =  ( { 
60-   data, 
61-   handleClose, 
62-   isOpen, 
63-   isUpdatingSingleIssue =  false , 
64-   prePopulateData, 
65-   fieldsToShow =  [ "all" ] , 
66-   onSubmit, 
67- } )  =>  { 
62+ export  const  CreateUpdateDraftIssueModal : React . FC < IssuesModalProps >  =  ( props )  =>  { 
63+   const  { 
64+     data, 
65+     handleClose, 
66+     isOpen, 
67+     isUpdatingSingleIssue =  false , 
68+     prePopulateData : prePopulateDataProps , 
69+     fieldsToShow =  [ "all" ] , 
70+     onSubmit, 
71+   }  =  props ; 
72+ 
6873  // states 
6974  const  [ createMore ,  setCreateMore ]  =  useState ( false ) ; 
7075  const  [ activeProject ,  setActiveProject ]  =  useState < string  |  null > ( null ) ; 
76+   const  [ prePopulateData ,  setPreloadedData ]  =  useState < Partial < IIssue >  |  undefined > ( undefined ) ; 
7177
7278  const  router  =  useRouter ( ) ; 
7379  const  {  workspaceSlug,  projectId,  cycleId,  moduleId,  viewId }  =  router . query ; 
@@ -86,19 +92,40 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
8692
8793  const  {  setToastAlert }  =  useToast ( ) ; 
8894
89-   if  ( cycleId )  prePopulateData  =  {  ...prePopulateData ,  cycle : cycleId  as  string  } ; 
90-   if  ( moduleId )  prePopulateData  =  {  ...prePopulateData ,  module : moduleId  as  string  } ; 
91-   if  ( router . asPath . includes ( "my-issues" )  ||  router . asPath . includes ( "assigned" ) ) 
92-     prePopulateData  =  { 
93-       ...prePopulateData , 
94-       assignees : [ ...( prePopulateData ?. assignees  ??  [ ] ) ,  user ?. id  ??  "" ] , 
95-     } ; 
96- 
9795  const  onClose  =  ( )  =>  { 
9896    handleClose ( ) ; 
9997    setActiveProject ( null ) ; 
10098  } ; 
10199
100+   useEffect ( ( )  =>  { 
101+     setPreloadedData ( prePopulateDataProps  ??  { } ) ; 
102+ 
103+     if  ( cycleId  &&  ! prePopulateDataProps ?. cycle )  { 
104+       setPreloadedData ( ( prevData )  =>  ( { 
105+         ...( prevData  ??  { } ) , 
106+         ...prePopulateDataProps , 
107+         cycle : cycleId . toString ( ) , 
108+       } ) ) ; 
109+     } 
110+     if  ( moduleId  &&  ! prePopulateDataProps ?. module )  { 
111+       setPreloadedData ( ( prevData )  =>  ( { 
112+         ...( prevData  ??  { } ) , 
113+         ...prePopulateDataProps , 
114+         module : moduleId . toString ( ) , 
115+       } ) ) ; 
116+     } 
117+     if  ( 
118+       ( router . asPath . includes ( "my-issues" )  ||  router . asPath . includes ( "assigned" ) )  && 
119+       ! prePopulateDataProps ?. assignees 
120+     )  { 
121+       setPreloadedData ( ( prevData )  =>  ( { 
122+         ...( prevData  ??  { } ) , 
123+         ...prePopulateDataProps , 
124+         assignees : prePopulateDataProps ?. assignees  ??  [ user ?. id  ??  "" ] , 
125+       } ) ) ; 
126+     } 
127+   } ,  [ prePopulateDataProps ,  cycleId ,  moduleId ,  router . asPath ,  user ?. id ] ) ; 
128+ 
102129  useEffect ( ( )  =>  { 
103130    // if modal is closed, reset active project to null 
104131    // and return to avoid activeProject being set to some other project 
@@ -109,10 +136,10 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
109136
110137    // if data is present, set active project to the project of the 
111138    // issue. This has more priority than the project in the url. 
112-     if  ( data  &&  data . project )  { 
113-        setActiveProject ( data . project ) ; 
114-        return ; 
115-     } 
139+     if  ( data  &&  data . project )  return   setActiveProject ( data . project ) ; 
140+ 
141+     if   ( prePopulateData   &&   prePopulateData . project   &&   ! activeProject ) 
142+        return   setActiveProject ( prePopulateData . project ) ; 
116143
117144    if  ( prePopulateData  &&  prePopulateData . project ) 
118145      return  setActiveProject ( prePopulateData . project ) ; 
@@ -147,7 +174,7 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
147174    ? VIEW_ISSUES ( viewId . toString ( ) ,  viewGanttParams ) 
148175    : PROJECT_ISSUES_LIST_WITH_PARAMS ( activeProject ?. toString ( )  ??  "" ) ; 
149176
150-   const  createIssue  =  async  ( payload : Partial < IIssue > )  =>  { 
177+   const  createDraftIssue  =  async  ( payload : Partial < IIssue > )  =>  { 
151178    if  ( ! workspaceSlug  ||  ! activeProject  ||  ! user )  return ; 
152179
153180    await  issuesService 
@@ -187,7 +214,7 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
187214    if  ( ! createMore )  onClose ( ) ; 
188215  } ; 
189216
190-   const  updateIssue  =  async  ( payload : Partial < IIssue > )  =>  { 
217+   const  updateDraftIssue  =  async  ( payload : Partial < IIssue > )  =>  { 
191218    if  ( ! user )  return ; 
192219
193220    await  issuesService 
@@ -203,6 +230,11 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
203230          mutate ( PROJECT_DRAFT_ISSUES_LIST_WITH_PARAMS ( activeProject  ??  "" ,  params ) ) ; 
204231        } 
205232
233+         if  ( ! payload . is_draft )  { 
234+           if  ( payload . cycle  &&  payload . cycle  !==  "" )  addIssueToCycle ( res . id ,  payload . cycle ) ; 
235+           if  ( payload . module  &&  payload . module  !==  "" )  addIssueToModule ( res . id ,  payload . module ) ; 
236+         } 
237+ 
206238        if  ( ! createMore )  onClose ( ) ; 
207239
208240        setToastAlert ( { 
@@ -220,7 +252,93 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
220252      } ) ; 
221253  } ; 
222254
223-   const  handleFormSubmit  =  async  ( formData : Partial < IIssue > )  =>  { 
255+   const  addIssueToCycle  =  async  ( issueId : string ,  cycleId : string )  =>  { 
256+     if  ( ! workspaceSlug  ||  ! activeProject )  return ; 
257+ 
258+     await  issuesService 
259+       . addIssueToCycle ( 
260+         workspaceSlug  as  string , 
261+         activeProject  ??  "" , 
262+         cycleId , 
263+         { 
264+           issues : [ issueId ] , 
265+         } , 
266+         user 
267+       ) 
268+       . then ( ( )  =>  { 
269+         if  ( cycleId )  { 
270+           mutate ( CYCLE_ISSUES_WITH_PARAMS ( cycleId ,  params ) ) ; 
271+           mutate ( CYCLE_DETAILS ( cycleId  as  string ) ) ; 
272+         } 
273+       } ) ; 
274+   } ; 
275+ 
276+   const  addIssueToModule  =  async  ( issueId : string ,  moduleId : string )  =>  { 
277+     if  ( ! workspaceSlug  ||  ! activeProject )  return ; 
278+ 
279+     await  modulesService 
280+       . addIssuesToModule ( 
281+         workspaceSlug  as  string , 
282+         activeProject  ??  "" , 
283+         moduleId  as  string , 
284+         { 
285+           issues : [ issueId ] , 
286+         } , 
287+         user 
288+       ) 
289+       . then ( ( )  =>  { 
290+         if  ( moduleId )  { 
291+           mutate ( MODULE_ISSUES_WITH_PARAMS ( moduleId  as  string ,  params ) ) ; 
292+           mutate ( MODULE_DETAILS ( moduleId  as  string ) ) ; 
293+         } 
294+       } ) ; 
295+   } ; 
296+ 
297+   const  createIssue  =  async  ( payload : Partial < IIssue > )  =>  { 
298+     if  ( ! workspaceSlug  ||  ! activeProject )  return ; 
299+ 
300+     await  issuesService 
301+       . createIssues ( workspaceSlug  as  string ,  activeProject  ??  "" ,  payload ,  user ) 
302+       . then ( async  ( res )  =>  { 
303+         mutate ( PROJECT_ISSUES_LIST_WITH_PARAMS ( activeProject  ??  "" ,  params ) ) ; 
304+         if  ( payload . cycle  &&  payload . cycle  !==  "" )  await  addIssueToCycle ( res . id ,  payload . cycle ) ; 
305+         if  ( payload . module  &&  payload . module  !==  "" )  await  addIssueToModule ( res . id ,  payload . module ) ; 
306+ 
307+         if  ( displayFilters . layout  ===  "calendar" )  mutate ( calendarFetchKey ) ; 
308+         if  ( displayFilters . layout  ===  "gantt_chart" ) 
309+           mutate ( ganttFetchKey ,  { 
310+             start_target_date : true , 
311+             order_by : "sort_order" , 
312+           } ) ; 
313+         if  ( displayFilters . layout  ===  "spreadsheet" )  mutate ( spreadsheetFetchKey ) ; 
314+         if  ( groupedIssues )  mutateMyIssues ( ) ; 
315+ 
316+         setToastAlert ( { 
317+           type : "success" , 
318+           title : "Success!" , 
319+           message : "Issue created successfully." , 
320+         } ) ; 
321+ 
322+         if  ( ! createMore )  onClose ( ) ; 
323+ 
324+         if  ( payload . assignees_list ?. some ( ( assignee )  =>  assignee  ===  user ?. id ) ) 
325+           mutate ( USER_ISSUE ( workspaceSlug  as  string ) ) ; 
326+ 
327+         if  ( payload . parent  &&  payload . parent  !==  "" )  mutate ( SUB_ISSUES ( payload . parent ) ) ; 
328+       } ) 
329+       . catch ( ( )  =>  { 
330+         setToastAlert ( { 
331+           type : "error" , 
332+           title : "Error!" , 
333+           message : "Issue could not be created. Please try again." , 
334+         } ) ; 
335+       } ) ; 
336+   } ; 
337+ 
338+   const  handleFormSubmit  =  async  ( 
339+     formData : Partial < IIssue > , 
340+     action : "createDraft"  |  "createNewIssue"  |  "updateDraft"  |  "convertToNewIssue"  =  "createDraft" 
341+   )  =>  { 
224342    if  ( ! workspaceSlug  ||  ! activeProject )  return ; 
225343
226344    const  payload : Partial < IIssue >  =  { 
@@ -231,8 +349,10 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = ({
231349      description_html : formData . description_html  ??  "<p></p>" , 
232350    } ; 
233351
234-     if  ( ! data )  await  createIssue ( payload ) ; 
235-     else  await  updateIssue ( payload ) ; 
352+     if  ( action  ===  "createDraft" )  await  createDraftIssue ( payload ) ; 
353+     else  if  ( action  ===  "updateDraft"  ||  action  ===  "convertToNewIssue" ) 
354+       await  updateDraftIssue ( payload ) ; 
355+     else  if  ( action  ===  "createNewIssue" )  await  createIssue ( payload ) ; 
236356
237357    clearDraftIssueLocalStorage ( ) ; 
238358
0 commit comments