From 30b6a675058fc0e55defcdea58cca7a3d5de51d5 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Mon, 12 Feb 2024 18:25:42 +0530 Subject: [PATCH] fix: prevent duplicate request creation and invalidate tabs with request deletion --- .../components/new-collections/rest/index.vue | 42 +++++++++---------- .../src/services/tab/rest.ts | 30 +++++++++++-- 2 files changed, 46 insertions(+), 26 deletions(-) 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 9178768c3..a17b5bdd6 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -128,9 +128,20 @@ import * as E from "fp-ts/lib/Either" import { useService } from "dioc/vue" import { markRaw, nextTick, ref } from "vue" +import { HoppCollection, HoppRESTAuth, HoppRESTRequest } from "@hoppscotch/data" +import { cloneDeep } from "lodash-es" import { useI18n } from "~/composables/i18n" +import { useReadonlyStream } from "~/composables/stream" import { useToast } from "~/composables/toast" import { WorkspaceRESTCollectionTreeAdapter } from "~/helpers/adapters/WorkspaceRESTCollectionTreeAdapter" +import { TeamCollection } from "~/helpers/backend/graphql" +import { updateInheritedPropertiesForAffectedRequests } from "~/helpers/collection/collection" +import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" +import { + navigateToFolderWithIndexPath, + restCollections$, + saveRESTRequestAs, +} from "~/newstore/collections" import { NewWorkspaceService } from "~/services/new-workspace" import { HandleRef } from "~/services/new-workspace/handle" import { Workspace } from "~/services/new-workspace/workspace" @@ -138,21 +149,6 @@ import { RESTTabService } from "~/services/tab/rest" import IconImport from "~icons/lucide/folder-down" import IconHelpCircle from "~icons/lucide/help-circle" import IconPlus from "~icons/lucide/plus" -import { - navigateToFolderWithIndexPath, - restCollections$, - saveRESTRequestAs, -} from "~/newstore/collections" -import { cloneDeep } from "lodash-es" -import { HoppCollection, HoppRESTAuth, HoppRESTRequest } from "@hoppscotch/data" -import { TeamCollection } from "~/helpers/backend/graphql" -import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" -import { useReadonlyStream } from "~/composables/stream" -import { updateInheritedPropertiesForAffectedRequests } from "~/helpers/collection/collection" -import { - resolveSaveContextOnRequestReorder, - getRequestsByPath, -} from "~/helpers/collection/request" const t = useI18n() const toast = useToast() @@ -312,8 +308,8 @@ const onRemoveRootCollection = async () => { displayConfirmModal(false) } -const addRequest = (requestPathIndex: string) => { - editingCollectionIndexPath.value = requestPathIndex +const addRequest = (parentCollectionIndexPath: string) => { + editingCollectionIndexPath.value = parentCollectionIndexPath displayModalAddRequest(true) } @@ -598,6 +594,11 @@ const onRemoveRequest = async () => { return } + const possibleTab = tabs.getTabRefWithSaveContext({ + originLocation: "workspace-user-collection", + requestHandle, + }) + const result = await workspaceService.removeRESTRequest(requestHandle) if (E.isLeft(result)) { @@ -605,11 +606,6 @@ const onRemoveRequest = async () => { return } - const possibleTab = tabs.getTabRefWithSaveContext({ - originLocation: "workspace-user-collection", - requestHandle, - }) - // If there is a tab attached to this request, dissociate its state and mark it dirty if (possibleTab) { possibleTab.value.document.saveContext = null @@ -715,7 +711,7 @@ const duplicateRequest = async (requestIndexPath: string) => { return } - const requestHandle = requestHandleResult.right + const requestHandle = cloneDeep(requestHandleResult.right) if (requestHandle.value.type === "invalid") { // COLLECTION_INVALIDATED | INVALID_REQUEST_HANDLE diff --git a/packages/hoppscotch-common/src/services/tab/rest.ts b/packages/hoppscotch-common/src/services/tab/rest.ts index 29323a730..cdcefa7b6 100644 --- a/packages/hoppscotch-common/src/services/tab/rest.ts +++ b/packages/hoppscotch-common/src/services/tab/rest.ts @@ -1,5 +1,5 @@ import { isEqual } from "lodash-es" -import { computed } from "vue" +import { computed, ref } from "vue" import { getDefaultRESTRequest } from "~/helpers/rest/default" import { HoppRESTDocument, HoppRESTSaveContext } from "~/helpers/rest/document" import { TabService } from "./tab" @@ -50,8 +50,32 @@ export class RESTTabService extends TabService { ) { return this.getTabRef(tab.id) } - } else if (isEqual(ctx, tab.document.saveContext)) { - return this.getTabRef(tab.id) + } else if (ctx?.originLocation === "user-collection") { + if (isEqual(ctx, tab.document.saveContext)) { + return this.getTabRef(tab.id) + } + } else if ( + ctx?.originLocation === "workspace-user-collection" && + tab.document.saveContext?.originLocation === "workspace-user-collection" + ) { + const tabDocumentRequestHandle = ref( + tab.document.saveContext?.requestHandle + ) + + if ( + ctx?.requestHandle.value?.type === "invalid" || + tabDocumentRequestHandle?.value.type === "invalid" + ) { + return + } + + if ( + isEqual( + ctx?.requestHandle.value.data.request.id, + tabDocumentRequestHandle.value.data.request.id + ) + ) + return this.getTabRef(tab.id) } }