From 0a0f441da1ee1b70ca5f8a50ee563a0c736bb66b Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Tue, 20 Feb 2024 20:19:57 +0530 Subject: [PATCH] refactor: unify markup --- .../src/components/new-collections/index.vue | 94 +--- .../components/new-collections/rest/index.vue | 449 ++++++++++-------- .../src/helpers/collection/collection.ts | 50 +- .../src/helpers/collection/request.ts | 16 +- .../providers/personal.workspace.ts | 66 +-- 5 files changed, 354 insertions(+), 321 deletions(-) diff --git a/packages/hoppscotch-common/src/components/new-collections/index.vue b/packages/hoppscotch-common/src/components/new-collections/index.vue index 73a0e0e57..d8020bde4 100644 --- a/packages/hoppscotch-common/src/components/new-collections/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/index.vue @@ -1,52 +1,19 @@ diff --git a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue index 832f0ed7b..4b147e18b 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -1,215 +1,244 @@ @@ -229,7 +258,6 @@ import { WorkspaceRESTCollectionTreeAdapter } from "~/helpers/adapters/Workspace import { TeamCollection } from "~/helpers/backend/graphql" import { getFoldersByPath, - resolveSaveContextOnCollectionReorder, updateInheritedPropertiesForAffectedRequests, } from "~/helpers/collection/collection" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" @@ -253,6 +281,7 @@ import { RESTTabService } from "~/services/tab/rest" import IconImport from "~icons/lucide/folder-down" import IconHelpCircle from "~icons/lucide/help-circle" import IconPlus from "~icons/lucide/plus" +import { currentReorderingStatus$ } from "~/newstore/reordering" const t = useI18n() const toast = useToast() @@ -306,6 +335,12 @@ const emit = defineEmits<{ const workspaceService = useService(NewWorkspaceService) const restCollectionState = useReadonlyStream(restCollections$, []) +const currentReorderingStatus = useReadonlyStream(currentReorderingStatus$, { + type: "collection", + id: "", + parentID: "", +}) + const treeAdapter = markRaw( new WorkspaceRESTCollectionTreeAdapter( props.workspaceHandle, @@ -313,6 +348,9 @@ const treeAdapter = markRaw( ) ) +const draggingToRoot = ref(false) +const searchText = ref("") + const modalLoadingState = ref(false) const showModalAdd = ref(false) @@ -1292,6 +1330,27 @@ const dropEvent = ( } } +/** + * This function is called when the user drops the collection + * to the root + * @param payload - object containing the collection index dragged + */ +const dropToRoot = ({ dataTransfer }: DragEvent) => { + if (dataTransfer) { + const collectionIndexDragged = dataTransfer.getData("collectionIndex") + if (!collectionIndexDragged) return + // check if the collection is already in the root + if (isAlreadyInRoot(collectionIndexDragged)) { + toast.error(`${t("collection.invalid_root_move")}`) + } else { + moveRESTFolder(collectionIndexDragged, null) + toast.success(`${t("collection.moved")}`) + } + + draggingToRoot.value = false + } +} + const dropRequest = (payload: { parentCollectionIndexPath?: string | undefined requestIndex: string diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index e4980fac9..59f25cb62 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -64,9 +64,14 @@ export function resolveSaveContextOnCollectionReorder( const tabService = getService(RESTTabService) const tabs = tabService.getTabsRefTo((tab) => { + if (tab.document.saveContext?.originLocation === "user-collection") { + return affectedPaths.has(tab.document.saveContext.folderPath) + } + return ( - tab.document.saveContext?.originLocation === "user-collection" && - affectedPaths.has(tab.document.saveContext.folderPath) + tab.document.saveContext?.originLocation === + "workspace-user-collection" && + affectedPaths.has(tab.document.saveContext.collectionID) ) }) @@ -77,6 +82,16 @@ export function resolveSaveContextOnCollectionReorder( )! tab.value.document.saveContext.folderPath = newPath } + + if ( + tab.value.document.saveContext?.originLocation === + "workspace-user-collection" + ) { + const newPath = affectedPaths.get( + tab.value.document.saveContext?.collectionID + )! + tab.value.document.saveContext.collectionID = newPath + } } } @@ -93,9 +108,14 @@ export function updateSaveContextForAffectedRequests( ) { const tabService = getService(RESTTabService) const tabs = tabService.getTabsRefTo((tab) => { + if (tab.document.saveContext?.originLocation === "user-collection") { + return tab.document.saveContext.folderPath.startsWith(oldFolderPath) + } + return ( - tab.document.saveContext?.originLocation === "user-collection" && - tab.document.saveContext.folderPath.startsWith(oldFolderPath) + tab.document.saveContext?.originLocation === + "workspace-user-collection" && + tab.document.saveContext.collectionID.startsWith(oldFolderPath) ) }) @@ -109,6 +129,19 @@ export function updateSaveContextForAffectedRequests( ), } } + + if ( + tab.value.document.saveContext?.originLocation === + "workspace-user-collection" + ) { + tab.value.document.saveContext = { + ...tab.value.document.saveContext, + collectionID: tab.value.document.saveContext.collectionID.replace( + oldFolderPath, + newFolderPath + ), + } + } } } /** @@ -281,9 +314,14 @@ export function updateInheritedPropertiesForAffectedRequests( function resetSaveContextForAffectedRequests(folderPath: string) { const tabService = getService(RESTTabService) const tabs = tabService.getTabsRefTo((tab) => { + if (tab.document.saveContext?.originLocation === "user-collection") { + return tab.document.saveContext.folderPath.startsWith(folderPath) + } + return ( - tab.document.saveContext?.originLocation === "user-collection" && - tab.document.saveContext.folderPath.startsWith(folderPath) + tab.document.saveContext?.originLocation === + "workspace-user-collection" && + tab.document.saveContext.collectionID.startsWith(folderPath) ) }) diff --git a/packages/hoppscotch-common/src/helpers/collection/request.ts b/packages/hoppscotch-common/src/helpers/collection/request.ts index d5576c3d7..300c5963c 100644 --- a/packages/hoppscotch-common/src/helpers/collection/request.ts +++ b/packages/hoppscotch-common/src/helpers/collection/request.ts @@ -39,10 +39,20 @@ export function resolveSaveContextOnRequestReorder(payload: { const tabService = getService(RESTTabService) const tabs = tabService.getTabsRefTo((tab) => { + if (tab.document.saveContext?.originLocation === "user-collection") { + return ( + tab.document.saveContext.folderPath === folderPath && + affectedIndexes.has(tab.document.saveContext.requestIndex) + ) + } + return ( - tab.document.saveContext?.originLocation === "user-collection" && - tab.document.saveContext.folderPath === folderPath && - affectedIndexes.has(tab.document.saveContext.requestIndex) + tab.document.saveContext?.originLocation === + "workspace-user-collection" && + tab.document.saveContext.collectionID === folderPath && + affectedIndexes.has( + parseInt(tab.document.saveContext.requestID.split("/").slice(-1)[0]) + ) ) }) 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 ed1004462..afdec1ee4 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 @@ -508,13 +508,11 @@ export class PersonalWorkspaceProviderService draggedCollectionIndex, destinationCollectionIndex ) - // resolveSaveContextOnCollectionReorder({ - // lastIndex: pathToLastIndex(draggedCollectionIndex), - // newIndex: pathToLastIndex( - // destinationCollectionIndex ? destinationCollectionIndex : "" - // ), - // folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"), - // }) + resolveSaveContextOnCollectionReorder({ + lastIndex: this.pathToLastIndex(draggedCollectionIndex), + newIndex: this.pathToLastIndex(destinationCollectionIndex ?? ""), + folderPath: draggedCollectionIndex.split("/").slice(0, -1).join("/"), + }) return Promise.resolve(E.right(undefined)) } @@ -533,34 +531,38 @@ export class PersonalWorkspaceProviderService 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" - // ) + if (destinationCollectionIndex === null) { + return Promise.resolve(E.right(undefined)) + } - // updateSaveContextForAffectedRequests( - // draggedCollectionIndex, - // `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}` - // ) + 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) + + resolveSaveContextOnCollectionReorder( + { + lastIndex: this.pathToLastIndex(draggedCollectionIndex), + newIndex: -1, + folderPath: parentFolder, + length: getFoldersByPath(restCollectionStore.value.state, parentFolder) + .length, + }, + "drop" + ) + + updateSaveContextForAffectedRequests( + draggedCollectionIndex, + `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}` + ) // const { auth, headers } = cascadeParentCollectionForHeaderAuth( // `${destinationCollectionIndex}/${totalFoldersOfDestinationCollection}`,