@@ -14,84 +14,63 @@ import {
14
14
useApiMutation ,
15
15
useApiQueryClient ,
16
16
usePrefetchedApiQuery ,
17
- type RouterRouteUpdate ,
18
17
} from '@oxide/api'
19
18
20
- import { DescriptionField } from '~/components/form/fields/DescriptionField'
21
- import { ListboxField } from '~/components/form/fields/ListboxField'
22
- import { NameField } from '~/components/form/fields/NameField'
23
- import { TextField } from '~/components/form/fields/TextField'
24
19
import { SideModalForm } from '~/components/form/SideModalForm'
25
20
import {
26
- fields ,
21
+ RouteFormFields ,
27
22
routeFormMessage ,
28
- targetValueDescription ,
29
- } from '~/forms/vpc-router-route/shared '
23
+ type RouteFormValues ,
24
+ } from '~/forms/vpc-router-route-common '
30
25
import { getVpcRouterRouteSelector , useVpcRouterRouteSelector } from '~/hooks/use-params'
31
26
import { addToast } from '~/stores/toast'
32
- import { Message } from '~/ui/lib/Message'
33
27
import { pb } from '~/util/path-builder'
34
28
35
29
EditRouterRouteSideModalForm . loader = async ( { params } : LoaderFunctionArgs ) => {
36
- const { project , vpc , router , route } = getVpcRouterRouteSelector ( params )
30
+ const { route , ... routerSelector } = getVpcRouterRouteSelector ( params )
37
31
await apiQueryClient . prefetchQuery ( 'vpcRouterRouteView' , {
38
32
path : { route } ,
39
- query : { project , vpc , router } ,
33
+ query : routerSelector ,
40
34
} )
41
35
return null
42
36
}
43
37
44
38
export function EditRouterRouteSideModalForm ( ) {
45
39
const queryClient = useApiQueryClient ( )
46
- const routeSelector = useVpcRouterRouteSelector ( )
47
- const { project, vpc, router : routerName , route : routeName } = routeSelector
40
+ const { route : routeName , ...routerSelector } = useVpcRouterRouteSelector ( )
48
41
const navigate = useNavigate ( )
49
42
const { data : route } = usePrefetchedApiQuery ( 'vpcRouterRouteView' , {
50
43
path : { route : routeName } ,
51
- query : { project , vpc , router : routerName } ,
44
+ query : routerSelector ,
52
45
} )
53
46
54
- const defaultValues : RouterRouteUpdate = R . pick ( route , [
47
+ const defaultValues : RouteFormValues = R . pick ( route , [
55
48
'name' ,
56
49
'description' ,
57
50
'target' ,
58
51
'destination' ,
59
52
] )
60
-
61
- const onDismiss = ( ) => {
62
- navigate ( pb . vpcRouter ( { project, vpc, router : routerName } ) )
63
- }
53
+ const form = useForm ( { defaultValues } )
54
+ const isDisabled = route ?. kind === 'vpc_subnet'
64
55
65
56
const updateRouterRoute = useApiMutation ( 'vpcRouterRouteUpdate' , {
66
57
onSuccess ( ) {
67
58
queryClient . invalidateQueries ( 'vpcRouterRouteList' )
68
59
addToast ( { content : 'Your route has been updated' } )
69
- onDismiss ( )
60
+ navigate ( pb . vpcRouter ( routerSelector ) )
70
61
} ,
71
62
} )
72
63
73
- const form = useForm ( { defaultValues } )
74
- const targetType = form . watch ( 'target.type' )
75
-
76
- let isDisabled = false
77
- let disabledReason = ''
78
-
79
- // Can simplify this if there aren't other disabling reasons
80
- if ( route ?. kind === 'vpc_subnet' ) {
81
- isDisabled = true
82
- disabledReason = routeFormMessage . vpcSubnetNotModifiable
83
- }
84
-
85
64
return (
86
65
< SideModalForm
87
66
form = { form }
88
67
formType = "edit"
89
68
resourceName = "route"
90
- onDismiss = { onDismiss }
69
+ onDismiss = { ( ) => navigate ( pb . vpcRouter ( routerSelector ) ) }
91
70
onSubmit = { ( { name, description, destination, target } ) =>
92
71
updateRouterRoute . mutate ( {
93
- query : { project, vpc, router : routerName } ,
94
72
path : { route : routeName } ,
73
+ query : routerSelector ,
95
74
body : {
96
75
name,
97
76
description,
@@ -103,35 +82,9 @@ export function EditRouterRouteSideModalForm() {
103
82
}
104
83
loading = { updateRouterRoute . isPending }
105
84
submitError = { updateRouterRoute . error }
85
+ submitDisabled = { isDisabled ? routeFormMessage . vpcSubnetNotModifiable : undefined }
106
86
>
107
- { isDisabled && < Message variant = "info" content = { disabledReason } /> }
108
- < NameField name = "name" control = { form . control } disabled = { isDisabled } />
109
- < DescriptionField name = "description" control = { form . control } disabled = { isDisabled } />
110
- < ListboxField { ...fields . destType } control = { form . control } disabled = { isDisabled } />
111
- < TextField { ...fields . destValue } control = { form . control } disabled = { isDisabled } />
112
- < ListboxField
113
- { ...fields . targetType }
114
- control = { form . control }
115
- disabled = { isDisabled }
116
- onChange = { ( value ) => {
117
- // 'outbound' is only valid option when targetType is 'internet_gateway'
118
- if ( value === 'internet_gateway' ) {
119
- form . setValue ( 'target.value' , 'outbound' )
120
- }
121
- if ( value === 'drop' ) {
122
- form . setValue ( 'target.value' , '' )
123
- }
124
- } }
125
- />
126
- { targetType !== 'drop' && (
127
- < TextField
128
- { ...fields . targetValue }
129
- control = { form . control }
130
- // when targetType is 'internet_gateway', we set it to `outbound` and make it non-editable
131
- disabled = { isDisabled || targetType === 'internet_gateway' }
132
- description = { targetValueDescription ( targetType ) }
133
- />
134
- ) }
87
+ < RouteFormFields form = { form } isDisabled = { isDisabled } />
135
88
</ SideModalForm >
136
89
)
137
90
}
0 commit comments