1616 * under the License.
1717 */
1818
19- import React , { useEffect , useState } from 'react' ;
19+ import React , { useEffect , useRef , useState } from 'react' ;
20+ import { useAppContext } from 'AppComponents/Shared/AppContext' ;
2021import { styled } from '@mui/material/styles' ;
2122import PropTypes from 'prop-types' ;
2223import TextField from '@mui/material/TextField' ;
@@ -42,13 +43,15 @@ const StyledSpan = styled('span')(({ theme }) => ({ color: theme.palette.error.d
4243function AddEditVhost ( props ) {
4344 const intl = useIntl ( ) ;
4445 const {
45- onVhostChange, initialVhosts, gatewayType,
46+ onVhostChange, initialVhosts, gatewayType, isEditMode ,
4647 } = props ;
4748 const [ userVhosts , setUserVhosts ] = useState ( [ ] ) ;
4849 const [ id , setId ] = useState ( 0 ) ;
4950 const defaultVhost = {
5051 host : '' , httpContext : '' , httpsPort : 8243 , httpPort : 8280 , wssPort : 8099 , wsPort : 9099 , isNew : true ,
5152 } ;
53+ const prevRef = useRef ( ) ;
54+ const { settings } = useAppContext ( ) ;
5255
5356 // change handlers
5457 const updateChanges = ( key , field , newValue ) => {
@@ -142,7 +145,38 @@ function AddEditVhost(props) {
142145 } ;
143146
144147 useEffect ( ( ) => {
145- if ( ! isUserVhostsUpdated ( userVhosts ) && initialVhosts && initialVhosts . length > 0 ) {
148+ if ( prevRef . gatewayType !== gatewayType ) {
149+ const config = settings . gatewayConfiguration . filter ( ( t ) => t . type === gatewayType ) [ 0 ] ;
150+ if ( initialVhosts && initialVhosts . length > 0 ) {
151+ let i = 0 ;
152+ if ( config && ! isEditMode ) {
153+ const defaultHostnameTemplate = config . defaultHostnameTemplate
154+ ? config . defaultHostnameTemplate : '' ;
155+ setUserVhosts ( initialVhosts . map ( ( vhost ) => {
156+ const keyedVhost = vhost ;
157+ keyedVhost . key = '' + i ++ ;
158+ keyedVhost . host = defaultHostnameTemplate ;
159+ return keyedVhost ;
160+ } ) ) ;
161+ } else {
162+ setUserVhosts ( initialVhosts . map ( ( vhost ) => {
163+ const keyedVhost = vhost ;
164+ keyedVhost . key = '' + i ++ ;
165+ return keyedVhost ;
166+ } ) ) ;
167+ }
168+ setId ( i ) ;
169+ } else {
170+ setId ( id + 1 ) ;
171+ const vhost = defaultVhost ;
172+ vhost . key = '' + id ;
173+ if ( config && config . defaultHostnameTemplate ) {
174+ vhost . host = config . defaultHostnameTemplate ;
175+ }
176+ setUserVhosts ( [ vhost ] ) ;
177+ }
178+ prevRef . gatewayType = gatewayType ;
179+ } else if ( ! isUserVhostsUpdated ( userVhosts ) && initialVhosts && initialVhosts . length > 0 ) {
146180 let i = 0 ;
147181 setUserVhosts ( initialVhosts . map ( ( vhost ) => {
148182 const keyedVhost = vhost ;
@@ -151,7 +185,7 @@ function AddEditVhost(props) {
151185 } ) ) ;
152186 setId ( i ) ;
153187 }
154- } , [ initialVhosts ] ) ;
188+ } , [ initialVhosts , gatewayType ] ) ;
155189
156190 let vhostCounter = 1 ;
157191 return (
0 commit comments