|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { useForm } from 'react-hook-form' |
| 9 | +import { useNavigate, type LoaderFunctionArgs } from 'react-router' |
| 10 | + |
| 11 | +import { |
| 12 | + api, |
| 13 | + getListQFn, |
| 14 | + q, |
| 15 | + queryClient, |
| 16 | + useApiMutation, |
| 17 | + usePrefetchedQuery, |
| 18 | +} from '@oxide/api' |
| 19 | + |
| 20 | +import { DescriptionField } from '~/components/form/fields/DescriptionField' |
| 21 | +import { NameField } from '~/components/form/fields/NameField' |
| 22 | +import { SideModalForm } from '~/components/form/SideModalForm' |
| 23 | +import { HL } from '~/components/HL' |
| 24 | +import { titleCrumb } from '~/hooks/use-crumbs' |
| 25 | +import { getExternalSubnetSelector, useExternalSubnetSelector } from '~/hooks/use-params' |
| 26 | +import { addToast } from '~/stores/toast' |
| 27 | +import { EmptyCell } from '~/table/cells/EmptyCell' |
| 28 | +import { InstanceLinkCell } from '~/table/cells/InstanceLinkCell' |
| 29 | +import { PropertiesTable } from '~/ui/lib/PropertiesTable' |
| 30 | +import { ALL_ISH } from '~/util/consts' |
| 31 | +import { pb } from '~/util/path-builder' |
| 32 | + |
| 33 | +const externalSubnetView = ({ |
| 34 | + project, |
| 35 | + externalSubnet, |
| 36 | +}: { |
| 37 | + project: string |
| 38 | + externalSubnet: string |
| 39 | +}) => |
| 40 | + q(api.externalSubnetView, { |
| 41 | + path: { externalSubnet }, |
| 42 | + query: { project }, |
| 43 | + }) |
| 44 | + |
| 45 | +const instanceList = (project: string) => |
| 46 | + getListQFn(api.instanceList, { query: { project, limit: ALL_ISH } }) |
| 47 | + |
| 48 | +export async function clientLoader({ params }: LoaderFunctionArgs) { |
| 49 | + const selector = getExternalSubnetSelector(params) |
| 50 | + await Promise.all([ |
| 51 | + queryClient.fetchQuery(externalSubnetView(selector)), |
| 52 | + queryClient.fetchQuery(instanceList(selector.project).optionsFn()), |
| 53 | + ]) |
| 54 | + return null |
| 55 | +} |
| 56 | + |
| 57 | +export const handle = titleCrumb('Edit External Subnet') |
| 58 | + |
| 59 | +export default function EditExternalSubnetSideModalForm() { |
| 60 | + const navigate = useNavigate() |
| 61 | + |
| 62 | + const subnetSelector = useExternalSubnetSelector() |
| 63 | + const onDismiss = () => navigate(pb.externalSubnets({ project: subnetSelector.project })) |
| 64 | + |
| 65 | + const { data: subnet } = usePrefetchedQuery(externalSubnetView(subnetSelector)) |
| 66 | + const { data: instances } = usePrefetchedQuery( |
| 67 | + instanceList(subnetSelector.project).optionsFn() |
| 68 | + ) |
| 69 | + const instanceName = instances.items.find((i) => i.id === subnet.instanceId)?.name |
| 70 | + |
| 71 | + const editExternalSubnet = useApiMutation(api.externalSubnetUpdate, { |
| 72 | + onSuccess(updated) { |
| 73 | + queryClient.invalidateEndpoint('externalSubnetList') |
| 74 | + // prettier-ignore |
| 75 | + addToast(<>External subnet <HL>{updated.name}</HL> updated</>) |
| 76 | + onDismiss() |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + const form = useForm({ defaultValues: subnet }) |
| 81 | + return ( |
| 82 | + <SideModalForm |
| 83 | + form={form} |
| 84 | + formType="edit" |
| 85 | + resourceName="external subnet" |
| 86 | + onDismiss={onDismiss} |
| 87 | + onSubmit={({ name, description }) => { |
| 88 | + editExternalSubnet.mutate({ |
| 89 | + path: { externalSubnet: subnetSelector.externalSubnet }, |
| 90 | + query: { project: subnetSelector.project }, |
| 91 | + body: { name, description }, |
| 92 | + }) |
| 93 | + }} |
| 94 | + loading={editExternalSubnet.isPending} |
| 95 | + submitError={editExternalSubnet.error} |
| 96 | + > |
| 97 | + <PropertiesTable> |
| 98 | + <PropertiesTable.IdRow id={subnet.id} /> |
| 99 | + <PropertiesTable.DateRow label="Created" date={subnet.timeCreated} /> |
| 100 | + <PropertiesTable.DateRow label="Updated" date={subnet.timeModified} /> |
| 101 | + <PropertiesTable.Row label="Subnet">{subnet.subnet}</PropertiesTable.Row> |
| 102 | + <PropertiesTable.Row label="Instance"> |
| 103 | + {instanceName ? ( |
| 104 | + <InstanceLinkCell instanceId={subnet.instanceId} /> |
| 105 | + ) : ( |
| 106 | + <EmptyCell /> |
| 107 | + )} |
| 108 | + </PropertiesTable.Row> |
| 109 | + </PropertiesTable> |
| 110 | + <NameField name="name" control={form.control} /> |
| 111 | + <DescriptionField name="description" control={form.control} /> |
| 112 | + </SideModalForm> |
| 113 | + ) |
| 114 | +} |
0 commit comments