1
- import React , { ChangeEvent , useState } from 'react' ;
1
+ import React , { ChangeEvent , useContext , useEffect , useState } from 'react' ;
2
2
import './index.scss' ;
3
3
import tick from '../../app/assets/images/tick.png' ;
4
- import { useNavigate } from 'react-router-dom' ;
4
+ import { useNavigate , useParams } from 'react-router-dom' ;
5
5
import { getUser } from 'app/api/user' ;
6
6
import toast from 'react-hot-toast' ;
7
7
import { useQuery } from 'react-query' ;
8
8
import axios from 'axios' ;
9
9
import { addProject } from 'app/api/project' ;
10
+ import UserContext from 'app/context/user/userContext' ;
11
+ import { OrgProjects , Projects , getOrgProjects } from 'app/api/organization' ;
10
12
const AddProject = ( ) => {
11
13
const navigate = useNavigate ( ) ;
14
+ const userContext = useContext ( UserContext ) ;
12
15
const token = localStorage . getItem ( 'token' ) ;
13
- const orgName = 'orgName' ;
16
+ const { spaceName } = useParams ( ) ;
14
17
const [ name , setName ] = useState < string | null > ( null ) ;
15
18
const [ description , setDescription ] = useState < string | null > ( null ) ;
16
19
const [ link , setLink ] = useState < string | null > ( null ) ;
17
20
18
- const [ validLink , setValidLink ] = useState < boolean > ( false ) ;
19
21
20
- const [ validName , setValidName ] = useState < boolean > ( false ) ;
22
+ const [ orgProject , setOrgProjects ] = useState < Projects | null > ( null ) ;
21
23
22
-
23
-
24
- const linkChange = async ( event : ChangeEvent < HTMLInputElement > ) => {
25
- setLink ( event . target . value ) ;
26
-
27
- if ( isGitHubRepositoryLink ( event . target . value ) ) {
24
+ const fetchData = async ( ) => {
25
+ if ( token && spaceName ) {
28
26
try {
29
- const response = await axios . get ( event . target . value ) ;
30
- setValidLink ( true ) ;
31
- return ;
27
+ const res = await getOrgProjects ( token , spaceName ) ;
28
+ console . log ( res . data . projects )
29
+ setOrgProjects ( res . data . projects ) ;
32
30
} catch ( e ) { }
33
31
}
32
+ } ;
34
33
35
- setValidLink ( false ) ;
34
+ useEffect ( ( ) => {
35
+ fetchData ( ) ;
36
+ } , [ spaceName ] ) ;
37
+
38
+ const linkChange = async ( event : ChangeEvent < HTMLInputElement > ) => {
39
+ setLink ( event . target . value ) ;
36
40
} ;
37
41
38
42
const nameChange = async ( event : ChangeEvent < HTMLInputElement > ) => {
39
43
setName ( event . target . value ) ;
40
- setValidName ( true ) ;
41
44
} ;
42
45
43
46
function isGitHubRepositoryLink ( link : string ) : boolean {
@@ -49,39 +52,51 @@ const AddProject = () => {
49
52
return githubRepoPattern . test ( link ) ;
50
53
}
51
54
55
+ function isValidName ( input : string ) : boolean {
56
+ // Regular expression to match only alphanumeric characters, hyphens, and underscores
57
+ const regex = / ^ [ a - z A - Z 0 - 9 - _ ] + $ / ;
58
+
59
+ // Test if the input string matches the regular expression
60
+ return regex . test ( input ) ;
61
+ }
62
+
63
+ const isUnique = ( name : string ) => {
64
+ if ( orgProject && name in orgProject ) {
65
+ return false ;
66
+ }
67
+ return true ;
68
+ }
69
+
52
70
const SubmitHandler = async ( ) => {
53
71
if (
72
+ spaceName &&
54
73
token &&
55
74
name &&
56
- validName &&
57
- validLink &&
58
- description &&
59
75
link &&
60
- description ?. length > 3
76
+ isValidName ( name ) &&
77
+ isGitHubRepositoryLink ( link ) &&
78
+ description &&
79
+
80
+ description ?. length < 200
61
81
) {
62
- try {
63
- const res = await addProject ( token , orgName , {
82
+ const func = async ( ) => {
83
+ const res = await addProject ( token , spaceName , {
64
84
name : name ,
65
85
description : description ,
66
86
link : link ,
67
87
} ) ;
68
- } catch ( e ) {
69
- toast . error ( 'Error while saving' ) ;
70
- }
88
+ navigate ( `/workspace/${ spaceName } ` ) ;
89
+ } ;
90
+ toast . promise ( func ( ) , {
91
+ loading : 'Saving Project' ,
92
+ success : < b > Project saved</ b > ,
93
+ error : < b > Could not save</ b > ,
94
+ } ) ;
95
+ } else {
96
+ toast . error ( 'Invalid inputs' ) ;
71
97
}
72
-
73
- toast . error ( 'Invalid inputs' ) ;
74
98
} ;
75
99
76
-
77
- toast . promise (
78
- SubmitHandler ( ) , {
79
- loading : 'Saving Project' ,
80
- success : < b > Project saved</ b > ,
81
- error :< b > Could not save</ b >
82
- }
83
- )
84
-
85
100
return (
86
101
< div >
87
102
< div className = 'add-project-container' >
@@ -93,13 +108,18 @@ const AddProject = () => {
93
108
onChange = { nameChange }
94
109
value = { name ? name : '' }
95
110
/>
111
+ { ! name ? 'Name feild should not be empty' : < > </ > }
112
+ { name && ! isValidName ( name ) && 'Not a valid name' }
113
+ { name && ! isUnique ( name ) && "This project name already exists" }
96
114
< div className = 'input-title' > Project link</ div >
97
115
< input
98
116
type = 'text'
99
117
value = { link ? link : '' }
100
118
onChange = { linkChange }
101
119
placeholder = 'Github link of project'
102
120
/>
121
+ { ! link ? 'Link feild should not be empty' : < > </ > }
122
+ { link && ! isGitHubRepositoryLink ( link ) && "Not a valid github repository link" }
103
123
< div className = 'input-title' > Description</ div >
104
124
< input
105
125
type = 'text'
@@ -109,8 +129,10 @@ const AddProject = () => {
109
129
}
110
130
placeholder = 'Details about project'
111
131
/>
132
+ { ! description ? 'Description feild should not be empty' : < > </ > }
133
+ { description && description . length >= 200 && "Description length should not be greater than 200" }
112
134
</ form >
113
- < button className = 'add-project-btn' >
135
+ < button className = 'add-project-btn' onClick = { SubmitHandler } >
114
136
< img src = { tick } alt = '' /> Done
115
137
</ button >
116
138
</ div >
0 commit comments