From 3a176f6620d5e60a98c5710116f2f987b72bfc3b Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 14 Feb 2024 20:09:26 +0530 Subject: [PATCH] fix: invalidate requests opened under tabs on deleting parent collection --- .../components/new-collections/rest/index.vue | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue index 4e744cf83..8b9a74183 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -375,6 +375,48 @@ const onRemoveRootCollection = async () => { return } + const activeTabs = tabs.getActiveTabs() + + for (const tab of activeTabs.value) { + if ( + tab.document.saveContext?.originLocation === "workspace-user-collection" + ) { + const requestHandle = ref(tab.document.saveContext.requestHandle) + + if (requestHandle.value.type === "invalid") { + tab.document.saveContext = null + tab.document.isDirty = true + continue + } + + const requestID = requestHandle.value.data.requestID + + const parentCollectionIndexPath = requestID + .split("/") + .slice(0, -1) + .join("/") + const requestIndex = parseInt(requestID.split("/").slice(-1)[0]) + + const parentCollection = navigateToFolderWithIndexPath( + restCollectionState.value, + parentCollectionIndexPath.split("/").map((id) => parseInt(id)) + ) + + console.log(`Parent collection is `, parentCollection) + + if (!parentCollection) { + tab.document.saveContext = null + tab.document.isDirty = true + continue + } + + if (!parentCollection.requests[requestIndex]) { + tab.document.saveContext = null + tab.document.isDirty = true + } + } + } + toast.success(t("state.deleted")) displayConfirmModal(false) } @@ -639,6 +681,46 @@ const onRemoveChildCollection = async () => { return } + const activeTabs = tabs.getActiveTabs() + + for (const tab of activeTabs.value) { + if ( + tab.document.saveContext?.originLocation === "workspace-user-collection" + ) { + const requestHandle = ref(tab.document.saveContext.requestHandle) + + if (requestHandle.value.type === "invalid") { + tab.document.saveContext = null + tab.document.isDirty = true + continue + } + + const requestID = requestHandle.value.data.requestID + + const parentCollectionIndexPath = requestID + .split("/") + .slice(0, -1) + .join("/") + const requestIndex = parseInt(requestID.split("/").slice(-1)[0]) + + const parentCollection = navigateToFolderWithIndexPath( + restCollectionState.value, + parentCollectionIndexPath.split("/").map((id) => parseInt(id)) + ) + + if (!parentCollection) { + tab.document.saveContext = null + tab.document.isDirty = true + continue + } + + if (!parentCollection.requests[requestIndex]) { + tab.document.saveContext = null + tab.document.isDirty = true + } + } + } + toast.success(t("state.deleted")) displayConfirmModal(false) }