Browse Source

refactor: provider method definitions for collection reorder/move

jamesgeorge007 1 year ago
parent
commit
1b1bfba790

+ 2 - 2
packages/hoppscotch-common/src/components/new-collections/rest/Collection.vue

@@ -23,7 +23,7 @@
       <div
         class="z-[3] group pointer-events-auto relative flex cursor-pointer items-stretch"
         @dragstart="dragStart"
-        @drop="handelDrop($event)"
+        @drop="handleDrop($event)"
         @dragover="handleDragOver($event)"
         @dragleave="resetDragState"
         @dragend="
@@ -387,7 +387,7 @@ const handleDragOver = (e: DragEvent) => {
   }
 }
 
-const handelDrop = (e: DragEvent) => {
+const handleDrop = (e: DragEvent) => {
   if (ordering.value) {
     orderUpdateCollectionEvent(e)
   } else if (orderingLastItem.value) {

+ 4 - 1
packages/hoppscotch-common/src/components/new-collections/rest/index.vue

@@ -1255,7 +1255,10 @@ const dragRequest = (
     requestIndex,
   }: { parentCollectionIndexPath: string | null; requestIndex: string }
 ) => {
-  if (!parentCollectionIndexPath) return
+  if (!parentCollectionIndexPath) {
+    return
+  }
+
   dataTransfer.setData("parentCollectionIndexPath", parentCollectionIndexPath)
   dataTransfer.setData("requestIndex", requestIndex)
 }

+ 122 - 0
packages/hoppscotch-common/src/services/new-workspace/index.ts

@@ -476,6 +476,128 @@ export class NewWorkspaceService extends Service {
     return E.right(result.right)
   }
 
+  public async reorderRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string
+  ): Promise<
+    E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
+  > {
+    if (collectionHandle.value.type === "invalid") {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
+    }
+
+    const provider = this.registeredProviders.get(
+      collectionHandle.value.data.providerID
+    )
+
+    if (!provider) {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
+    }
+
+    const result = await provider.reorderRESTCollection(
+      collectionHandle,
+      destinationCollectionIndex
+    )
+
+    if (E.isLeft(result)) {
+      return E.left({ type: "PROVIDER_ERROR", error: result.left })
+    }
+
+    return E.right(result.right)
+  }
+
+  public async moveRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string
+  ): Promise<
+    E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
+  > {
+    if (collectionHandle.value.type === "invalid") {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
+    }
+
+    const provider = this.registeredProviders.get(
+      collectionHandle.value.data.providerID
+    )
+
+    if (!provider) {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
+    }
+
+    const result = await provider.moveRESTCollection(
+      collectionHandle,
+      destinationCollectionIndex
+    )
+
+    if (E.isLeft(result)) {
+      return E.left({ type: "PROVIDER_ERROR", error: result.left })
+    }
+
+    return E.right(result.right)
+  }
+
+  public async reorderRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationRequestIndex: string,
+    destinationCollectionIndex: string
+  ): Promise<
+    E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
+  > {
+    if (requestHandle.value.type === "invalid") {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
+    }
+
+    const provider = this.registeredProviders.get(
+      requestHandle.value.data.providerID
+    )
+
+    if (!provider) {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
+    }
+
+    const result = await provider.reorderRESTRequest(
+      requestHandle,
+      destinationRequestIndex,
+      destinationCollectionIndex
+    )
+
+    if (E.isLeft(result)) {
+      return E.left({ type: "PROVIDER_ERROR", error: result.left })
+    }
+
+    return E.right(result.right)
+  }
+
+  public async moveRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationCollectionIndex: string
+  ): Promise<
+    E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
+  > {
+    if (requestHandle.value.type === "invalid") {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
+    }
+
+    const provider = this.registeredProviders.get(
+      requestHandle.value.data.providerID
+    )
+
+    if (!provider) {
+      return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
+    }
+
+    const result = await provider.moveRESTRequest(
+      requestHandle,
+      destinationCollectionIndex
+    )
+
+    if (E.isLeft(result)) {
+      return E.left({ type: "PROVIDER_ERROR", error: result.left })
+    }
+
+    return E.right(result.right)
+  }
+
   public async getRESTCollectionChildrenView(
     collectionHandle: HandleRef<WorkspaceCollection>
   ): Promise<

+ 18 - 0
packages/hoppscotch-common/src/services/new-workspace/provider.ts

@@ -80,4 +80,22 @@ export interface WorkspaceProvider {
     collectionHandle: HandleRef<WorkspaceCollection>,
     collection: HoppCollection
   ): Promise<E.Either<unknown, void>>
+
+  reorderRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string
+  ): Promise<E.Either<unknown, void>>
+  moveRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string | null
+  ): Promise<E.Either<unknown, void>>
+  reorderRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationRequestIndex: string,
+    destinationCollectionIndex: string
+  ): Promise<E.Either<unknown, void>>
+  moveRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationCollectionIndex: string
+  ): Promise<E.Either<unknown, void>>
 }

+ 198 - 0
packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts

@@ -16,15 +16,20 @@ import {
   addRESTCollection,
   addRESTFolder,
   appendRESTCollections,
+  cascadeParentCollectionForHeaderAuth,
   editRESTCollection,
   editRESTFolder,
   editRESTRequest,
+  moveRESTFolder,
+  moveRESTRequest,
   navigateToFolderWithIndexPath,
   removeRESTCollection,
   removeRESTFolder,
   removeRESTRequest,
   restCollectionStore,
   saveRESTRequestAs,
+  updateRESTCollectionOrder,
+  updateRESTRequestOrder,
 } from "~/newstore/collections"
 import { platform } from "~/platform"
 
@@ -51,6 +56,16 @@ import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
 import IconUser from "~icons/lucide/user"
 import { NewWorkspaceService } from ".."
 import { initializeDownloadFile } from "~/helpers/import-export/export"
+import {
+  getFoldersByPath,
+  resolveSaveContextOnCollectionReorder,
+  updateInheritedPropertiesForAffectedRequests,
+  updateSaveContextForAffectedRequests,
+} from "~/helpers/collection/collection"
+import {
+  getRequestsByPath,
+  resolveSaveContextOnRequestReorder,
+} from "~/helpers/collection/request"
 
 export class PersonalWorkspaceProviderService
   extends Service
@@ -475,6 +490,189 @@ export class PersonalWorkspaceProviderService
     return Promise.resolve(E.right(undefined))
   }
 
+  public reorderRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string
+  ): Promise<E.Either<unknown, void>> {
+    if (
+      collectionHandle.value.type !== "ok" ||
+      collectionHandle.value.data.providerID !== this.providerID ||
+      collectionHandle.value.data.workspaceID !== "personal"
+    ) {
+      return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
+    }
+
+    const draggedCollectionIndex = collectionHandle.value.data.collectionID
+
+    updateRESTCollectionOrder(
+      draggedCollectionIndex,
+      destinationCollectionIndex
+    )
+    // resolveSaveContextOnCollectionReorder({
+    //   lastIndex: pathToLastIndex(draggedCollectionIndex),
+    //   newIndex: pathToLastIndex(
+    //     destinationCollectionIndex ? destinationCollectionIndex : ""
+    //   ),
+    //   folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"),
+    // })
+
+    return Promise.resolve(E.right(undefined))
+  }
+
+  public moveRESTCollection(
+    collectionHandle: HandleRef<WorkspaceCollection>,
+    destinationCollectionIndex: string | null
+  ): Promise<E.Either<unknown, void>> {
+    if (
+      collectionHandle.value.type !== "ok" ||
+      collectionHandle.value.data.providerID !== this.providerID ||
+      collectionHandle.value.data.workspaceID !== "personal"
+    ) {
+      return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
+    }
+
+    const draggedCollectionIndex = collectionHandle.value.data.collectionID
+
+    // const parentFolder = draggedCollectionIndex
+    //   .split("/")
+    //   .slice(0, -1)
+    //   .join("/") // remove last folder to get parent folder
+
+    // const totalFoldersOfDestinationCollection =
+    //   getFoldersByPath(
+    //     restCollectionStore.value.state,
+    //     destinationCollectionIndex
+    //   ).length - (parentFolder === destinationCollectionIndex ? 1 : 0)
+
+    moveRESTFolder(draggedCollectionIndex, destinationCollectionIndex)
+
+    // resolveSaveContextOnCollectionReorder(
+    //   {
+    //     lastIndex: pathToLastIndex(draggedCollectionIndex),
+    //     newIndex: -1,
+    //     folderPath: parentFolder,
+    //     length: getFoldersByPath(restCollectionStore.value.state, parentFolder)
+    //       .length,
+    //   },
+    //   "drop"
+    // )
+
+    // updateSaveContextForAffectedRequests(
+    //   draggedCollectionIndex,
+    //   `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}`
+    // )
+
+    // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
+    //   `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}`,
+    //   "rest"
+    // )
+
+    // const inheritedProperty = {
+    //   auth,
+    //   headers,
+    // }
+
+    // updateInheritedPropertiesForAffectedRequests(
+    //   `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}`,
+    //   inheritedProperty,
+    //   "rest"
+    // )
+
+    return Promise.resolve(E.right(undefined))
+  }
+
+  public reorderRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationCollectionIndex: string,
+    destinationRequestIndex: string
+  ): Promise<E.Either<unknown, void>> {
+    if (
+      requestHandle.value.type !== "ok" ||
+      requestHandle.value.data.providerID !== this.providerID ||
+      requestHandle.value.data.workspaceID !== "personal"
+    ) {
+      return Promise.resolve(E.left("INVALID_REQUEST_HANDLE" as const))
+    }
+
+    const draggedRequestIndex = requestHandle.value.data.requestID
+
+    updateRESTRequestOrder(
+      this.pathToLastIndex(draggedRequestIndex),
+      destinationRequestIndex
+        ? this.pathToLastIndex(destinationRequestIndex)
+        : null,
+      destinationCollectionIndex
+    )
+
+    return Promise.resolve(E.right(undefined))
+  }
+
+  public moveRESTRequest(
+    requestHandle: HandleRef<WorkspaceRequest>,
+    destinationCollectionIndex: string
+  ): Promise<E.Either<unknown, void>> {
+    if (
+      requestHandle.value.type !== "ok" ||
+      requestHandle.value.data.providerID !== this.providerID ||
+      requestHandle.value.data.workspaceID !== "personal"
+    ) {
+      return Promise.resolve(E.left("INVALID_REQUEST_HANDLE" as const))
+    }
+
+    const requestIndex = requestHandle.value.data.requestID
+    const parentCollectionIndexPath = requestIndex
+      .split("/")
+      .slice(0, -1)
+      .join("/")
+
+    // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
+    //   destinationCollectionIndex,
+    //   "rest"
+    // )
+
+    // const possibleTab = tabs.getTabRefWithSaveContext({
+    //   originLocation: "user-collection",
+    //   folderPath: parentCollectionIndexPath,
+    //   requestIndex: this.pathToLastIndex(requestIndex),
+    // })
+
+    // // If there is a tab attached to this request, change save its save context
+    // if (possibleTab) {
+    //   possibleTab.value.document.saveContext = {
+    //     originLocation: "user-collection",
+    //     folderPath: destinationCollectionIndex,
+    //     requestIndex: getRequestsByPath(
+    //       restCollectionStore.value.state,
+    //       destinationCollectionIndex
+    //     ).length,
+    //   }
+
+    //   possibleTab.value.document.inheritedProperties = {
+    //     auth,
+    //     headers,
+    //   }
+    // }
+
+    // // When it's drop it's basically getting deleted from last folder. reordering last folder accordingly
+    // resolveSaveContextOnRequestReorder({
+    //   lastIndex: this.pathToLastIndex(requestIndex),
+    //   newIndex: -1, // being deleted from last folder
+    //   folderPath: parentCollectionIndexPath,
+    //   length: getRequestsByPath(
+    //     restCollectionStore.value.state,
+    //     parentCollectionIndexPath
+    //   ).length,
+    // })
+
+    moveRESTRequest(
+      parentCollectionIndexPath,
+      this.pathToLastIndex(requestIndex),
+      destinationCollectionIndex
+    )
+
+    return Promise.resolve(E.right(undefined))
+  }
+
   public getCollectionHandle(
     workspaceHandle: HandleRef<Workspace>,
     collectionID: string