-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Choose custom router on subnet create/edit forms (#2393)
* Add combobox to subnet create and edit forms --------- Co-authored-by: David Crespo <[email protected]>
- Loading branch information
1 parent
1bb9270
commit 8028f9a
Showing
8 changed files
with
239 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* Copyright Oxide Computer Company | ||
*/ | ||
|
||
import { useMemo } from 'react' | ||
|
||
import { useApiQuery } from '~/api' | ||
import { useVpcSelector } from '~/hooks' | ||
|
||
/** | ||
* Special value indicating no router. Must use helper functions to convert | ||
* `undefined` to this when populating form, and this back to `undefined` in | ||
* onSubmit. | ||
*/ | ||
const NO_ROUTER = '||no router||' | ||
|
||
/** Convert form value to value for PUT body */ | ||
export function customRouterFormToData(value: string): string | undefined { | ||
return value === NO_ROUTER ? undefined : value | ||
} | ||
|
||
/** Convert value from response body to form value */ | ||
export function customRouterDataToForm(value: string | undefined): string { | ||
return value || NO_ROUTER | ||
} | ||
|
||
export const useCustomRouterItems = () => { | ||
const vpcSelector = useVpcSelector() | ||
const { data, isLoading } = useApiQuery('vpcRouterList', { query: vpcSelector }) | ||
|
||
const routerItems = useMemo(() => { | ||
const items = (data?.items || []) | ||
.filter((item) => item.kind === 'custom') | ||
.map((router) => ({ value: router.id, label: router.name })) | ||
|
||
return [{ value: NO_ROUTER, label: 'None' }, ...items] | ||
}, [data]) | ||
|
||
return { isLoading, items: routerItems } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* Copyright Oxide Computer Company | ||
*/ | ||
|
||
import { useApiQuery } from '~/api' | ||
import { useVpcSelector } from '~/hooks' | ||
import { Badge } from '~/ui/lib/Badge' | ||
import { pb } from '~/util/path-builder' | ||
|
||
import { EmptyCell, SkeletonCell } from './EmptyCell' | ||
import { LinkCell } from './LinkCell' | ||
|
||
export const RouterLinkCell = ({ value }: { value?: string }) => { | ||
const { project, vpc } = useVpcSelector() | ||
const { data: router, isError } = useApiQuery( | ||
'vpcRouterView', | ||
{ | ||
path: { router: value! }, | ||
query: { project, vpc }, | ||
}, | ||
{ enabled: !!value } | ||
) | ||
if (!value) return <EmptyCell /> | ||
// probably not possible but let’s be safe | ||
if (isError) return <Badge color="neutral">Deleted</Badge> | ||
if (!router) return <SkeletonCell /> // loading | ||
return ( | ||
<LinkCell to={pb.vpcRouter({ project, vpc, router: router.name })}> | ||
{router.name} | ||
</LinkCell> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.