2022-07-21 20:25:39 +02:00
import { Button , Group , Space , TextInput , Notification , Modal } from '@mantine/core' ;
2023-06-05 00:55:38 +02:00
import { useForm } from '@mantine/form' ;
2023-09-24 05:48:54 +02:00
import { useEffect , useState } from 'react' ;
2022-07-22 00:34:57 +02:00
import { okNotify } from '../../../js/utils' ;
2022-07-21 20:25:39 +02:00
import { ImCross } from "react-icons/im"
2022-07-21 21:17:06 +02:00
import { regexproxy , Service } from '../utils' ;
2022-07-21 20:25:39 +02:00
function RenameForm ( { opened , onClose , service } : { opened :boolean , onClose : ( ) = > void , service :Service } ) {
const form = useForm ( {
initialValues : { name :service.name } ,
2023-06-14 21:49:15 +02:00
validate : { name : ( value ) = > value !== "" ? null : "Service name is required" }
2022-07-21 20:25:39 +02:00
} )
const close = ( ) = > {
onClose ( )
form . reset ( )
setError ( null )
}
useEffect ( ( ) = > form . setFieldValue ( "name" , service . name ) , [ opened ] )
const [ submitLoading , setSubmitLoading ] = useState ( false )
const [ error , setError ] = useState < string | null > ( null )
const submitRequest = ( { name } : { name :string } ) = > {
setSubmitLoading ( true )
2022-07-21 21:17:06 +02:00
regexproxy . servicerename ( service . id , name ) . then ( res = > {
2022-07-21 20:25:39 +02:00
if ( ! res ) {
setSubmitLoading ( false )
close ( ) ;
okNotify ( ` Service ${ service . name } has been renamed in ${ name } ` , ` Successfully renamed service on port ${ service . public_port } ` )
} else {
setSubmitLoading ( false )
setError ( "Error: [ " + res + " ]" )
}
} ) . catch ( err = > {
setSubmitLoading ( false )
setError ( "Request Failed! [ " + err + " ]" )
} )
}
return < Modal size = "xl" title = { ` Rename ' ${ service . name } ' service on port ${ service . public_port } ` } opened = { opened } onClose = { close } closeOnClickOutside = { false } centered >
< form onSubmit = { form . onSubmit ( submitRequest ) } >
< TextInput
label = "Service Name"
placeholder = "Awesome Service Name!"
{ . . . form . getInputProps ( 'name' ) }
/ >
2024-10-13 01:50:52 +02:00
< Group align = "right" mt = "md" >
2022-07-21 20:25:39 +02:00
< Button loading = { submitLoading } type = "submit" > Rename < / Button >
< / Group >
< Space h = "md" / >
{ error ? < >
< Notification icon = { < ImCross size = { 14 } / > } color = "red" onClose = { ( ) = > { setError ( null ) } } >
Error : { error }
< / Notification > < Space h = "md" / > < / > : null }
< / form >
< / Modal >
}
2023-06-05 00:55:38 +02:00
export default RenameForm ;