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 10, 2024
1 parent e4d204b commit 4926b7d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 54 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
15 changes: 2 additions & 13 deletions packages/hoppscotch-common/src/pages/index.vue
Expand Up @@ -126,8 +126,6 @@ import { InspectionService } from "~/services/inspection"
import { EnvironmentInspectorService } from "~/services/inspection/inspectors/environment.inspector"
import { HeaderInspectorService } from "~/services/inspection/inspectors/header.inspector"
import { ResponseInspectorService } from "~/services/inspection/inspectors/response.inspector"
import { HandleRef } from "~/services/new-workspace/handle"
import { WorkspaceRequest } from "~/services/new-workspace/workspace"
import { HoppTab, PersistableTabState } from "~/services/tab"
import { RESTTabService } from "~/services/tab/rest"
Expand Down Expand Up @@ -297,12 +295,7 @@ const onResolveConfirmSaveTab = () => {
if (
!saveContext ||
(saveContext.originLocation === "workspace-user-collection" &&
// `requestHandle` gets unwrapped here
(
saveContext.requestHandle as
| HandleRef<WorkspaceRequest>["value"]
| undefined
)?.type === "invalid")
saveContext.requestHandle?.get().value.type === "invalid")
) {
return (savingRequest.value = true)
}
Expand Down Expand Up @@ -338,11 +331,7 @@ const getTabDirtyStatus = (tab: HoppTab<HoppRESTDocument>) => {
return (
tab.document.saveContext?.originLocation === "workspace-user-collection" &&
(
tab.document.saveContext.requestHandle as
| HandleRef<WorkspaceRequest>["value"]
| undefined
)?.type === "invalid"
tab.document.saveContext.requestHandle?.get().value.type === "invalid"
)
}
Expand Down
Expand Up @@ -666,6 +666,53 @@ export class PersonalWorkspaceProviderService
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}

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

let resolvedDestinationCollectionID = destinationCollectionID

if (destinationCollectionID === null) {
// destinationCollectionID` being `null` indicates moving to root
// New ID will be the length of the root nodes at this point (before the store update)
resolvedDestinationCollectionID =
this.restCollectionState.value.state.length.toString()
} else {
// Move to an inner-level collection
// The count of child collections within the destination collection will be the new index position
// Appended to the `resolvedDestinationCollectionID`
const destinationCollectionIndexPos = getFoldersByPath(
this.restCollectionState.value.state,
destinationCollectionID
).length.toString()

resolvedDestinationCollectionID = `${destinationCollectionID}/${destinationCollectionIndexPos}`
}

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

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

const reqIndexPos = handle.value.data.requestID.slice(-1)[0]

console.error(`Dragged request collection ID: ${draggedCollectionID}`)
console.error(
`Resolved destination collection ID: ${resolvedDestinationCollectionID}`
)

console.error(`Previous handle value `, handle.value.data)

if (handle.value.data.requestID.startsWith(draggedCollectionID)) {
handle.value.data.collectionID = resolvedDestinationCollectionID
handle.value.data.requestID = `${resolvedDestinationCollectionID}/${reqIndexPos}`

console.error(`Handle updated to `, handle.value.data)
}
})

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

0 comments on commit 4926b7d

Please sign in to comment.