Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 14 additions & 38 deletions app/forms/disk-attach.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useParams } from 'react-router-dom'
import invariant from 'tiny-invariant'

import type { Disk, DiskIdentifier } from '@oxide/api'
import { useApiMutation, useApiQuery, useApiQueryClient } from '@oxide/api'
import type { DiskIdentifier, ErrorResult } from '@oxide/api'
import { useApiQuery } from '@oxide/api'

import { ListboxField, SideModalForm } from 'app/components/form'
import { useProjectSelector } from 'app/hooks'
Expand All @@ -11,35 +8,24 @@ const defaultValues = { name: '' }

type AttachDiskProps = {
/** If defined, this overrides the usual mutation */
onSubmit?: (diskAttach: DiskIdentifier) => void
onSubmit: (diskAttach: DiskIdentifier) => void
onDismiss: () => void
onSuccess?: (disk: Disk) => void
loading?: boolean
submitError?: ErrorResult | null
}

/**
* Can be used with either a `setState` or a real mutation as `onSubmit`, hence
* the optional `loading` and `submitError`
*/
export function AttachDiskSideModalForm({
onSubmit,
onSuccess,
onDismiss,
loading,
submitError = null,
}: AttachDiskProps) {
const queryClient = useApiQueryClient()
// instance name undefined when this form is called from DisksTableField on
// instance create, which passes in its own onSubmit, bypassing the attachDisk mutation
const { instanceName } = useParams()
const projectSelector = useProjectSelector()

// TODO: pass in this mutation from outside so we don't have to do the instanceName check
const attachDisk = useApiMutation('instanceDiskAttachV1', {
onSuccess(data) {
invariant(instanceName, 'instanceName is required')
queryClient.invalidateQueries('instanceDiskListV1', {
path: { instance: instanceName },
query: projectSelector,
})
onSuccess?.(data)
onDismiss()
},
})

// TODO: loading state? because this fires when the modal opens and not when
// they focus the combobox, it will almost always be done by the time they
// click in
Expand All @@ -54,19 +40,9 @@ export function AttachDiskSideModalForm({
id="form-disk-attach"
title="Attach Disk"
formOptions={{ defaultValues }}
onSubmit={
onSubmit ||
(({ name }) => {
invariant(instanceName, 'instanceName is required')
attachDisk.mutate({
path: { instance: instanceName },
query: projectSelector,
body: { disk: name },
})
})
}
loading={attachDisk.isLoading}
submitError={attachDisk.error}
onSubmit={onSubmit}
loading={loading}
submitError={submitError}
onDismiss={onDismiss}
>
{({ control }) => (
Expand Down
48 changes: 14 additions & 34 deletions app/forms/network-interface-create.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useMemo } from 'react'
import { useParams } from 'react-router-dom'
import invariant from 'tiny-invariant'

import type { NetworkInterfaceCreate } from '@oxide/api'
import { useApiMutation, useApiQuery, useApiQueryClient } from '@oxide/api'
import type { ErrorResult, NetworkInterfaceCreate } from '@oxide/api'
import { useApiQuery } from '@oxide/api'
import { Divider } from '@oxide/ui'

import {
Expand All @@ -26,28 +24,23 @@ const defaultValues: NetworkInterfaceCreate = {

type CreateNetworkInterfaceFormProps = {
onDismiss: () => void
onSubmit?: (values: NetworkInterfaceCreate) => void
onSubmit: (values: NetworkInterfaceCreate) => void
loading?: boolean
submitError?: ErrorResult | null
}

/**
* Can be used with either a `setState` or a real mutation as `onSubmit`, hence
* the optional `loading` and `submitError`
*/
export default function CreateNetworkInterfaceForm({
onSubmit,
onDismiss,
loading,
submitError = null,
}: CreateNetworkInterfaceFormProps) {
const queryClient = useApiQueryClient()
const { instanceName } = useParams()
const projectSelector = useProjectSelector()

// TODO: pass in this mutation from outside so we don't have to do the instanceName check
const createNetworkInterface = useApiMutation('instanceNetworkInterfaceCreateV1', {
onSuccess() {
invariant(instanceName, 'instanceName is required when posting a network interface')
queryClient.invalidateQueries('instanceNetworkInterfaceListV1', {
query: { instance: instanceName, ...projectSelector },
})
onDismiss()
},
})

const { data: vpcsData } = useApiQuery('vpcListV1', { query: projectSelector })
const vpcs = useMemo(() => vpcsData?.items || [], [vpcsData])

Expand All @@ -57,22 +50,9 @@ export default function CreateNetworkInterfaceForm({
title="Add network interface"
formOptions={{ defaultValues }}
onDismiss={onDismiss}
onSubmit={
onSubmit ||
((body) => {
invariant(
instanceName,
'instanceName is required when posting a network interface'
)

createNetworkInterface.mutate({
query: { ...projectSelector, instance: instanceName },
body,
})
})
}
loading={createNetworkInterface.isLoading}
submitError={createNetworkInterface.error}
onSubmit={onSubmit}
loading={loading}
submitError={submitError}
>
{({ control }) => (
<>
Expand Down
14 changes: 13 additions & 1 deletion app/pages/project/instances/instance/tabs/NetworkingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ export function NetworkingTab() {

const getQuery = ['instanceNetworkInterfaceListV1', { query: instanceSelector }] as const

const createNic = useApiMutation('instanceNetworkInterfaceCreateV1', {
onSuccess() {
queryClient.invalidateQueries('instanceNetworkInterfaceListV1', {
query: instanceSelector,
})
setCreateModalOpen(false)
},
})

const deleteNic = useApiMutation('instanceNetworkInterfaceDeleteV1', {
onSuccess() {
queryClient.invalidateQueries(...getQuery)
Expand Down Expand Up @@ -200,7 +209,10 @@ export function NetworkingTab() {
</div>

{createModalOpen && (
<CreateNetworkInterfaceForm onDismiss={() => setCreateModalOpen(false)} />
<CreateNetworkInterfaceForm
onDismiss={() => setCreateModalOpen(false)}
onSubmit={(body) => createNic.mutate({ query: instanceSelector, body })}
/>
)}
{editing && (
<EditNetworkInterfaceForm editing={editing} onDismiss={() => setEditing(null)} />
Expand Down
13 changes: 11 additions & 2 deletions app/pages/project/instances/instance/tabs/StorageTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ export function StorageTab() {

const attachDisk = useApiMutation('instanceDiskAttachV1', {
onSuccess() {
console.log('disk atttach success')
queryClient.invalidateQueries('instanceDiskListV1', instancePathQuery)
// cover all our bases. this is called by both modals
setShowDiskCreate(false)
setShowDiskAttach(false)
},
onError(err) {
addToast({
Expand Down Expand Up @@ -193,7 +195,14 @@ export function StorageTab() {
/>
)}
{showDiskAttach && (
<AttachDiskSideModalForm onDismiss={() => setShowDiskAttach(false)} />
<AttachDiskSideModalForm
onDismiss={() => setShowDiskAttach(false)}
onSubmit={({ name }) => {
attachDisk.mutate({ ...instancePathQuery, body: { disk: name } })
}}
loading={attachDisk.isLoading}
submitError={attachDisk.error}
/>
)}
</>
)
Expand Down
1 change: 0 additions & 1 deletion libs/api-mocks/msw/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export const lookup = {
return snapshot
},
vpc({ vpc: id, ...projectSelector }: PP.Vpc): Json<Api.Vpc> {
console.log({ id, ...projectSelector })
if (!id) throw notFoundErr

if (isUuid(id)) return lookupById(db.vpcs, id)
Expand Down
1 change: 0 additions & 1 deletion libs/api-mocks/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ export const handlers = makeHandlers({
state: 'attached',
instance: instance.id,
}
console.log(disk)
return disk
},
instanceDiskDetachV1({ body, path, query: projectParams }) {
Expand Down