1
1
import Select from "react-select" ;
2
- import { useEffect , useRef , useState } from "react" ;
2
+ import { useEffect , useState } from "react" ;
3
3
import { useAuthState } from "react-firebase-hooks/auth" ;
4
4
import {
5
5
addTask ,
@@ -11,6 +11,8 @@ import { auth, logout} from "../../../firebase";
11
11
import styles from "./CreateTask.module.css" ;
12
12
import { useNavigate , useLocation } from "react-router-dom" ;
13
13
import SummaryEdit from './components/SummaryEdit' ;
14
+ import { toast , ToastContainer } from 'react-toastify' ;
15
+ import "react-toastify/dist/ReactToastify.css" ;
14
16
15
17
export default function CreateTask ( ) {
16
18
const location = useLocation ( ) ;
@@ -24,14 +26,17 @@ export default function CreateTask() {
24
26
}
25
27
26
28
//If the user logs out redirect to login page
27
- const [ user , loading ] = useAuthState ( auth ) ;
29
+ const [ user ] = useAuthState ( auth ) ;
28
30
useEffect ( ( ) => {
29
31
if ( loading ) {
30
32
return ;
31
33
}
32
34
if ( ! user ) {
33
35
navigate ( "/" ) ;
34
36
}
37
+ if ( ! location . state . userData ) {
38
+ navigate ( "/" ) ;
39
+ }
35
40
} , [ user ] ) ;
36
41
37
42
// hardcoded values
@@ -93,9 +98,7 @@ export default function CreateTask() {
93
98
version : 4 ,
94
99
} )
95
100
96
- // function logout(){
97
- // navigate("/");
98
- // }
101
+ const [ loading , setLoading ] = useState ( false ) ;
99
102
100
103
// get project specific data when new project is selected
101
104
useEffect ( ( ) => {
@@ -108,31 +111,39 @@ export default function CreateTask() {
108
111
} ) ;
109
112
} ) ;
110
113
111
- // get segments of the selected project
112
- getSegments ( task . project_ID ) . then ( ( segmentData ) => {
113
- setData ( ( prevData ) => {
114
- return { ...prevData , segments : segmentData ? segmentData . map ( ( segment ) => {
115
- return { value : segment [ 0 ] , label : segment [ 0 ] , sections : segment [ 1 ] }
116
- } ) : [ ] } ;
117
- } )
118
- } ) ;
119
-
120
- // get constibutors of the selected project
121
- getContributors ( task . project_ID ) . then ( ( contributorsData ) => {
122
- setData ( ( prevData ) => {
123
- return { ...prevData , contributors : contributorsData ? contributorsData . map ( ( contributor ) => {
124
- return { ...contributor , value : contributor . EMAIL , label : contributor . USERNAME }
125
- } ) : [ ] } ;
126
- } )
127
- } ) ;
114
+ // get segments of the selected project
115
+ getSegments ( task . project_ID ) . then ( ( segmentData ) => {
116
+ setData ( ( prevData ) => {
117
+ return { ...prevData , segments : segmentData ? segmentData . map ( ( segment ) => {
118
+ return { value : segment [ 0 ] , label : segment [ 0 ] , sections : segment [ 1 ] }
119
+ } ) : [ ] } ;
120
+ } )
121
+ } ) ;
122
+
123
+ // get constibutors of the selected project
124
+ getContributors ( task . project_ID ) . then ( ( contributorsData ) => {
125
+ setData ( ( prevData ) => {
126
+ return { ...prevData , contributors : contributorsData ? contributorsData . map ( ( contributor ) => {
127
+ return { ...contributor , value : contributor . EMAIL , label : contributor . USERNAME }
128
+ } ) : [ ] } ;
129
+ } )
130
+ } ) ;
128
131
129
132
return ( ) => {
130
- // reset any selected moderators or tags from task array
133
+ // reset any selected moderators, tags, section or segment from task object
131
134
setTask ( ( prevTask ) => ( {
132
135
...prevTask ,
133
136
moderators : [ ] ,
134
137
tags : [ ] ,
138
+ assignee : undefined ,
139
+ section : undefined ,
140
+ segment : undefined ,
135
141
} ) ) ;
142
+
143
+ // reset section select options
144
+ setData ( ( prevData ) => ( {
145
+ ...prevData , sections : [ ] ,
146
+ } ) )
136
147
} ;
137
148
} , [ task . project_ID ] ) ;
138
149
@@ -150,17 +161,31 @@ export default function CreateTask() {
150
161
] [ "sections" ] . map ( ( section ) => ( { value : section , label : section } ) ) ,
151
162
} ;
152
163
} ) ;
164
+ return ( ( ) => {
165
+ // reset any selected section in the task object
166
+ setTask ( ( prevTask ) => ( {
167
+ ...prevTask ,
168
+ section : undefined ,
169
+ } ) ) ;
170
+ } )
153
171
} , [ task . segment ] ) ;
154
172
155
173
// handle submission of the task
156
174
async function handleTaskSubmit ( e ) {
175
+ setLoading ( true ) ;
157
176
e . preventDefault ( ) ;
158
177
// console.log(task);
159
178
addTask ( task ) . then ( ( ) => {
160
- alert ( "task added succesfully" ) ;
179
+ toast . success ( "Task added successfully !" , {
180
+ position : "top-center"
181
+ } ) ;
161
182
} ) . catch ( ( error ) => {
162
183
console . log ( error )
163
- alert ( "error adding task" ) ;
184
+ toast . error ( "Problem Adding Task !" , {
185
+ position : "top-center"
186
+ } ) ;
187
+ } ) . finally ( ( ) => {
188
+ setLoading ( false ) ;
164
189
} )
165
190
}
166
191
@@ -263,13 +288,14 @@ export default function CreateTask() {
263
288
// render task form
264
289
return (
265
290
< >
291
+ < ToastContainer />
266
292
< form className = { styles . form } onSubmit = { handleTaskSubmit } >
267
293
< div className = { styles . header } >
268
294
< img className = { styles . logoImg } src = "./O2logo.png" alt = "O2 logo" />
269
295
{ /* <h1>New Task</h1> */ }
270
296
< div className = { styles . btnCont } >
271
297
< button type = "submit" className = { styles . taskSubmit } >
272
- Create Task
298
+ { loading ? "Hold On..." : " Create Task" }
273
299
</ button >
274
300
< button type = "button" className = { styles . logoutBtn } onClick = { handleLogout } >
275
301
Logout
@@ -337,6 +363,9 @@ export default function CreateTask() {
337
363
< fieldset >
338
364
< legend > Assignee</ legend >
339
365
< Select
366
+ value = { task . assignee ? data . contributors [ data . contributors . findIndex ( ( contributor ) => {
367
+ return contributor . value === task . assignee ;
368
+ } ) ] : "" }
340
369
options = { data . contributors }
341
370
onChange = { ( select ) => handleSelectChange ( select , "assignee" ) }
342
371
autoFocus = { true }
@@ -353,6 +382,7 @@ export default function CreateTask() {
353
382
< fieldset >
354
383
< legend > Segment</ legend >
355
384
< Select
385
+ value = { task . segment ? { value : task . segment , label : task . segment } : "" }
356
386
options = { data . segments }
357
387
onChange = { ( select ) => handleSelectChange ( select , "segment" ) }
358
388
autoFocus = { true }
@@ -368,6 +398,7 @@ export default function CreateTask() {
368
398
< fieldset >
369
399
< legend > Section</ legend >
370
400
< Select
401
+ value = { task . section ? { value : task . section , label : task . section } : "" }
371
402
options = { data . sections }
372
403
onChange = { ( select ) => handleSelectChange ( select , "section" ) }
373
404
autoFocus = { true }
@@ -417,6 +448,9 @@ export default function CreateTask() {
417
448
< legend > Moderators</ legend >
418
449
< Select
419
450
isMulti
451
+ value = { task . moderators != [ ] ? modOptions . filter ( ( modOption ) => {
452
+ return task . moderators . includes ( modOption . value )
453
+ } ) : "" }
420
454
onChange = { handleModsSelect }
421
455
options = { modOptions }
422
456
placeholder = { "Moderators" }
0 commit comments