Skip to content

Commit

Permalink
refactor: handle based updates post collection move (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed May 9, 2024
1 parent e4d204b commit 2e3d9ff
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
Expand Up @@ -1569,10 +1569,10 @@ const dropToRoot = async ({ dataTransfer }: DragEvent) => {
restCollectionState.value.length - 1
).toString()
updateSaveContextForAffectedRequests(
draggedCollectionIndex,
destinationRootCollectionIndex
)
// updateSaveContextForAffectedRequests(
// draggedCollectionIndex,
// destinationRootCollectionIndex
// )
const destinationRootCollectionHandleResult =
await workspaceService.getCollectionHandle(
Expand Down Expand Up @@ -1808,43 +1808,43 @@ const dropCollection = async (payload: {
// Hence, we need to reduce the destination root collection index by 1
// This only applies to the case when the destination collection lies below the dragged collection
let resolvedDestinationCollectionIndex = destinationCollectionIndex
const draggedRootCollectionIndex = parseInt(
pathToIndex(draggedCollectionIndex)[0]
)
const destinationRootCollectionIndex = parseInt(
pathToIndex(destinationCollectionIndex)[0]
)
if (
isAlreadyInRoot(draggedCollectionIndex) &&
destinationRootCollectionIndex > draggedRootCollectionIndex
) {
resolvedDestinationCollectionIndex = `${
destinationRootCollectionIndex - 1
}/${pathToIndex(destinationCollectionIndex).slice(1).join("/")}`
resolvedDestinationCollectionIndex =
resolvedDestinationCollectionIndex.endsWith("/")
? resolvedDestinationCollectionIndex.slice(0, -1)
: resolvedDestinationCollectionIndex
}
resolveSaveContextOnCollectionReorder({
lastIndex: pathToLastIndex(draggedCollectionIndex),
newIndex: -1,
folderPath: draggedParentCollectionIndex,
length: getFoldersByPath(
restCollectionState.value,
draggedParentCollectionIndex
).length,
})
updateSaveContextForAffectedRequests(
draggedCollectionIndex,
`${resolvedDestinationCollectionIndex}/${totalChildCollectionsInDestinationCollection}`
)
// let resolvedDestinationCollectionIndex = destinationCollectionIndex
// const draggedRootCollectionIndex = parseInt(
// pathToIndex(draggedCollectionIndex)[0]
// )
// const destinationRootCollectionIndex = parseInt(
// pathToIndex(destinationCollectionIndex)[0]
// )
// if (
// isAlreadyInRoot(draggedCollectionIndex) &&
// destinationRootCollectionIndex > draggedRootCollectionIndex
// ) {
// resolvedDestinationCollectionIndex = `${
// destinationRootCollectionIndex - 1
// }/${pathToIndex(destinationCollectionIndex).slice(1).join("/")}`
// resolvedDestinationCollectionIndex =
// resolvedDestinationCollectionIndex.endsWith("/")
// ? resolvedDestinationCollectionIndex.slice(0, -1)
// : resolvedDestinationCollectionIndex
// }
// resolveSaveContextOnCollectionReorder({
// lastIndex: pathToLastIndex(draggedCollectionIndex),
// newIndex: -1,
// folderPath: draggedParentCollectionIndex,
// length: getFoldersByPath(
// restCollectionState.value,
// draggedParentCollectionIndex
// ).length,
// })
// updateSaveContextForAffectedRequests(
// draggedCollectionIndex,
// `${resolvedDestinationCollectionIndex}/${totalChildCollectionsInDestinationCollection}`
// )
const destinationCollectionHandleResult =
await workspaceService.getCollectionHandle(
Expand Down
1 change: 1 addition & 0 deletions packages/hoppscotch-common/src/helpers/rest/document.ts
Expand Up @@ -12,6 +12,7 @@ export type HoppRESTSaveContext =
* The origin source of the request
*/
// TODO: Make this `user-collection` after porting all usages
// Future TODO: Keep separate types for the IDs (specific to persistence) & `requestHandle` (only existing at runtime)
originLocation: "workspace-user-collection"
/**
* ID of the workspace
Expand Down
Expand Up @@ -666,6 +666,31 @@ export class PersonalWorkspaceProviderService
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}

const { collectionID: draggedCollectionID } = collectionHandleRef.value.data

const resolvedDestinationCollectionID =
destinationCollectionID === null
? this.restCollectionState.value.state.length.toString()
: destinationCollectionID

this.issuedHandles.forEach((handle) => {
if (handle.value.type === "invalid") {
return
}

if (!("requestID" in handle.value.data)) {
return
}

if (handle.value.data.requestID.startsWith(draggedCollectionID)) {
handle.value.data.collectionID = resolvedDestinationCollectionID
handle.value.data.requestID = handle.value.data.requestID.replace(
draggedCollectionID,
resolvedDestinationCollectionID
)
}
})

moveRESTFolder(
collectionHandleRef.value.data.collectionID,
destinationCollectionID
Expand Down

0 comments on commit 2e3d9ff

Please sign in to comment.