fix: affected request indices computation post request reorder

This commit is contained in:
jamesgeorge007
2024-05-09 19:24:34 +05:30
parent bb57b2248c
commit 141652ffb6
3 changed files with 67 additions and 18 deletions

View File

@@ -525,13 +525,15 @@ const saveRequest = async () => {
return
}
const requestHandle = ref(saveContext.requestHandle)
const { requestHandle } = saveContext
if (!requestHandle.value) {
const requestHandleRef = requestHandle.get()
if (!requestHandleRef.value) {
return
}
if (requestHandle.value.type === "invalid") {
if (requestHandleRef.value.type === "invalid") {
showSaveRequestModal.value = true
return
}

View File

@@ -691,17 +691,11 @@ const restCollectionDispatchers = defineDispatchers({
// if the destination is null, we are moving to the end of the list
if (destinationRequestIndex === null) {
// move to the end of the list
// TODO: Verify if this can be safely removed
targetLocation.requests.push(
targetLocation.requests.splice(requestIndex, 1)[0]
)
resolveSaveContextOnRequestReorder({
lastIndex: requestIndex,
newIndex: targetLocation.requests.length,
folderPath: destinationCollectionPath,
})
return {
state: newState,
}
@@ -709,12 +703,6 @@ const restCollectionDispatchers = defineDispatchers({
reorderItems(targetLocation.requests, requestIndex, destinationRequestIndex)
resolveSaveContextOnRequestReorder({
lastIndex: requestIndex,
newIndex: destinationRequestIndex,
folderPath: destinationCollectionPath,
})
return {
state: newState,
}

View File

@@ -81,6 +81,8 @@ import {
isValidWorkspaceHandle,
} from "../helpers"
import { lazy } from "~/helpers/utils/lazy"
import { getAffectedIndexes } from "~/helpers/collection/affectedIndex"
import { request } from "http"
export class PersonalWorkspaceProviderService
extends Service
@@ -683,10 +685,67 @@ export class PersonalWorkspaceProviderService
return Promise.resolve(E.left("INVALID_REQUEST_HANDLE" as const))
}
const draggedRequestIndex = requestHandleRef.value.data.requestID
const { collectionID, requestID: draggedRequestID } =
requestHandleRef.value.data
const collectionRequestCount = getRequestsByPath(
restCollectionStore.value.state,
collectionID
).length
// Compute the affected request IDs
// Maps the previous request ID to the new one affected by the reorder
const affectedRequestIndices = getAffectedIndexes(
this.pathToLastIndex(draggedRequestID),
destinationRequestID === null
? collectionRequestCount - 1
: this.pathToLastIndex(destinationRequestID)
)
// Compile the handle indices within `issuedHandles` along with the ID to update it based on the affected request indices
// This is done in 2 steps since finding the corresponding handle and updating it straightaway would result in ambiguities
// Updating the request ID for a certain handle and attempting to find the handle corresponding to the same ID would pick the former handle
const affectedRequestHandleUpdateInfo = Array.from(
affectedRequestIndices.entries()
).map(([oldRequestIndexPos, newRequestIndexPos]) => {
const affectedRequestHandleIdx = this.issuedHandles.findIndex(
(handle) => {
if (handle.value.type === "invalid") {
return
}
if (!("requestID" in handle.value.data)) {
return
}
return (
handle.value.data.requestID ===
`${collectionID}/${oldRequestIndexPos}`
)
}
)
return { affectedRequestHandleIdx, newRequestIndexPos }
})
affectedRequestHandleUpdateInfo.forEach(
({ affectedRequestHandleIdx, newRequestIndexPos }) => {
const handle = this.issuedHandles[affectedRequestHandleIdx]
if (
!handle ||
handle.value.type === "invalid" ||
!("requestID" in handle.value.data)
) {
return
}
handle.value.data.requestID = `${collectionID}/${newRequestIndexPos}`
}
)
updateRESTRequestOrder(
this.pathToLastIndex(draggedRequestIndex),
this.pathToLastIndex(draggedRequestID),
destinationRequestID ? this.pathToLastIndex(destinationRequestID) : null,
destinationCollectionID
)