1- import React , { useState } from 'react' ;
2- import { Modal , Button , Form } from 'react-bootstrap' ;
1+ import React , { useEffect , useState } from 'react' ;
2+ import { Button , Form , Modal } from 'react-bootstrap' ;
3+
4+ export type LandscapeTokenCreateParams = {
5+ alias : string ;
6+ value ?: string ;
7+ secret ?: string ;
8+ } ;
39
410interface TokenCreationModalProps {
511 show : boolean ;
612 handleClose : ( ) => void ;
7- createToken : ( tokenAlias : string ) => Promise < void > ;
13+ createToken : ( params : LandscapeTokenCreateParams ) => Promise < void > ;
814}
915
16+ const emptyFormState = ( ) : LandscapeTokenCreateParams => ( {
17+ alias : '' ,
18+ value : '' ,
19+ secret : '' ,
20+ } ) ;
21+
1022export default function TokenCreationModal ( {
1123 show,
1224 handleClose,
1325 createToken,
1426} : TokenCreationModalProps ) {
15- const [ tokenAlias , setTokenAlias ] = useState ( '' ) ;
27+ const [ formState , setFormState ] =
28+ useState < LandscapeTokenCreateParams > ( emptyFormState ) ;
29+ const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
1630
17- const handleSubmit = ( e : React . FormEvent ) => {
31+ useEffect ( ( ) => {
32+ if ( ! show ) {
33+ setFormState ( emptyFormState ( ) ) ;
34+ setIsSubmitting ( false ) ;
35+ }
36+ } , [ show ] ) ;
37+
38+ const handleSubmit = async ( e : React . FormEvent ) => {
1839 e . preventDefault ( ) ;
19- createToken ( tokenAlias ) ;
20- handleClose ( ) ;
40+ setIsSubmitting ( true ) ;
41+
42+ const params : LandscapeTokenCreateParams = {
43+ alias : formState . alias . trim ( ) ,
44+ } ;
45+ const value = formState . value ?. trim ( ) ;
46+ const secret = formState . secret ?. trim ( ) ;
47+ if ( value ) {
48+ params . value = value ;
49+ }
50+ if ( secret ) {
51+ params . secret = secret ;
52+ }
53+
54+ try {
55+ await createToken ( params ) ;
56+ } finally {
57+ setIsSubmitting ( false ) ;
58+ }
2159 } ;
2260
2361 return (
@@ -27,20 +65,54 @@ export default function TokenCreationModal({
2765 </ Modal . Header >
2866 < Modal . Body >
2967 < Form onSubmit = { handleSubmit } >
30- < Form . Group controlId = "tokenAlias" >
31- < Form . Label > Alias (may be left empty )</ Form . Label >
68+ < Form . Group controlId = "tokenAlias" className = "mb-3" >
69+ < Form . Label > Alias (optional )</ Form . Label >
3270 < Form . Control
3371 type = "text"
3472 autoFocus
35- value = { tokenAlias }
36- onChange = { ( e ) => setTokenAlias ( e . target . value ) }
73+ value = { formState . alias }
74+ onChange = { ( e ) =>
75+ setFormState ( ( prev ) => ( { ...prev , alias : e . target . value } ) )
76+ }
77+ />
78+ </ Form . Group >
79+ < Form . Group controlId = "tokenValue" className = "mb-3" >
80+ < Form . Label > Landscape Token (optional)</ Form . Label >
81+ < Form . Control
82+ type = "text"
83+ placeholder = "Leave empty to generate automatically"
84+ value = { formState . value ?? '' }
85+ onChange = { ( e ) =>
86+ setFormState ( ( prev ) => ( { ...prev , value : e . target . value } ) )
87+ }
88+ />
89+ < Form . Text className = "text-muted" >
90+ Specify a custom token value or leave empty to generate one.
91+ </ Form . Text >
92+ </ Form . Group >
93+ < Form . Group controlId = "tokenSecret" className = "mb-3" >
94+ < Form . Label > Token Secret (optional)</ Form . Label >
95+ < Form . Control
96+ type = "text"
97+ placeholder = "Leave empty to generate automatically"
98+ value = { formState . secret ?? '' }
99+ onChange = { ( e ) =>
100+ setFormState ( ( prev ) => ( { ...prev , secret : e . target . value } ) )
101+ }
37102 />
103+ < Form . Text className = "text-muted" >
104+ Specify a custom token secret or leave empty to generate one.
105+ </ Form . Text >
38106 </ Form . Group >
39107 < Modal . Footer >
40108 < Button variant = "outline-secondary" onClick = { handleClose } >
41109 Cancel
42110 </ Button >
43- < Button type = "submit" variant = "outline-primary" >
111+ < Button
112+ type = "submit"
113+ variant = "outline-primary"
114+ disabled = { isSubmitting }
115+ >
44116 Create
45117 </ Button >
46118 </ Modal . Footer >
0 commit comments