diff --git a/src/pages/instances/EditInstance.tsx b/src/pages/instances/EditInstance.tsx index e2017e6a3f..9433fb4161 100644 --- a/src/pages/instances/EditInstance.tsx +++ b/src/pages/instances/EditInstance.tsx @@ -54,7 +54,7 @@ import { useEventQueue } from "context/eventQueue"; import { hasDiskError, hasNetworkError } from "util/instanceValidation"; import FormFooterLayout from "components/forms/FormFooterLayout"; import { useToastNotification } from "context/toastNotificationProvider"; -import { instanceLinkFromOperation } from "util/instances"; +import { instanceLinkFromName } from "util/instances"; export interface InstanceEditDetailsFormValues { name: string; @@ -113,11 +113,11 @@ const EditInstance: FC = ({ instance }) => { instancePayload.etag = instance.etag; void updateInstance(instancePayload, project).then((operation) => { - const instanceLink = instanceLinkFromOperation({ - operation, + const instanceName = values.name; + const instanceLink = instanceLinkFromName({ + instanceName, project, }); - if (!instanceLink) return; eventQueue.set( operation.metadata.id, () => { diff --git a/src/pages/storage/forms/EditVolumeSnapshotForm.tsx b/src/pages/storage/forms/EditVolumeSnapshotForm.tsx index 3ee6d916df..18527d294c 100644 --- a/src/pages/storage/forms/EditVolumeSnapshotForm.tsx +++ b/src/pages/storage/forms/EditVolumeSnapshotForm.tsx @@ -40,46 +40,33 @@ const EditVolumeSnapshotForm: FC = ({ volume, snapshot, close }) => { close(); }; - const update = (expiresAt: string | null, newName?: string) => { - // NOTE: volume snapshot update api call is synchronous, so can't use events api - void updateVolumeSnapshot({ + const updateExpirationTime = async (expiresAt: string | null) => { + await updateVolumeSnapshot({ volume, - snapshot: { ...snapshot, name: newName || snapshot.name }, + snapshot, expiresAt, - }) - .then(() => { - notifyUpdateSuccess(newName || snapshot.name); - }) - .catch((error: Error) => { - notify.failure("Snapshot update failed", error); - formik.setSubmitting(false); - }); + }).catch((error: Error) => { + notify.failure("Snapshot update failed", error); + formik.setSubmitting(false); + }); }; - const rename = (newName: string, expiresAt?: string | null) => { - void renameVolumeSnapshot({ - volume, - snapshot, - newName, - }).then((operation) => - eventQueue.set( - operation.metadata.id, - () => { - if (expiresAt) { - update(expiresAt || null, newName); - } else { - notifyUpdateSuccess(newName); - } - }, - (msg) => { + const rename = (newName: string): Promise => { + return new Promise((resolve) => { + void renameVolumeSnapshot({ + volume, + snapshot, + newName, + }).then((operation) => + eventQueue.set(operation.metadata.id, resolve, (msg) => { toastNotify.failure( `Snapshot ${snapshot.name} rename failed`, new Error(msg), ); formik.setSubmitting(false); - }, - ), - ); + }), + ); + }); }; const [expiryDate, expiryTime] = !snapshot.expires_at @@ -100,24 +87,21 @@ const EditVolumeSnapshotForm: FC = ({ volume, snapshot, close }) => { controllerState, snapshot.name, ), - onSubmit: (values) => { + onSubmit: async (values) => { notify.clear(); - const newName = values.name; const expiresAt = values.expirationDate && values.expirationTime ? stringToIsoTime( getExpiresAt(values.expirationDate, values.expirationTime), ) : null; - const shouldRename = newName !== snapshot.name; - const shouldUpdate = expiresAt !== snapshot.expires_at; - if (shouldRename && shouldUpdate) { - rename(newName, expiresAt); - } else if (shouldRename) { - rename(newName); - } else { - update(expiresAt); + if (expiresAt !== snapshot.expires_at) { + await updateExpirationTime(expiresAt); + } + if (values.name !== snapshot.name) { + await rename(values.name); } + notifyUpdateSuccess(values.name); }, }); diff --git a/src/types/storage.d.ts b/src/types/storage.d.ts index 4dee3d6647..f868808ef6 100644 --- a/src/types/storage.d.ts +++ b/src/types/storage.d.ts @@ -77,6 +77,6 @@ export interface UploadState { export interface LxdVolumeSnapshot { name: string; created_at: string; - expires_at: string; + expires_at?: string; description?: string; }