From 01573cc51c2c53cf9e678f90551e5d8223e9e0b7 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 29 Feb 2024 22:25:59 +0530 Subject: [PATCH] chore: keep existing implementation for save context resolution --- .../components/new-collections/rest/index.vue | 104 +++++++++--------- .../src/helpers/collection/collection.ts | 72 ++++++------ 2 files changed, 87 insertions(+), 89 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 24b4c45a4..a92e4d0f6 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -497,9 +497,14 @@ import { WorkspaceRESTCollectionTreeAdapter } from "~/helpers/adapters/Workspace import { TeamCollection } from "~/helpers/backend/graphql" import { getFoldersByPath, + resolveSaveContextOnCollectionReorder, updateInheritedPropertiesForAffectedRequests, + updateSaveContextForAffectedRequests, } from "~/helpers/collection/collection" -import { getRequestsByPath } from "~/helpers/collection/request" +import { + getRequestsByPath, + resolveSaveContextOnRequestReorder, +} from "~/helpers/collection/request" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" import { Picked } from "~/helpers/types/HoppPicked" import { @@ -1780,6 +1785,17 @@ const dropRequest = async (payload: { } } + // When it's drop it's basically getting deleted from last folder. reordering last folder accordingly + resolveSaveContextOnRequestReorder({ + lastIndex: pathToLastIndex(requestIndex), + newIndex: -1, // being deleted from last folder + folderPath: parentCollectionIndexPath, + length: getRequestsByPath( + restCollectionState.value, + parentCollectionIndexPath + ).length, + }) + toast.success(`${t("request.moved")}`) draggingToRoot.value = false } @@ -1808,21 +1824,22 @@ const dropCollection = async (payload: { return } - //check if the collection is being moved to its own parent + // Check if the collection is being moved to its own parent if ( isMoveToSameLocation(draggedCollectionIndex, destinationCollectionIndex) ) { return } - const draggedParentCollection = draggedCollectionIndex + const draggedParentCollectionIndex = draggedCollectionIndex .split("/") .slice(0, -1) - .join("/") // remove last folder to get parent folder + .join("/") // Remove the last child-collection index to get the parent collection index const totalChildCollectionsInDestinationCollection = getFoldersByPath(restCollectionState.value, destinationCollectionIndex) - .length - (draggedParentCollection === destinationCollectionIndex ? 1 : 0) + .length - + (draggedParentCollectionIndex === destinationCollectionIndex ? 1 : 0) const draggedCollectionHandleResult = await workspaceService.getCollectionHandle( @@ -1879,6 +1896,19 @@ const dropCollection = async (payload: { : resolvedDestinationCollectionIndex } + resolveSaveContextOnCollectionReorder( + { + lastIndex: pathToLastIndex(draggedCollectionIndex), + newIndex: -1, + folderPath: draggedParentCollectionIndex, + length: getFoldersByPath( + restCollectionState.value, + draggedParentCollectionIndex + ).length, + }, + "drop" + ) + updateSaveContextForAffectedRequests( draggedCollectionIndex, `${resolvedDestinationCollectionIndex}/${totalChildCollectionsInDestinationCollection}` @@ -2075,20 +2105,15 @@ const updateCollectionOrder = async ( } } - // resolveSaveContextOnCollectionReorder({ - // lastIndex: pathToLastIndex(draggedCollectionIndex), - // newIndex: pathToLastIndex( - // destinationCollectionIndex - // ? destinationCollectionIndex - // : newDestinationCollectionIndex.toString() - // ), - // folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"), - // }) - - updateSaveContextForAffectedRequests( - draggedCollectionIndex, - destinationCollectionIndex ?? newDestinationCollectionIndex.toString() - ) + resolveSaveContextOnCollectionReorder({ + lastIndex: pathToLastIndex(draggedCollectionIndex), + newIndex: pathToLastIndex( + destinationCollectionIndex + ? destinationCollectionIndex + : newDestinationCollectionIndex.toString() + ), + folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"), + }) toast.success(`${t("collection.order_changed")}`) } @@ -2261,6 +2286,16 @@ const pathToIndex = (path: string) => { return pathArr } +/** + * Used to get the index of the request from the path + * @param path The path of the request + * @returns The index of the request + */ +const pathToLastIndex = (path: string) => { + const pathArr = path.split("/") + return parseInt(pathArr[pathArr.length - 1]) +} + const resolveConfirmModal = (title: string | null) => { if (title === `${t("confirm.remove_collection")}`) { onRemoveRootCollection() @@ -2284,35 +2319,4 @@ const resetSelectedData = () => { editingRequestName.value = "" editingRequestIndexPath.value = "" } - -const updateSaveContextForAffectedRequests = ( - draggedCollectionIndex: string, - destinationCollectionIndex: string -) => { - const activeTabs = tabs.getActiveTabs() - - for (const tab of activeTabs.value) { - if ( - tab.document.saveContext?.originLocation === "workspace-user-collection" - ) { - const { requestID } = tab.document.saveContext - - const collectionID = requestID.split("/").slice(0, -1).join("/") - const requestIndex = requestID.split("/").slice(-1)[0] - - if (collectionID.startsWith(draggedCollectionIndex)) { - const newCollectionID = collectionID.replace( - draggedCollectionIndex, - destinationCollectionIndex - ) - const newRequestID = `${newCollectionID}/${requestIndex}` - - tab.document.saveContext = { - ...tab.document.saveContext, - requestID: newRequestID, - } - } - } - } -} diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index 661e90fd0..ee6eed346 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -117,57 +117,51 @@ export function resolveSaveContextOnCollectionReorder( */ export function updateSaveContextForAffectedRequests( - oldFolderPath: string, - newFolderPath: string + draggedCollectionIndex: string, + destinationCollectionIndex: string ) { const tabService = getService(RESTTabService) - const tabs = tabService.getTabsRefTo((tab) => { + + const activeTabs = tabService.getActiveTabs() + + for (const tab of activeTabs.value) { if (tab.document.saveContext?.originLocation === "user-collection") { - return tab.document.saveContext.folderPath.startsWith(oldFolderPath) - } + const { folderPath } = tab.document.saveContext - if ( - tab.document.saveContext?.originLocation !== "workspace-user-collection" - ) { - return false - } + if (folderPath.startsWith(draggedCollectionIndex)) { + const newFolderPath = folderPath.replace( + draggedCollectionIndex, + destinationCollectionIndex + ) - const collectionID = tab.document.saveContext.requestID - .split("/") - .slice(0, -1) - .join("/") - - return collectionID.startsWith(oldFolderPath) - }) - - for (const tab of tabs) { - if (tab.value.document.saveContext?.originLocation === "user-collection") { - tab.value.document.saveContext = { - ...tab.value.document.saveContext, - folderPath: tab.value.document.saveContext.folderPath.replace( - oldFolderPath, - newFolderPath - ), + tab.document.saveContext = { + ...tab.document.saveContext, + folderPath: newFolderPath, + } } + + return } if ( - tab.value.document.saveContext?.originLocation === - "workspace-user-collection" + tab.document.saveContext?.originLocation === "workspace-user-collection" ) { - const collectionID = tab.value.document.saveContext.requestID - .split("/") - .slice(0, -1) - .join("/") + const { requestID } = tab.document.saveContext - const newCollectionID = collectionID.replace(oldFolderPath, newFolderPath) - const newRequestID = `${newCollectionID}/${ - tab.value.document.saveContext.requestID.split("/").slice(-1)[0] - }` + const collectionID = requestID.split("/").slice(0, -1).join("/") + const requestIndex = requestID.split("/").slice(-1)[0] - tab.value.document.saveContext = { - ...tab.value.document.saveContext, - requestID: newRequestID, + if (collectionID.startsWith(draggedCollectionIndex)) { + const newCollectionID = collectionID.replace( + draggedCollectionIndex, + destinationCollectionIndex + ) + const newRequestID = `${newCollectionID}/${requestIndex}` + + tab.document.saveContext = { + ...tab.document.saveContext, + requestID: newRequestID, + } } } }