|
| 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 { useNavigate, type LoaderFunctionArgs } from 'react-router-dom' |
| 9 | + |
| 10 | +import { |
| 11 | + apiQueryClient, |
| 12 | + useApiMutation, |
| 13 | + useApiQueryClient, |
| 14 | + usePrefetchedApiQuery, |
| 15 | +} from '@oxide/api' |
| 16 | + |
| 17 | +import { DescriptionField } from '~/components/form/fields/DescriptionField' |
| 18 | +import { NameField } from '~/components/form/fields/NameField' |
| 19 | +import { SideModalForm } from '~/components/form/SideModalForm' |
| 20 | +import { getFloatingIpSelector, useFloatingIpSelector, useForm, useToast } from 'app/hooks' |
| 21 | +import { pb } from 'app/util/path-builder' |
| 22 | + |
| 23 | +EditFloatingIpSideModalForm.loader = async ({ params }: LoaderFunctionArgs) => { |
| 24 | + const { floatingIp, project } = getFloatingIpSelector(params) |
| 25 | + await apiQueryClient.prefetchQuery('floatingIpView', { |
| 26 | + path: { floatingIp }, |
| 27 | + query: { project }, |
| 28 | + }) |
| 29 | + return null |
| 30 | +} |
| 31 | + |
| 32 | +export function EditFloatingIpSideModalForm() { |
| 33 | + const queryClient = useApiQueryClient() |
| 34 | + const addToast = useToast() |
| 35 | + const navigate = useNavigate() |
| 36 | + |
| 37 | + const floatingIpSelector = useFloatingIpSelector() |
| 38 | + |
| 39 | + const onDismiss = () => navigate(pb.floatingIps({ project: floatingIpSelector.project })) |
| 40 | + |
| 41 | + const { data: floatingIp } = usePrefetchedApiQuery('floatingIpView', { |
| 42 | + path: { floatingIp: floatingIpSelector.floatingIp }, |
| 43 | + query: { project: floatingIpSelector.project }, |
| 44 | + }) |
| 45 | + |
| 46 | + const editFloatingIp = useApiMutation('floatingIpUpdate', { |
| 47 | + onSuccess(_floatingIp) { |
| 48 | + queryClient.invalidateQueries('floatingIpList') |
| 49 | + addToast({ content: 'Your floating IP has been updated' }) |
| 50 | + onDismiss() |
| 51 | + }, |
| 52 | + }) |
| 53 | + |
| 54 | + const form = useForm({ defaultValues: floatingIp }) |
| 55 | + |
| 56 | + return ( |
| 57 | + <SideModalForm |
| 58 | + id="edit-floating-ip-form" |
| 59 | + form={form} |
| 60 | + title="Edit floating IP" |
| 61 | + onDismiss={onDismiss} |
| 62 | + onSubmit={({ name, description }) => { |
| 63 | + editFloatingIp.mutate({ |
| 64 | + path: { floatingIp: floatingIpSelector.floatingIp }, |
| 65 | + query: { project: floatingIpSelector.project }, |
| 66 | + body: { name, description }, |
| 67 | + }) |
| 68 | + }} |
| 69 | + loading={editFloatingIp.isPending} |
| 70 | + submitError={editFloatingIp.error} |
| 71 | + submitLabel="Save changes" |
| 72 | + > |
| 73 | + <NameField name="name" control={form.control} /> |
| 74 | + <DescriptionField name="description" control={form.control} /> |
| 75 | + </SideModalForm> |
| 76 | + ) |
| 77 | +} |
0 commit comments