@@ -4,15 +4,17 @@ import { Stack } from "components/Stack/Stack"
4
4
import { ChangeEvent , FC , useState } from "react"
5
5
import Typography from "@material-ui/core/Typography"
6
6
import { allowedExtensions , isAllowedFile } from "util/templateVersion"
7
+ import { FileTree , validatePath } from "util/filetree"
7
8
8
9
export const CreateFileDialog : FC < {
9
10
onClose : ( ) => void
10
11
checkExists : ( path : string ) => boolean
11
12
onConfirm : ( path : string ) => void
12
13
open : boolean
13
- } > = ( { checkExists, onClose, onConfirm, open } ) => {
14
+ fileTree : FileTree
15
+ } > = ( { checkExists, onClose, onConfirm, open, fileTree } ) => {
14
16
const [ pathValue , setPathValue ] = useState ( "" )
15
- const [ error , setError ] = useState ( "" )
17
+ const [ error , setError ] = useState < string > ( )
16
18
const handleChange = ( event : ChangeEvent < HTMLInputElement > ) => {
17
19
setPathValue ( event . target . value )
18
20
}
@@ -32,7 +34,13 @@ export const CreateFileDialog: FC<{
32
34
)
33
35
return
34
36
}
37
+ const pathError = validatePath ( pathValue , fileTree )
38
+ if ( pathError ) {
39
+ setError ( pathError )
40
+ return
41
+ }
35
42
onConfirm ( pathValue )
43
+ setError ( undefined )
36
44
setPathValue ( "" )
37
45
}
38
46
@@ -41,6 +49,7 @@ export const CreateFileDialog: FC<{
41
49
open = { open }
42
50
onClose = { ( ) => {
43
51
onClose ( )
52
+ setError ( undefined )
44
53
setPathValue ( "" )
45
54
} }
46
55
onConfirm = { handleConfirm }
@@ -62,6 +71,7 @@ export const CreateFileDialog: FC<{
62
71
handleConfirm ( )
63
72
}
64
73
} }
74
+ error = { Boolean ( error ) }
65
75
helperText = { error }
66
76
name = "file-path"
67
77
autoComplete = "off"
@@ -109,9 +119,10 @@ export const RenameFileDialog: FC<{
109
119
checkExists : ( path : string ) => boolean
110
120
open : boolean
111
121
filename : string
112
- } > = ( { checkExists, onClose, onConfirm, open, filename } ) => {
122
+ fileTree : FileTree
123
+ } > = ( { checkExists, onClose, onConfirm, open, filename, fileTree } ) => {
113
124
const [ pathValue , setPathValue ] = useState ( filename )
114
- const [ error , setError ] = useState ( "" )
125
+ const [ error , setError ] = useState < string > ( )
115
126
const handleChange = ( event : ChangeEvent < HTMLInputElement > ) => {
116
127
setPathValue ( event . target . value )
117
128
}
@@ -127,11 +138,17 @@ export const RenameFileDialog: FC<{
127
138
if ( ! isAllowedFile ( pathValue ) ) {
128
139
const extensions = allowedExtensions . join ( ", " )
129
140
setError (
130
- `This extension is not allowed. You only can create files with the following extensions: ${ extensions } .` ,
141
+ `This extension is not allowed. You only can rename files with the following extensions: ${ extensions } .` ,
131
142
)
132
143
return
133
144
}
145
+ const pathError = validatePath ( pathValue , fileTree )
146
+ if ( pathError ) {
147
+ setError ( pathError )
148
+ return
149
+ }
134
150
onConfirm ( pathValue )
151
+ setError ( undefined )
135
152
setPathValue ( "" )
136
153
}
137
154
@@ -140,6 +157,7 @@ export const RenameFileDialog: FC<{
140
157
open = { open }
141
158
onClose = { ( ) => {
142
159
onClose ( )
160
+ setError ( undefined )
143
161
setPathValue ( "" )
144
162
} }
145
163
onConfirm = { handleConfirm }
@@ -161,6 +179,7 @@ export const RenameFileDialog: FC<{
161
179
handleConfirm ( )
162
180
}
163
181
} }
182
+ error = { Boolean ( error ) }
164
183
helperText = { error }
165
184
name = "file-path"
166
185
autoComplete = "off"
0 commit comments