11import Select from "react-select" ;
2- import { useEffect , useRef , useState } from "react" ;
2+ import { useEffect , useState } from "react" ;
33import { useAuthState } from "react-firebase-hooks/auth" ;
44import {
55 addTask ,
@@ -11,6 +11,8 @@ import { auth, logout} from "../../../firebase";
1111import styles from "./CreateTask.module.css" ;
1212import { useNavigate , useLocation } from "react-router-dom" ;
1313import SummaryEdit from './components/SummaryEdit' ;
14+ import { toast , ToastContainer } from 'react-toastify' ;
15+ import "react-toastify/dist/ReactToastify.css" ;
1416
1517export default function CreateTask ( ) {
1618 const location = useLocation ( ) ;
@@ -24,14 +26,17 @@ export default function CreateTask() {
2426 }
2527
2628 //If the user logs out redirect to login page
27- const [ user , loading ] = useAuthState ( auth ) ;
29+ const [ user ] = useAuthState ( auth ) ;
2830 useEffect ( ( ) => {
2931 if ( loading ) {
3032 return ;
3133 }
3234 if ( ! user ) {
3335 navigate ( "/" ) ;
3436 }
37+ if ( ! location . state . userData ) {
38+ navigate ( "/" ) ;
39+ }
3540 } , [ user ] ) ;
3641
3742 // hardcoded values
@@ -93,9 +98,7 @@ export default function CreateTask() {
9398 version : 4 ,
9499 } )
95100
96- // function logout(){
97- // navigate("/");
98- // }
101+ const [ loading , setLoading ] = useState ( false ) ;
99102
100103 // get project specific data when new project is selected
101104 useEffect ( ( ) => {
@@ -108,31 +111,39 @@ export default function CreateTask() {
108111 } ) ;
109112 } ) ;
110113
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+ } ) ;
128131
129132 return ( ) => {
130- // reset any selected moderators or tags from task array
133+ // reset any selected moderators, tags, section or segment from task object
131134 setTask ( ( prevTask ) => ( {
132135 ...prevTask ,
133136 moderators : [ ] ,
134137 tags : [ ] ,
138+ assignee : undefined ,
139+ section : undefined ,
140+ segment : undefined ,
135141 } ) ) ;
142+
143+ // reset section select options
144+ setData ( ( prevData ) => ( {
145+ ...prevData , sections : [ ] ,
146+ } ) )
136147 } ;
137148 } , [ task . project_ID ] ) ;
138149
@@ -150,17 +161,31 @@ export default function CreateTask() {
150161 ] [ "sections" ] . map ( ( section ) => ( { value : section , label : section } ) ) ,
151162 } ;
152163 } ) ;
164+ return ( ( ) => {
165+ // reset any selected section in the task object
166+ setTask ( ( prevTask ) => ( {
167+ ...prevTask ,
168+ section : undefined ,
169+ } ) ) ;
170+ } )
153171 } , [ task . segment ] ) ;
154172
155173 // handle submission of the task
156174 async function handleTaskSubmit ( e ) {
175+ setLoading ( true ) ;
157176 e . preventDefault ( ) ;
158177 // console.log(task);
159178 addTask ( task ) . then ( ( ) => {
160- alert ( "task added succesfully" ) ;
179+ toast . success ( "Task added successfully !" , {
180+ position : "top-center"
181+ } ) ;
161182 } ) . catch ( ( error ) => {
162183 console . log ( error )
163- alert ( "error adding task" ) ;
184+ toast . error ( "Problem Adding Task !" , {
185+ position : "top-center"
186+ } ) ;
187+ } ) . finally ( ( ) => {
188+ setLoading ( false ) ;
164189 } )
165190 }
166191
@@ -263,13 +288,14 @@ export default function CreateTask() {
263288 // render task form
264289 return (
265290 < >
291+ < ToastContainer />
266292 < form className = { styles . form } onSubmit = { handleTaskSubmit } >
267293 < div className = { styles . header } >
268294 < img className = { styles . logoImg } src = "./O2logo.png" alt = "O2 logo" />
269295 { /* <h1>New Task</h1> */ }
270296 < div className = { styles . btnCont } >
271297 < button type = "submit" className = { styles . taskSubmit } >
272- Create Task
298+ { loading ? "Hold On..." : " Create Task" }
273299 </ button >
274300 < button type = "button" className = { styles . logoutBtn } onClick = { handleLogout } >
275301 Logout
@@ -337,6 +363,9 @@ export default function CreateTask() {
337363 < fieldset >
338364 < legend > Assignee</ legend >
339365 < Select
366+ value = { task . assignee ? data . contributors [ data . contributors . findIndex ( ( contributor ) => {
367+ return contributor . value === task . assignee ;
368+ } ) ] : "" }
340369 options = { data . contributors }
341370 onChange = { ( select ) => handleSelectChange ( select , "assignee" ) }
342371 autoFocus = { true }
@@ -353,6 +382,7 @@ export default function CreateTask() {
353382 < fieldset >
354383 < legend > Segment</ legend >
355384 < Select
385+ value = { task . segment ? { value : task . segment , label : task . segment } : "" }
356386 options = { data . segments }
357387 onChange = { ( select ) => handleSelectChange ( select , "segment" ) }
358388 autoFocus = { true }
@@ -368,6 +398,7 @@ export default function CreateTask() {
368398 < fieldset >
369399 < legend > Section</ legend >
370400 < Select
401+ value = { task . section ? { value : task . section , label : task . section } : "" }
371402 options = { data . sections }
372403 onChange = { ( select ) => handleSelectChange ( select , "section" ) }
373404 autoFocus = { true }
@@ -417,6 +448,9 @@ export default function CreateTask() {
417448 < legend > Moderators</ legend >
418449 < Select
419450 isMulti
451+ value = { task . moderators != [ ] ? modOptions . filter ( ( modOption ) => {
452+ return task . moderators . includes ( modOption . value )
453+ } ) : "" }
420454 onChange = { handleModsSelect }
421455 options = { modOptions }
422456 placeholder = { "Moderators" }
0 commit comments