Skip to content

Commit 60c2285

Browse files
authored
Error toast for disk detach and snapshot on instance disks list (#1804)
show toast on disk detach and snapshot error on instance disks list
1 parent d9cf1ef commit 60c2285

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

app/pages/project/instances/instance/tabs/StorageTab.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { Button, EmptyMessage, Storage24Icon } from '@oxide/ui'
2525
import { DiskStatusBadge } from 'app/components/StatusBadge'
2626
import AttachDiskSideModalForm from 'app/forms/disk-attach'
2727
import { CreateDiskSideModalForm } from 'app/forms/disk-create'
28-
import { getInstanceSelector, useInstanceSelector, useToast } from 'app/hooks'
28+
import { getInstanceSelector, useInstanceSelector } from 'app/hooks'
29+
import { addToast } from 'app/stores/toast'
2930

3031
import { fancifyStates } from './common'
3132

@@ -53,19 +54,37 @@ export function StorageTab() {
5354
const [showDiskCreate, setShowDiskCreate] = useState(false)
5455
const [showDiskAttach, setShowDiskAttach] = useState(false)
5556

56-
const addToast = useToast()
5757
const queryClient = useApiQueryClient()
5858
const { instance: instanceName, project } = useInstanceSelector()
5959
const instancePathQuery = useMemo(
6060
() => ({ path: { instance: instanceName }, query: { project } }),
6161
[instanceName, project]
6262
)
6363

64-
const detachDisk = useApiMutation('instanceDiskDetach', {})
64+
const detachDisk = useApiMutation('instanceDiskDetach', {
65+
onSuccess() {
66+
queryClient.invalidateQueries('instanceDiskList')
67+
addToast({ content: 'Disk detached' })
68+
},
69+
onError(err) {
70+
addToast({
71+
title: 'Failed to detach disk',
72+
content: err.message,
73+
variant: 'error',
74+
})
75+
},
76+
})
6577
const createSnapshot = useApiMutation('snapshotCreate', {
6678
onSuccess() {
6779
queryClient.invalidateQueries('snapshotList')
68-
addToast({ content: 'Snapshot successfully created' })
80+
addToast({ content: 'Snapshot created' })
81+
},
82+
onError(err) {
83+
addToast({
84+
title: 'Failed to create snapshot',
85+
content: err.message,
86+
variant: 'error',
87+
})
6988
},
7089
})
7190

@@ -97,18 +116,11 @@ export function StorageTab() {
97116
<>Instance must be in state {detachableStates} before disk can be detached</>
98117
),
99118
onActivate() {
100-
detachDisk.mutate(
101-
{ body: { disk: disk.name }, ...instancePathQuery },
102-
{
103-
onSuccess: () => {
104-
queryClient.invalidateQueries('instanceDiskList')
105-
},
106-
}
107-
)
119+
detachDisk.mutate({ body: { disk: disk.name }, ...instancePathQuery })
108120
},
109121
},
110122
],
111-
[detachDisk, instance, queryClient, instancePathQuery, createSnapshot, project]
123+
[detachDisk, instance, instancePathQuery, createSnapshot, project]
112124
)
113125

114126
const attachDisk = useApiMutation('instanceDiskAttach', {

app/test/e2e/instance/disks.e2e.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,27 @@ test('Attach disk', async ({ page }) => {
6666
await expectVisible(page, ['role=cell[name="disk-3"]'])
6767
})
6868

69+
test('Detach disk', async ({ page }) => {
70+
await page.goto('/projects/mock-project/instances/db1')
71+
72+
// Have to stop instance to edit disks
73+
await stopInstance(page)
74+
75+
const successMsg = page.getByText('Disk detached').nth(0)
76+
const row = page.getByRole('row', { name: 'disk-1' })
77+
await expect(row).toBeVisible()
78+
await expect(successMsg).toBeHidden()
79+
80+
await clickRowAction(page, 'disk-1', 'Detach')
81+
await expect(successMsg).toBeVisible()
82+
await expect(row).toBeHidden() // disk row goes away
83+
})
84+
6985
test('Snapshot disk', async ({ page }) => {
7086
await page.goto('/projects/mock-project/instances/db1')
7187

7288
// have to use nth with toasts because the text shows up in multiple spots
73-
const successMsg = page.getByText('Snapshot successfully created').nth(0)
89+
const successMsg = page.getByText('Snapshot created').nth(0)
7490
await expect(successMsg).toBeHidden()
7591

7692
await clickRowAction(page, 'disk-2', 'Snapshot')

libs/api-mocks/msw/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export const handlers = makeHandlers({
438438
instanceDiskDetach({ body, path, query: projectParams }) {
439439
const instance = lookup.instance({ ...path, ...projectParams })
440440
if (instance.run_state !== 'stopped') {
441-
throw 'Cannot detach disk to instance that is not stopped'
441+
throw 'Cannot detach disk from instance that is not stopped'
442442
}
443443
const disk = lookup.disk({ ...projectParams, disk: body.disk })
444444
disk.state = { state: 'detached' }

0 commit comments

Comments
 (0)