|
| 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 | +import * as R from 'remeda' |
| 11 | + |
| 12 | +import { api, q, queryClient, useApiMutation, usePrefetchedQuery } from '@oxide/api' |
| 13 | + |
| 14 | +import { DescriptionField } from '~/components/form/fields/DescriptionField' |
| 15 | +import { NameField } from '~/components/form/fields/NameField' |
| 16 | +import { SideModalForm } from '~/components/form/SideModalForm' |
| 17 | +import { HL } from '~/components/HL' |
| 18 | +import { makeCrumb } from '~/hooks/use-crumbs' |
| 19 | +import { getSubnetPoolSelector, useSubnetPoolSelector } from '~/hooks/use-params' |
| 20 | +import { addToast } from '~/stores/toast' |
| 21 | +import { Message } from '~/ui/lib/Message' |
| 22 | +import { SideModalFormDocs } from '~/ui/lib/ModalLinks' |
| 23 | +import { docLinks } from '~/util/links' |
| 24 | +import { pb } from '~/util/path-builder' |
| 25 | +import type * as PP from '~/util/path-params' |
| 26 | + |
| 27 | +const subnetPoolView = ({ subnetPool }: PP.SubnetPool) => |
| 28 | + q(api.systemSubnetPoolView, { path: { pool: subnetPool } }) |
| 29 | + |
| 30 | +export async function clientLoader({ params }: LoaderFunctionArgs) { |
| 31 | + const selector = getSubnetPoolSelector(params) |
| 32 | + await queryClient.prefetchQuery(subnetPoolView(selector)) |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +export const handle = makeCrumb('Edit subnet pool') |
| 37 | + |
| 38 | +export default function EditSubnetPoolSideModalForm() { |
| 39 | + const navigate = useNavigate() |
| 40 | + const poolSelector = useSubnetPoolSelector() |
| 41 | + |
| 42 | + const { data: pool } = usePrefetchedQuery(subnetPoolView(poolSelector)) |
| 43 | + |
| 44 | + const form = useForm({ defaultValues: R.pick(pool, ['name', 'description']) }) |
| 45 | + |
| 46 | + const editPool = useApiMutation(api.systemSubnetPoolUpdate, { |
| 47 | + onSuccess(updatedPool) { |
| 48 | + queryClient.invalidateEndpoint('systemSubnetPoolList') |
| 49 | + navigate(pb.subnetPool({ subnetPool: updatedPool.name })) |
| 50 | + // prettier-ignore |
| 51 | + addToast(<>Subnet pool <HL>{updatedPool.name}</HL> updated</>) |
| 52 | + |
| 53 | + if (pool.name === updatedPool.name) { |
| 54 | + queryClient.invalidateEndpoint('systemSubnetPoolView') |
| 55 | + } |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + return ( |
| 60 | + <SideModalForm |
| 61 | + form={form} |
| 62 | + formType="edit" |
| 63 | + resourceName="subnet pool" |
| 64 | + onDismiss={() => navigate(pb.subnetPool({ subnetPool: poolSelector.subnetPool }))} |
| 65 | + onSubmit={({ name, description }) => { |
| 66 | + editPool.mutate({ |
| 67 | + path: { pool: poolSelector.subnetPool }, |
| 68 | + body: { name, description }, |
| 69 | + }) |
| 70 | + }} |
| 71 | + loading={editPool.isPending} |
| 72 | + submitError={editPool.error} |
| 73 | + > |
| 74 | + <Message |
| 75 | + variant="info" |
| 76 | + content="Users in linked silos will use subnet pool names and descriptions to help them choose a pool when allocating external subnets." |
| 77 | + /> |
| 78 | + <NameField name="name" control={form.control} /> |
| 79 | + <DescriptionField name="description" control={form.control} /> |
| 80 | + <SideModalFormDocs docs={[docLinks.subnetPools]} /> |
| 81 | + </SideModalForm> |
| 82 | + ) |
| 83 | +} |
0 commit comments