Просмотр исходного кода

chore: keep existing implementation for save context resolution

jamesgeorge007 1 год назад
Родитель
Сommit
0ee7723659

+ 54 - 50
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,
-        }
-      }
-    }
-  }
-}
 </script>

+ 32 - 38
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) => {
-    if (tab.document.saveContext?.originLocation === "user-collection") {
-      return tab.document.saveContext.folderPath.startsWith(oldFolderPath)
-    }
 
-    if (
-      tab.document.saveContext?.originLocation !== "workspace-user-collection"
-    ) {
-      return false
-    }
+  const activeTabs = tabService.getActiveTabs()
 
-    const collectionID = tab.document.saveContext.requestID
-      .split("/")
-      .slice(0, -1)
-      .join("/")
+  for (const tab of activeTabs.value) {
+    if (tab.document.saveContext?.originLocation === "user-collection") {
+      const { folderPath } = tab.document.saveContext
 
-    return collectionID.startsWith(oldFolderPath)
-  })
+      if (folderPath.startsWith(draggedCollectionIndex)) {
+        const newFolderPath = folderPath.replace(
+          draggedCollectionIndex,
+          destinationCollectionIndex
+        )
 
-  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]
+
+      if (collectionID.startsWith(draggedCollectionIndex)) {
+        const newCollectionID = collectionID.replace(
+          draggedCollectionIndex,
+          destinationCollectionIndex
+        )
+        const newRequestID = `${newCollectionID}/${requestIndex}`
 
-      tab.value.document.saveContext = {
-        ...tab.value.document.saveContext,
-        requestID: newRequestID,
+        tab.document.saveContext = {
+          ...tab.document.saveContext,
+          requestID: newRequestID,
+        }
       }
     }
   }