1- import { useEffect , useState } from 'react' ;
1+ import { useState } from 'react' ;
22import { useDispatch } from 'react-redux' ;
33import { useNavigate } from 'react-router-dom' ;
44
@@ -15,7 +15,6 @@ import {setAlert} from '@redux/reducers/alert';
1515import { closeStaticRouteModal } from '@redux/reducers/ui' ;
1616import { useCreateStaticRouteMutation } from '@redux/services/enhancedApi' ;
1717
18- import { renderErrorModal } from '@components/AntdCustom' ;
1918import { TargetForm } from '@components/TargetForm' ;
2019
2120import PathForm from './PathForm' ;
@@ -27,66 +26,69 @@ const AddStaticRouteModal = () => {
2726 const navigate = useNavigate ( ) ;
2827 const dispatch = useDispatch ( ) ;
2928 const [ form ] = Form . useForm ( ) ;
30- const [ createStaticRoute , { error , isError, reset, isUninitialized } ] = useCreateStaticRouteMutation ( ) ;
29+ const [ createStaticRoute , { isError, reset} ] = useCreateStaticRouteMutation ( ) ;
3130 const [ currentStep , setCurrentStep ] = useState ( 0 ) ;
3231 const step = currentStep === 0 ? 'routeInfo' : currentStep === 1 ? 'pathInfo' : 'targetInfo' ;
3332
3433 const okText = currentStep === 0 ? 'Add path' : currentStep === 1 ? 'Add target' : 'Create static route' ;
3534
36- useEffect ( ( ) => {
37- if ( ! isUninitialized && isError && error ?. message ) {
38- renderErrorModal ( { title : 'Unable to deploy static route' , error : error ?. message } ) ;
39- }
40- // eslint-disable-next-line react-hooks/exhaustive-deps
41- } , [ isError , isUninitialized ] ) ;
42-
4335 const onSubmitHandler = async ( ) => {
44- await form . validateFields ( ) ;
45- if ( step !== 'targetInfo' ) {
46- setCurrentStep ( currentStep + 1 ) ;
47- return ;
48- }
49- const { name, namespace, deployment, path, methods, upstream, redirect} = await form . getFieldsValue ( true ) ;
50-
51- const newStaticRouteDefinition : StaticRoute = {
52- apiVersion : 'gateway.kusk.io/v1alpha1' ,
53- kind : 'StaticRoute' ,
54- metadata : {
55- name,
56- } ,
57- spec : {
58- fleet : {
59- name : deployment . split ( ',' ) [ 1 ] ,
60- namespace : deployment . split ( ',' ) [ 0 ] ,
36+ try {
37+ await form . validateFields ( ) ;
38+ if ( step !== 'targetInfo' ) {
39+ setCurrentStep ( currentStep + 1 ) ;
40+ return ;
41+ }
42+ const { name, namespace, deployment, path, methods, upstream, redirect} = await form . getFieldsValue ( true ) ;
43+
44+ const newStaticRouteDefinition : StaticRoute = {
45+ apiVersion : 'gateway.kusk.io/v1alpha1' ,
46+ kind : 'StaticRoute' ,
47+ metadata : {
48+ name,
6149 } ,
62- hosts : [ ] ,
63- paths : {
64- [ path ] : methods . reduce ( ( acc : any , item : any ) => {
65- acc [ item ] = redirect ? { redirect} : { route : { upstream} } ;
66- return acc ;
67- } , { } ) ,
50+ spec : {
51+ fleet : {
52+ name : deployment . split ( ',' ) [ 1 ] ,
53+ namespace : deployment . split ( ',' ) [ 0 ] ,
54+ } ,
55+ hosts : [ ] ,
56+ paths : {
57+ [ path ] : methods . reduce ( ( acc : any , item : any ) => {
58+ acc [ item ] = redirect ? { redirect} : { route : { upstream} } ;
59+ return acc ;
60+ } , { } ) ,
61+ } ,
6862 } ,
69- } ,
70- } ;
71-
72- const result = await createStaticRoute ( {
73- body : {
74- name,
75- namespace,
76- envoyFleetNamespace : deployment . split ( ',' ) [ 0 ] ,
77- envoyFleetName : deployment . split ( ',' ) [ 1 ] ,
78- openapi : YAML . stringify ( newStaticRouteDefinition ) ,
79- } ,
80- } ) . unwrap ( ) ;
81- dispatch (
82- setAlert ( {
83- title : 'Static route deployed successfully' ,
84- description : `${ name } was deployed successfully in ${ namespace } namespace` ,
85- type : AlertEnum . Success ,
86- } )
87- ) ;
88- dispatch ( closeStaticRouteModal ( ) ) ;
89- navigate ( `${ AppRoutes . STATIC_ROUTE } /${ result . namespace } /${ result . name } ` ) ;
63+ } ;
64+
65+ const result = await createStaticRoute ( {
66+ body : {
67+ name,
68+ namespace,
69+ envoyFleetNamespace : deployment . split ( ',' ) [ 0 ] ,
70+ envoyFleetName : deployment . split ( ',' ) [ 1 ] ,
71+ openapi : YAML . stringify ( newStaticRouteDefinition ) ,
72+ } ,
73+ } ) . unwrap ( ) ;
74+ dispatch (
75+ setAlert ( {
76+ title : 'Static route deployed successfully' ,
77+ description : `${ name } was deployed successfully in ${ namespace } namespace` ,
78+ type : AlertEnum . Success ,
79+ } )
80+ ) ;
81+ dispatch ( closeStaticRouteModal ( ) ) ;
82+ navigate ( `${ AppRoutes . STATIC_ROUTE } /${ result . namespace } /${ result . name } ` ) ;
83+ } catch ( e : any ) {
84+ dispatch (
85+ setAlert ( {
86+ title : 'Unable to deploy Static Route' ,
87+ description : e ?. message ,
88+ type : AlertEnum . Error ,
89+ } )
90+ ) ;
91+ }
9092 } ;
9193
9294 const onBackHandler = ( ) => {
0 commit comments