2022-06-30 19:21:14 +02:00
import { Button , Group , NumberInput , Space , Notification , Modal , Center , Title } from '@mantine/core' ;
2022-06-30 17:50:31 +02:00
import { useForm } from '@mantine/hooks' ;
2022-06-30 19:21:14 +02:00
import React , { useEffect , useState } from 'react' ;
2022-06-30 17:50:31 +02:00
import { changeports , fireUpdateRequest , okNotify } from '../../js/utils' ;
import { ImCross } from "react-icons/im"
import { Service } from '../../js/models' ;
2022-06-30 19:21:14 +02:00
import { BsArrowDownSquareFill } from 'react-icons/bs' ;
2022-06-30 17:50:31 +02:00
type InputForm = {
internalPort :number ,
2022-06-30 19:21:14 +02:00
port :number
2022-06-30 17:50:31 +02:00
}
2022-06-30 19:21:14 +02:00
function ChangePortModal ( { service , opened , onClose } : { service :Service , opened :boolean , onClose : ( ) = > void } ) {
2022-06-30 17:50:31 +02:00
const form = useForm ( {
2022-06-30 19:21:14 +02:00
initialValues : {
internalPort : service.internal_port ,
port : service.public_port
} ,
validationRules : {
internalPort : ( value ) = > value > 0 && value < 65536 ,
port : ( value ) = > value > 0 && value < 65536
}
2022-06-30 17:50:31 +02:00
} )
2022-06-30 19:21:14 +02:00
useEffect ( ( ) = > {
form . setValues ( { internalPort : service.internal_port , port :service.public_port } )
} , [ opened ] )
2022-06-30 17:50:31 +02:00
const close = ( ) = > {
onClose ( )
form . reset ( )
setError ( null )
}
const [ submitLoading , setSubmitLoading ] = useState ( false )
const [ error , setError ] = useState < string | null > ( null )
const submitRequest = ( data :InputForm ) = > {
setSubmitLoading ( true )
changeports ( service . id , data ) . then ( res = > {
if ( ! res ) {
setSubmitLoading ( false )
close ( ) ;
fireUpdateRequest ( ) ;
okNotify ( ` Internal port on ${ service . name } service has changed in ${ data . internalPort } ` , ` Successfully changed internal port of service with id ${ service . id } ` )
} else {
setSubmitLoading ( false )
setError ( "Invalid request! [ " + res + " ]" )
}
} ) . catch ( err = > {
setSubmitLoading ( false )
setError ( "Request Failed! [ " + err + " ]" )
} )
}
2022-06-30 19:21:14 +02:00
return < Modal size = "xl" title = "Change Ports" opened = { opened } onClose = { close } closeOnClickOutside = { false } centered >
2022-06-30 17:50:31 +02:00
< form onSubmit = { form . onSubmit ( submitRequest ) } >
2022-06-30 19:21:14 +02:00
2022-06-30 17:50:31 +02:00
< NumberInput
2022-06-30 19:21:14 +02:00
placeholder = "30001"
2022-06-30 17:50:31 +02:00
min = { 1 }
max = { 65535 }
label = "Internal Proxy Port"
{ . . . form . getInputProps ( 'internalPort' ) }
2022-06-30 19:21:14 +02:00
/ >
< Space h = "xl" / >
< Center > < BsArrowDownSquareFill size = { 50 } / > < / Center >
< NumberInput
placeholder = "8080"
min = { 1 }
max = { 65535 }
label = "Public Service Port"
{ . . . form . getInputProps ( 'port' ) }
2022-06-30 17:50:31 +02:00
/ >
2022-06-30 19:21:14 +02:00
< Space h = "xl" / >
< Center > < Title order = { 5 } > The change of the ports will cause a temporarily shutdown of the service ! ⚠ ️ < / Title > < / Center >
< Space h = "md" / >
2022-06-30 17:50:31 +02:00
< Group position = "right" mt = "md" >
2022-06-30 19:21:14 +02:00
< Button loading = { submitLoading } disabled = {
service . internal_port === form . values . internalPort && service . public_port === form . values . port
} type = "submit" > Change Port < / Button >
2022-06-30 17:50:31 +02:00
< / Group >
< Space h = "md" / >
{ error ? < >
< Notification icon = { < ImCross size = { 14 } / > } color = "red" onClose = { ( ) = > { setError ( null ) } } >
Error : { error }
< / Notification > < Space h = "md" / > < / > : null }
< / form >
< / Modal >
}
2022-06-30 19:21:14 +02:00
export default ChangePortModal ;