1
1
import Select from "react-select" ;
2
- import { useEffect , useRef , useState } from "react" ;
2
+ import { useEffect , useState } from "react" ;
3
+ import { useAuthState } from "react-firebase-hooks/auth" ;
3
4
import {
4
5
addTask ,
5
6
getTags ,
6
7
getSegments ,
7
8
getContributors ,
8
- getProjects ,
9
- uploadImage ,
10
- logout
11
9
} from "../../utils/databaseOps" ;
12
-
10
+ import { auth , logout } from "../../../firebase" ;
13
11
import styles from "./CreateTask.module.css" ;
14
12
import { useNavigate , useLocation } from "react-router-dom" ;
15
13
import SummaryEdit from './components/SummaryEdit' ;
@@ -18,6 +16,24 @@ export default function CreateTask() {
18
16
const location = useLocation ( ) ;
19
17
const navigate = useNavigate ( ) ;
20
18
19
+ function handleLogout ( ) {
20
+ if ( ! user ) {
21
+ navigate ( '/' ) ;
22
+ }
23
+ logout ( ) ;
24
+ }
25
+
26
+ //If the user logs out redirect to login page
27
+ const [ user , loading ] = useAuthState ( auth ) ;
28
+ useEffect ( ( ) => {
29
+ if ( loading ) {
30
+ return ;
31
+ }
32
+ if ( ! user ) {
33
+ navigate ( "/" ) ;
34
+ }
35
+ } , [ user ] ) ;
36
+
21
37
// hardcoded values
22
38
const priorities = [
23
39
{ value : 1 , label : "Low" } ,
@@ -46,7 +62,9 @@ export default function CreateTask() {
46
62
47
63
// values per project basis
48
64
const [ data , setData ] = useState ( {
49
- projects : [ ] ,
65
+ projects : location . state ?. userData . PROJECTS . map ( ( project ) => {
66
+ return { value : project , label : project }
67
+ } ) ,
50
68
segments : [ ] ,
51
69
sections : [ ] ,
52
70
contributors : [ ] ,
@@ -75,35 +93,16 @@ export default function CreateTask() {
75
93
version : 4 ,
76
94
} )
77
95
78
- function logout ( ) {
79
- navigate ( "/" ) ;
80
- }
81
-
82
- // get projects on page load
83
- useEffect ( ( ) => {
84
- if ( ! location . state ?. userData )
85
- logout ( ) ;
86
- setData ( ( prevData ) => {
87
- return { ...prevData , projects : location . state ?. userData . PROJECTS . map ( ( project ) => {
88
- return { value : project , label : project }
89
- } ) }
90
- } )
91
- // getProjects(task.assigner).then((projects) => {
92
- // setData((prevData) => {
93
- // return {...prevData, projects : projects ? projects.map((project) => {
94
- // return {value : project, label : project}
95
- // }) : [] }
96
- // })
97
- // })
98
- } , [ location . state ?. userData ] )
96
+ // function logout(){
97
+ // navigate("/");
98
+ // }
99
99
100
100
// get project specific data when new project is selected
101
101
useEffect ( ( ) => {
102
102
if ( ! task . project_ID ) return ;
103
103
104
104
// get tags of the selected project
105
105
getTags ( task . project_ID ) . then ( ( tagsData ) => {
106
- console . log ( tagsData ) ;
107
106
setData ( ( prevData ) => {
108
107
return { ...prevData , tags : tagsData ? [ ...tagsData ] : [ ] } ;
109
108
} ) ;
@@ -172,7 +171,7 @@ export default function CreateTask() {
172
171
// event handler for handling changes in select
173
172
const handleSelectChange = ( select , spec ) => {
174
173
setTask ( ( prevTask ) => ( { ...prevTask , [ spec ] : select . value } ) ) ;
175
- console . log ( `Option selected:` , select . value ) ;
174
+ // console.log(`Option selected:`, select.value);
176
175
} ;
177
176
178
177
// event handler for handling project title and desc input
@@ -265,49 +264,20 @@ export default function CreateTask() {
265
264
} ) ) ;
266
265
}
267
266
268
- // Handler for handling the image drop
269
- function handleImageDrop ( e ) {
270
- e . preventDefault ( ) ;
271
- setDragging ( false ) ;
272
- const file = e . dataTransfer . files [ 0 ] ;
273
- if ( ( task , file ) ) {
274
- setUploadMessage ( `Uploading ${ file . name } ...` ) ;
275
- console . log ( "File" , file ) ;
276
- uploadImage ( task , file ) . then ( ( url ) => {
277
- console . log ( "IMAGE URL from store" , url ) ;
278
- const cursorPosition = descriptionRef . current . selectionStart ;
279
- const newDescription =
280
- task . description . slice ( 0 , cursorPosition ) +
281
- `` +
282
- task . description . slice ( cursorPosition ) ;
283
-
284
- setTask ( ( prevTask ) => ( { ...prevTask , description : newDescription } ) ) ;
285
- setUploadMessage ( null ) ;
286
- } ) ;
287
- }
288
- }
289
-
290
- // event handler for handling drag enter
291
- function handleDragEnter ( e ) {
292
- e . preventDefault ( ) ;
293
- setDragging ( true ) ;
294
- }
295
-
296
- // event handler for handling drag leave
297
- function handleDragLeave ( e ) {
298
- e . preventDefault ( ) ;
299
- setDragging ( false ) ;
300
- }
301
-
302
267
// render task form
303
268
return (
304
269
< >
305
270
< form className = { styles . form } onSubmit = { handleTaskSubmit } >
306
271
< div className = { styles . header } >
307
272
< h1 > New Task</ h1 >
308
- < button type = "submit" className = { styles . taskSubmit } >
309
- Create Task
310
- </ button >
273
+ < div className = { styles . btnCont } >
274
+ < button type = "submit" className = { styles . taskSubmit } >
275
+ Create Task
276
+ </ button >
277
+ < button type = "button" className = { styles . logoutBtn } onClick = { handleLogout } >
278
+ Logout
279
+ </ button >
280
+ </ div >
311
281
</ div >
312
282
< div className = { styles . taskSpecs } >
313
283
0 commit comments