refactor: handle based updates for affected requests post request deletion
This commit is contained in:
@@ -497,9 +497,7 @@ import { WorkspaceRESTCollectionTreeAdapter } from "~/helpers/adapters/Workspace
|
|||||||
import { TeamCollection } from "~/helpers/backend/graphql"
|
import { TeamCollection } from "~/helpers/backend/graphql"
|
||||||
import {
|
import {
|
||||||
getFoldersByPath,
|
getFoldersByPath,
|
||||||
resolveSaveContextOnCollectionReorder,
|
|
||||||
updateInheritedPropertiesForAffectedRequests,
|
updateInheritedPropertiesForAffectedRequests,
|
||||||
updateSaveContextForAffectedRequests,
|
|
||||||
} from "~/helpers/collection/collection"
|
} from "~/helpers/collection/collection"
|
||||||
import { getRequestsByPath } from "~/helpers/collection/request"
|
import { getRequestsByPath } from "~/helpers/collection/request"
|
||||||
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
|
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
|
||||||
@@ -1983,35 +1981,6 @@ const updateCollectionOrder = async (
|
|||||||
return
|
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")}`)
|
toast.success(`${t("collection.order_changed")}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2194,16 +2163,6 @@ const pathToIndex = (path: string) => {
|
|||||||
return pathArr
|
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) => {
|
const resolveConfirmModal = (title: string | null) => {
|
||||||
if (title === `${t("confirm.remove_collection")}`) {
|
if (title === `${t("confirm.remove_collection")}`) {
|
||||||
onRemoveRootCollection()
|
onRemoveRootCollection()
|
||||||
|
|||||||
@@ -63,16 +63,15 @@ import {
|
|||||||
WorkspaceRequest,
|
WorkspaceRequest,
|
||||||
} from "~/services/new-workspace/workspace"
|
} from "~/services/new-workspace/workspace"
|
||||||
|
|
||||||
|
import { getAffectedIndexes } from "~/helpers/collection/affectedIndex"
|
||||||
import {
|
import {
|
||||||
getFoldersByPath,
|
getFoldersByPath,
|
||||||
resolveSaveContextOnCollectionReorder,
|
resolveSaveContextOnCollectionReorder,
|
||||||
} from "~/helpers/collection/collection"
|
} from "~/helpers/collection/collection"
|
||||||
import {
|
import { getRequestsByPath } from "~/helpers/collection/request"
|
||||||
getRequestsByPath,
|
|
||||||
resolveSaveContextOnRequestReorder,
|
|
||||||
} from "~/helpers/collection/request"
|
|
||||||
import { initializeDownloadFile } from "~/helpers/import-export/export"
|
import { initializeDownloadFile } from "~/helpers/import-export/export"
|
||||||
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
|
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
|
||||||
|
import { lazy } from "~/helpers/utils/lazy"
|
||||||
import IconUser from "~icons/lucide/user"
|
import IconUser from "~icons/lucide/user"
|
||||||
import { NewWorkspaceService } from ".."
|
import { NewWorkspaceService } from ".."
|
||||||
import {
|
import {
|
||||||
@@ -80,8 +79,6 @@ import {
|
|||||||
isValidRequestHandle,
|
isValidRequestHandle,
|
||||||
isValidWorkspaceHandle,
|
isValidWorkspaceHandle,
|
||||||
} from "../helpers"
|
} from "../helpers"
|
||||||
import { lazy } from "~/helpers/utils/lazy"
|
|
||||||
import { getAffectedIndexes } from "~/helpers/collection/affectedIndex"
|
|
||||||
|
|
||||||
export class PersonalWorkspaceProviderService
|
export class PersonalWorkspaceProviderService
|
||||||
extends Service
|
extends Service
|
||||||
@@ -496,38 +493,79 @@ export class PersonalWorkspaceProviderService
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { collectionID, requestID } = requestHandleRef.value.data
|
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(
|
const requestToRemove = navigateToFolderWithIndexPath(
|
||||||
restCollectionStore.value.state,
|
restCollectionStore.value.state,
|
||||||
collectionID.split("/").map((id) => parseInt(id))
|
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()) {
|
// Requests appearing below the request being moved will be affected by the action
|
||||||
if (handle.value.type === "invalid") continue
|
const affectedReqIndexRange =
|
||||||
|
parentCollectionSizeBeforeDeletion - 1 - requestIndexPos
|
||||||
|
|
||||||
if ("requestID" in handle.value.data) {
|
const affectedRequestIDs = Array.from({
|
||||||
if (handle.value.data.requestID === requestID) {
|
length: affectedReqIndexRange,
|
||||||
// @ts-expect-error - We're deleting the data to invalidate the handle
|
}).map((_, idx) => {
|
||||||
delete this.issuedHandles[idx].value.data
|
// 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
|
const deletedRequestHandle = this.issuedHandles.find((handle) => {
|
||||||
this.issuedHandles[idx].value.reason = "REQUEST_INVALIDATED"
|
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
|
affectedRequestIDs.forEach((requestID) => {
|
||||||
resolveSaveContextOnRequestReorder({
|
const handle = this.issuedHandles.find((handle) => {
|
||||||
lastIndex: requestIndex,
|
if (handle.value.type === "invalid") {
|
||||||
newIndex: -1,
|
return
|
||||||
folderPath: collectionID,
|
}
|
||||||
length: getRequestsByPath(restCollectionStore.value.state, collectionID)
|
|
||||||
.length,
|
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))
|
return Promise.resolve(E.right(undefined))
|
||||||
@@ -913,7 +951,7 @@ export class PersonalWorkspaceProviderService
|
|||||||
|
|
||||||
const { collectionID, requestID } = handle.value.data
|
const { collectionID, requestID } = handle.value.data
|
||||||
|
|
||||||
const reqIndexPos = requestID.slice(-1)[0]
|
const affectedRequestIndexPos = requestID.slice(-1)[0]
|
||||||
|
|
||||||
if (requestID.startsWith(draggedCollectionID)) {
|
if (requestID.startsWith(draggedCollectionID)) {
|
||||||
const newCollectionID = collectionID.replace(
|
const newCollectionID = collectionID.replace(
|
||||||
@@ -922,7 +960,7 @@ export class PersonalWorkspaceProviderService
|
|||||||
)
|
)
|
||||||
|
|
||||||
handle.value.data.collectionID = newCollectionID
|
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
|
// 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.split("/").slice(-1)[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
handle.value.data.requestID = `${sourceCollectionID}/${reqIndexPos - 1}`
|
handle.value.data.requestID = `${sourceCollectionID}/${
|
||||||
|
affectedRequestIndexPos - 1
|
||||||
|
}`
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.resolve(E.right(undefined))
|
return Promise.resolve(E.right(undefined))
|
||||||
|
|||||||
Reference in New Issue
Block a user