From d0c7c4a245c4f7bf5f01a58771c18507fa45d948 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Tue, 20 Feb 2024 19:45:36 +0530 Subject: [PATCH] refactor: provider method definitions for collection reorder/move --- .../new-collections/rest/Collection.vue | 4 +- .../components/new-collections/rest/index.vue | 5 +- .../src/services/new-workspace/index.ts | 122 +++++++++++ .../src/services/new-workspace/provider.ts | 18 ++ .../providers/personal.workspace.ts | 198 ++++++++++++++++++ 5 files changed, 344 insertions(+), 3 deletions(-) diff --git a/packages/hoppscotch-common/src/components/new-collections/rest/Collection.vue b/packages/hoppscotch-common/src/components/new-collections/rest/Collection.vue index e22d572e6..f28567745 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/Collection.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/Collection.vue @@ -23,7 +23,7 @@
, + destinationCollectionIndex: string + ): Promise< + E.Either, 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, + destinationCollectionIndex: string + ): Promise< + E.Either, 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, + destinationRequestIndex: string, + destinationCollectionIndex: string + ): Promise< + E.Either, 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, + destinationCollectionIndex: string + ): Promise< + E.Either, 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 ): Promise< diff --git a/packages/hoppscotch-common/src/services/new-workspace/provider.ts b/packages/hoppscotch-common/src/services/new-workspace/provider.ts index 5886d7422..99a5a43a2 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/provider.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/provider.ts @@ -80,4 +80,22 @@ export interface WorkspaceProvider { collectionHandle: HandleRef, collection: HoppCollection ): Promise> + + reorderRESTCollection( + collectionHandle: HandleRef, + destinationCollectionIndex: string + ): Promise> + moveRESTCollection( + collectionHandle: HandleRef, + destinationCollectionIndex: string | null + ): Promise> + reorderRESTRequest( + requestHandle: HandleRef, + destinationRequestIndex: string, + destinationCollectionIndex: string + ): Promise> + moveRESTRequest( + requestHandle: HandleRef, + destinationCollectionIndex: string + ): Promise> } diff --git a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts index 4b53d0a67..ed1004462 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts +++ b/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, + destinationCollectionIndex: string + ): Promise> { + 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, + destinationCollectionIndex: string | null + ): Promise> { + 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, + destinationCollectionIndex: string, + destinationRequestIndex: string + ): Promise> { + 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, + destinationCollectionIndex: string + ): Promise> { + 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, collectionID: string