1
1
import React , { useState } from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
2
import InputLabel from '@material-ui/core/InputLabel' ;
4
3
import Input from '@material-ui/core/Input' ;
5
4
import FormControl from '@material-ui/core/FormControl' ;
@@ -17,15 +16,35 @@ import { makeStyles } from '@material-ui/core/styles';
17
16
import styles from '../../../assets/jss/material-dashboard-react/views/dashboardStyle' ;
18
17
import { RepoIcon } from '@primer/octicons-react' ;
19
18
20
- const useStyles = makeStyles ( styles ) ;
19
+ interface AddRepositoryDialogProps {
20
+ open : boolean ;
21
+ onClose : ( ) => void ;
22
+ onSuccess : ( data : RepositoryData ) => void ;
23
+ }
24
+
25
+ export interface RepositoryData {
26
+ _id ?: string ;
27
+ project : string ;
28
+ name : string ;
29
+ url : string ;
30
+ maxUser : number ;
31
+ lastModified ?: string ;
32
+ dateCreated ?: string ;
33
+ proxyURL ?: string ;
34
+ }
21
35
22
- function AddRepositoryDialog ( props ) {
36
+ interface NewRepoProps {
37
+ onSuccess : ( data : RepositoryData ) => void ;
38
+ }
39
+
40
+ const useStyles = makeStyles ( styles as any ) ;
41
+
42
+ const AddRepositoryDialog : React . FC < AddRepositoryDialogProps > = ( { open, onClose, onSuccess } ) => {
23
43
const [ project , setProject ] = useState ( '' ) ;
24
44
const [ name , setName ] = useState ( '' ) ;
25
45
const [ url , setUrl ] = useState ( '' ) ;
26
46
const [ error , setError ] = useState ( '' ) ;
27
47
const [ tip , setTip ] = useState ( false ) ;
28
- const { onClose, open, onSuccess } = props ;
29
48
const classes = useStyles ( ) ;
30
49
31
50
const handleClose = ( ) => {
@@ -34,7 +53,7 @@ function AddRepositoryDialog(props) {
34
53
onClose ( ) ;
35
54
} ;
36
55
37
- const handleSuccess = ( data ) => {
56
+ const handleSuccess = ( data : RepositoryData ) => {
38
57
onSuccess ( data ) ;
39
58
setTip ( true ) ;
40
59
} ;
@@ -46,27 +65,27 @@ function AddRepositoryDialog(props) {
46
65
} ;
47
66
48
67
const add = async ( ) => {
49
- const data = {
50
- project : project ,
51
- name : name ,
52
- url : url ,
68
+ const data : RepositoryData = {
69
+ project : project . trim ( ) ,
70
+ name : name . trim ( ) ,
71
+ url : url . trim ( ) ,
53
72
maxUser : 1 ,
54
73
} ;
55
74
56
- if ( data . project . trim ( ) . length == 0 || data . project . length > 100 ) {
57
- setError ( 'project name length unexpected ' ) ;
75
+ if ( data . project . length = == 0 || data . project . length > 100 ) {
76
+ setError ( 'Project name length must be between 1 and 100 characters ' ) ;
58
77
return ;
59
78
}
60
79
61
- if ( data . name . trim ( ) . length == 0 || data . name . length > 100 ) {
62
- setError ( 'Repo name length unexpected ' ) ;
80
+ if ( data . name . length = == 0 || data . name . length > 100 ) {
81
+ setError ( 'Repository name length must be between 1 and 100 characters ' ) ;
63
82
return ;
64
83
}
65
84
66
85
try {
67
86
new URL ( data . url ) ;
68
87
} catch {
69
- setError ( 'Invalid URL' ) ;
88
+ setError ( 'Invalid URL format ' ) ;
70
89
return ;
71
90
}
72
91
@@ -75,15 +94,15 @@ function AddRepositoryDialog(props) {
75
94
handleSuccess ( data ) ;
76
95
handleClose ( ) ;
77
96
} catch ( e ) {
78
- if ( e . message ) {
97
+ if ( e instanceof Error ) {
79
98
setError ( e . message ) ;
80
99
} else {
81
- setError ( e . toString ( ) ) ;
100
+ setError ( 'An unexpected error occurred' ) ;
82
101
}
83
102
}
84
103
} ;
85
104
86
- const inputStyle = {
105
+ const inputStyle : React . CSSProperties = {
87
106
width : '100%' ,
88
107
} ;
89
108
@@ -106,9 +125,11 @@ function AddRepositoryDialog(props) {
106
125
fullWidth
107
126
maxWidth = 'md'
108
127
>
109
- < DialogTitle style = { { color : 'red' } } id = 'simple-dialog-title' >
110
- { error }
111
- </ DialogTitle >
128
+ { error && (
129
+ < DialogTitle style = { { color : 'red' } } id = 'simple-dialog-title' >
130
+ { error }
131
+ </ DialogTitle >
132
+ ) }
112
133
< DialogTitle style = { { textAlign : 'left' } } className = { classes . cardTitle } >
113
134
Add a repository...
114
135
</ DialogTitle >
@@ -123,6 +144,7 @@ function AddRepositoryDialog(props) {
123
144
inputProps = { { maxLength : 200 , minLength : 3 } }
124
145
aria-describedby = 'project-helper-text'
125
146
onChange = { ( e ) => setProject ( e . target . value ) }
147
+ value = { project }
126
148
/>
127
149
< FormHelperText id = 'project-helper-text' > GitHub Organization</ FormHelperText >
128
150
</ FormControl >
@@ -135,6 +157,7 @@ function AddRepositoryDialog(props) {
135
157
id = 'name'
136
158
aria-describedby = 'name-helper-text'
137
159
onChange = { ( e ) => setName ( e . target . value ) }
160
+ value = { name }
138
161
/>
139
162
< FormHelperText id = 'name-helper-text' > GitHub Repository Name</ FormHelperText >
140
163
</ FormControl >
@@ -148,6 +171,7 @@ function AddRepositoryDialog(props) {
148
171
id = 'url'
149
172
aria-describedby = 'url-helper-text'
150
173
onChange = { ( e ) => setUrl ( e . target . value ) }
174
+ value = { url }
151
175
/>
152
176
< FormHelperText id = 'url-helper-text' > GitHub Repository URL</ FormHelperText >
153
177
</ FormControl >
@@ -168,20 +192,11 @@ function AddRepositoryDialog(props) {
168
192
</ Dialog >
169
193
</ >
170
194
) ;
171
- }
172
-
173
- AddRepositoryDialog . propTypes = {
174
- onClose : PropTypes . func . isRequired ,
175
- open : PropTypes . bool . isRequired ,
176
- onSuccess : PropTypes . func . isRequired ,
177
195
} ;
178
196
179
- NewRepo . propTypes = {
180
- onSuccess : PropTypes . func . isRequired ,
181
- } ;
197
+ const NewRepo : React . FC < NewRepoProps > = ( { onSuccess } ) => {
198
+ const [ open , setOpen ] = useState ( false ) ;
182
199
183
- export default function NewRepo ( props ) {
184
- const [ open , setOpen ] = React . useState ( false ) ;
185
200
const handleClickOpen = ( ) => {
186
201
setOpen ( true ) ;
187
202
} ;
@@ -193,9 +208,11 @@ export default function NewRepo(props) {
193
208
return (
194
209
< div >
195
210
< Button color = 'success' onClick = { handleClickOpen } >
196
- < RepoIcon > </ RepoIcon > Add repository
211
+ < RepoIcon /> Add repository
197
212
</ Button >
198
- < AddRepositoryDialog open = { open } onClose = { handleClose } onSuccess = { props . onSuccess } />
213
+ < AddRepositoryDialog open = { open } onClose = { handleClose } onSuccess = { onSuccess } />
199
214
</ div >
200
215
) ;
201
- }
216
+ } ;
217
+
218
+ export default NewRepo ;
0 commit comments