From 25b0818016f1eb2de3bd5ebd04f374d3b13504ca Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 15 May 2024 17:22:41 +0530 Subject: [PATCH] refactor: update inherited properties for affected requests flow updates --- .../components/new-collections/rest/index.vue | 39 ++++---- .../src/helpers/collection/collection.ts | 99 +++++++------------ .../providers/personal.workspace.ts | 9 +- 3 files changed, 61 insertions(+), 86 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 292d03e65..0b99a0416 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -1703,27 +1703,28 @@ const dropRequest = async (payload: { const { auth, headers } = cascadingAuthHeadersHandle.value.data - const possibleTab = tabs.getTabRefWithSaveContext({ - originLocation: "workspace-user-collection", - requestHandle, - }) + const requestHandleRef = requestHandle.get() - // If there is a tab attached to this request, update the document `inheritedProperties` - if (possibleTab) { - // @ts-expect-error - Updating the handle - possibleTab.value.document.saveContext.requestHandle.data = { - // @ts-expect-error - Updating the handle - ...possibleTab.value.document.saveContext.requestHandle.data, - collectionID: destinationCollectionIndex, - requestID: `${destinationCollectionIndex}/${( - getRequestsByPath(restCollectionState.value, destinationCollectionIndex) - .length - 1 - ).toString()}`, - } + if (requestHandleRef.value.type === "ok") { + const newRequestIndexPos = ( + getRequestsByPath(restCollectionState.value, destinationCollectionIndex) + .length - 1 + ).toString() - possibleTab.value.document.inheritedProperties = { - auth, - headers, + requestHandleRef.value.data.collectionID = destinationCollectionIndex + requestHandleRef.value.data.requestID = `${destinationCollectionIndex}/${newRequestIndexPos}` + + const possibleTab = tabs.getTabRefWithSaveContext({ + originLocation: "workspace-user-collection", + requestHandle: { get: () => requestHandleRef }, + }) + + // If there is a tab attached to this request, update the document `inheritedProperties` + if (possibleTab) { + possibleTab.value.document.inheritedProperties = { + auth, + headers, + } } } diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index 425defab3..8894419da 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -1,12 +1,13 @@ import { HoppCollection } from "@hoppscotch/data" import * as E from "fp-ts/Either" -import { ref } from "vue" import { getService } from "~/modules/dioc" import { GQLTabService } from "~/services/tab/graphql" import { RESTTabService } from "~/services/tab/rest" import { runGQLQuery } from "../backend/GQLClient" import { GetSingleRequestDocument } from "../backend/graphql" +import { HoppGQLSaveContext } from "../graphql/document" +import { HoppRESTSaveContext } from "../rest/document" import { HoppInheritedProperty } from "../types/HoppInheritedProperties" import { getAffectedIndexes } from "./affectedIndex" @@ -242,6 +243,33 @@ function removeDuplicatesAndKeepLast(arr: HoppInheritedProperty["headers"]) { return result } +function getSaveContextCollectionID( + saveContext: HoppRESTSaveContext | HoppGQLSaveContext | undefined +): string | undefined { + if (!saveContext) { + return + } + + const { originLocation } = saveContext + + if (originLocation === "team-collection") { + return saveContext.collectionID + } + + if (originLocation === "user-collection") { + return saveContext.folderPath + } + + const requestHandleRef = saveContext.requestHandle?.get() + + if (!requestHandleRef || requestHandleRef.value.type === "invalid") { + return + } + + // TODO: Remove `collectionID` and obtain it from `requestID` + return requestHandleRef.value.data.collectionID +} + export function updateInheritedPropertiesForAffectedRequests( path: string, inheritedProperties: HoppInheritedProperty, @@ -250,73 +278,20 @@ export function updateInheritedPropertiesForAffectedRequests( const tabService = type === "rest" ? getService(RESTTabService) : getService(GQLTabService) - const tabs = tabService.getTabsRefTo((tab) => { - if (tab.document.saveContext?.originLocation === "user-collection") { - return tab.document.saveContext.folderPath.startsWith(path) - } + const effectedTabs = tabService.getTabsRefTo((tab) => { + const saveContext = tab.document.saveContext - if (tab.document.saveContext?.originLocation === "team-collection") { - return Boolean(tab.document.saveContext.collectionID?.startsWith(path)) - } - - const collectionID = tab.document.saveContext?.requestID - ?.split("/") - .slice(0, -1) - .join("/") - - return Boolean(collectionID?.startsWith(path)) + const collectionID = getSaveContextCollectionID(saveContext) + return collectionID?.startsWith(path) ?? false }) - const tabsEffectedByAuth = tabs.filter((tab) => { - if (tab.value.document.saveContext?.originLocation === "user-collection") { - return ( - tab.value.document.saveContext.folderPath.startsWith(path) && - path === - folderPathCloseToSaveContext( - tab.value.document.inheritedProperties?.auth.parentID, - path, - tab.value.document.saveContext.folderPath - ) - ) - } - - if ( - tab.value.document.saveContext?.originLocation !== - "workspace-user-collection" - ) { - return false - } - - return ( - tab.value.document.saveContext.folderPath.startsWith(path) && - path === - folderPathCloseToSaveContext( - tab.value.document.inheritedProperties?.auth.parentID, - path, - tab.value.document.saveContext.folderPath - ) - ) - }) - - tabsEffectedByAuth.map((tab) => { + effectedTabs.map((tab) => { const inheritedParentID = tab.value.document.inheritedProperties?.auth.parentID - let contextPath = "" - - if (tab.value.document.saveContext?.originLocation === "user-collection") { - contextPath = tab.value.document.saveContext.folderPath - } else if ( - tab.value.document.saveContext?.originLocation === - "workspace-user-collection" - ) { - const requestHandle = ref(tab.value.document.saveContext.requestHandle) - if (requestHandle.value?.type === "ok") { - contextPath = requestHandle.value.data.collectionID - } - } else { - contextPath = tab.value.document.saveContext?.collectionID ?? "" - } + const contextPath = getSaveContextCollectionID( + tab.value.document.saveContext + ) const effectedPath = folderPathCloseToSaveContext( inheritedParentID, diff --git a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts index d76aedb0b..138c5e91f 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts @@ -1,7 +1,5 @@ import { HoppCollection, - HoppRESTAuth, - HoppRESTHeaders, HoppRESTRequest, makeCollection, } from "@hoppscotch/data" @@ -369,7 +367,8 @@ export class PersonalWorkspaceProviderService return } - if (handle.value.data.requestID.startsWith(removedCollectionID)) { + // TODO: Obtain collection ID from request ID instead + if (handle.value.data.collectionID.startsWith(removedCollectionID)) { handle.value = { type: "invalid", reason: "REQUEST_INVALIDATED", @@ -1623,8 +1622,8 @@ export class PersonalWorkspaceProviderService return { type: "ok", data: { auth, headers } } } - const parentFolderAuth: HoppRESTAuth = parentFolder.auth - const parentFolderHeaders: HoppRESTHeaders = parentFolder.headers + const { auth: parentFolderAuth, headers: parentFolderHeaders } = + parentFolder // check if the parent folder has authType 'inherit' and if it is the root folder if (