From 6032cbb17b7733af74b191d938cca554fe7c0580 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Tue, 14 May 2024 20:26:04 +0530 Subject: [PATCH] refactor: handle based updates for affected requests post request deletion --- .../components/new-collections/rest/index.vue | 41 ------- .../providers/personal.workspace.ts | 100 ++++++++++++------ 2 files changed, 70 insertions(+), 71 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 b22bcb02c..292d03e65 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,7 @@ 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 { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" @@ -1983,35 +1981,6 @@ const updateCollectionOrder = async ( return } - // Moving to the last position indicated by `destinationCollectionIndex` being `null` requires computing the index path of the new child collection being inserted - // let newDestinationCollectionIndex = 0 - // if (destinationCollectionIndex === null) { - // if (destinationCollectionParentIndex === null) { - // newDestinationCollectionIndex = restCollectionState.value.length - 1 - // } else { - // const destinationCollectionParent = navigateToFolderWithIndexPath( - // restCollectionState.value, - // destinationCollectionParentIndex.split("/").map((id) => parseInt(id)) - // ) - - // if (!destinationCollectionParent) { - // return - // } - - // newDestinationCollectionIndex = destinationCollectionParent.folders.length - // } - // } - - // resolveSaveContextOnCollectionReorder({ - // lastIndex: pathToLastIndex(draggedCollectionIndex), - // newIndex: pathToLastIndex( - // destinationCollectionIndex - // ? destinationCollectionIndex - // : newDestinationCollectionIndex.toString() - // ), - // folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"), - // }) - toast.success(`${t("collection.order_changed")}`) } @@ -2194,16 +2163,6 @@ 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() 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 f0e60d655..f428e88fb 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 @@ -63,16 +63,15 @@ import { WorkspaceRequest, } from "~/services/new-workspace/workspace" +import { getAffectedIndexes } from "~/helpers/collection/affectedIndex" import { getFoldersByPath, resolveSaveContextOnCollectionReorder, } from "~/helpers/collection/collection" -import { - getRequestsByPath, - resolveSaveContextOnRequestReorder, -} from "~/helpers/collection/request" +import { getRequestsByPath } from "~/helpers/collection/request" import { initializeDownloadFile } from "~/helpers/import-export/export" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" +import { lazy } from "~/helpers/utils/lazy" import IconUser from "~icons/lucide/user" import { NewWorkspaceService } from ".." import { @@ -80,8 +79,6 @@ import { isValidRequestHandle, isValidWorkspaceHandle, } from "../helpers" -import { lazy } from "~/helpers/utils/lazy" -import { getAffectedIndexes } from "~/helpers/collection/affectedIndex" export class PersonalWorkspaceProviderService extends Service @@ -496,38 +493,79 @@ export class PersonalWorkspaceProviderService } const { collectionID, requestID } = requestHandleRef.value.data - const requestIndex = parseInt(requestID.split("/").slice(-1)[0]) + const requestIndexPos = parseInt(requestID.split("/").slice(-1)[0]) const requestToRemove = navigateToFolderWithIndexPath( restCollectionStore.value.state, collectionID.split("/").map((id) => parseInt(id)) - )?.requests[requestIndex] + )?.requests[requestIndexPos] - removeRESTRequest(collectionID, requestIndex, requestToRemove?.id) + const parentCollectionSizeBeforeDeletion = getRequestsByPath( + this.restCollectionState.value.state, + collectionID + ).length - for (const [idx, handle] of this.issuedHandles.entries()) { - if (handle.value.type === "invalid") continue + // Requests appearing below the request being moved will be affected by the action + const affectedReqIndexRange = + parentCollectionSizeBeforeDeletion - 1 - requestIndexPos - if ("requestID" in handle.value.data) { - if (handle.value.data.requestID === requestID) { - // @ts-expect-error - We're deleting the data to invalidate the handle - delete this.issuedHandles[idx].value.data + const affectedRequestIDs = Array.from({ + length: affectedReqIndexRange, + }).map((_, idx) => { + // Affected request indices will start from the next position of the dragged request, hence adding `1` + const resolvedRequestIndexPos = requestIndexPos + idx + 1 + return `${collectionID}/${resolvedRequestIndexPos}` + }) - this.issuedHandles[idx].value.type = "invalid" + removeRESTRequest(collectionID, requestIndexPos, requestToRemove?.id) - // @ts-expect-error - Setting the handle invalidation reason - this.issuedHandles[idx].value.reason = "REQUEST_INVALIDATED" - } + const deletedRequestHandle = this.issuedHandles.find((handle) => { + if ( + handle.value.type === "invalid" || + !("requestID" in handle.value.data) + ) { + return } + + return handle.value.data.requestID === requestID + }) + + if (deletedRequestHandle) { + deletedRequestHandle.value.type = "invalid" + + // @ts-expect-error - Setting the handle invalidation reason + deletedRequestHandle.value.reason = "REQUEST_INVALIDATED" } - // The same function is used to reorder requests since after removing, it's basically doing reorder - resolveSaveContextOnRequestReorder({ - lastIndex: requestIndex, - newIndex: -1, - folderPath: collectionID, - length: getRequestsByPath(restCollectionStore.value.state, collectionID) - .length, + affectedRequestIDs.forEach((requestID) => { + const handle = this.issuedHandles.find((handle) => { + if (handle.value.type === "invalid") { + return + } + + if (!("requestID" in handle.value.data)) { + return + } + + return handle.value.data.requestID === requestID + }) + + if ( + !handle || + handle.value.type === "invalid" || + !("requestID" in handle.value.data) + ) { + return + } + + // Decrement the index pos in affected requests due to move + const affectedRequestIndexPos = Number( + handle.value.data.requestID.split("/").slice(-1)[0] + ) + + handle.value.data.requestID = `${collectionID}/${ + affectedRequestIndexPos - 1 + }` }) return Promise.resolve(E.right(undefined)) @@ -913,7 +951,7 @@ export class PersonalWorkspaceProviderService const { collectionID, requestID } = handle.value.data - const reqIndexPos = requestID.slice(-1)[0] + const affectedRequestIndexPos = requestID.slice(-1)[0] if (requestID.startsWith(draggedCollectionID)) { const newCollectionID = collectionID.replace( @@ -922,7 +960,7 @@ export class PersonalWorkspaceProviderService ) handle.value.data.collectionID = newCollectionID - handle.value.data.requestID = `${newCollectionID}/${reqIndexPos}` + handle.value.data.requestID = `${newCollectionID}/${affectedRequestIndexPos}` } }) @@ -1161,11 +1199,13 @@ export class PersonalWorkspaceProviderService } // Decrement the index pos in affected requests due to move - const reqIndexPos = Number( + const affectedRequestIndexPos = Number( handle.value.data.requestID.split("/").slice(-1)[0] ) - handle.value.data.requestID = `${sourceCollectionID}/${reqIndexPos - 1}` + handle.value.data.requestID = `${sourceCollectionID}/${ + affectedRequestIndexPos - 1 + }` }) return Promise.resolve(E.right(undefined))