refactor: update inherited properties for affected requests flow updates

This commit is contained in:
jamesgeorge007
2024-05-15 17:22:41 +05:30
parent 648cc8f5bd
commit 25b0818016
3 changed files with 61 additions and 86 deletions

View File

@@ -1703,27 +1703,28 @@ const dropRequest = async (payload: {
const { auth, headers } = cascadingAuthHeadersHandle.value.data
const possibleTab = tabs.getTabRefWithSaveContext({
originLocation: "workspace-user-collection",
requestHandle,
})
const requestHandleRef = requestHandle.get()
// If there is a tab attached to this request, update the document `inheritedProperties`
if (possibleTab) {
// @ts-expect-error - Updating the handle
possibleTab.value.document.saveContext.requestHandle.data = {
// @ts-expect-error - Updating the handle
...possibleTab.value.document.saveContext.requestHandle.data,
collectionID: destinationCollectionIndex,
requestID: `${destinationCollectionIndex}/${(
getRequestsByPath(restCollectionState.value, destinationCollectionIndex)
.length - 1
).toString()}`,
}
if (requestHandleRef.value.type === "ok") {
const newRequestIndexPos = (
getRequestsByPath(restCollectionState.value, destinationCollectionIndex)
.length - 1
).toString()
possibleTab.value.document.inheritedProperties = {
auth,
headers,
requestHandleRef.value.data.collectionID = destinationCollectionIndex
requestHandleRef.value.data.requestID = `${destinationCollectionIndex}/${newRequestIndexPos}`
const possibleTab = tabs.getTabRefWithSaveContext({
originLocation: "workspace-user-collection",
requestHandle: { get: () => requestHandleRef },
})
// If there is a tab attached to this request, update the document `inheritedProperties`
if (possibleTab) {
possibleTab.value.document.inheritedProperties = {
auth,
headers,
}
}
}

View File

@@ -1,12 +1,13 @@
import { HoppCollection } from "@hoppscotch/data"
import * as E from "fp-ts/Either"
import { ref } from "vue"
import { getService } from "~/modules/dioc"
import { GQLTabService } from "~/services/tab/graphql"
import { RESTTabService } from "~/services/tab/rest"
import { runGQLQuery } from "../backend/GQLClient"
import { GetSingleRequestDocument } from "../backend/graphql"
import { HoppGQLSaveContext } from "../graphql/document"
import { HoppRESTSaveContext } from "../rest/document"
import { HoppInheritedProperty } from "../types/HoppInheritedProperties"
import { getAffectedIndexes } from "./affectedIndex"
@@ -242,6 +243,33 @@ function removeDuplicatesAndKeepLast(arr: HoppInheritedProperty["headers"]) {
return result
}
function getSaveContextCollectionID(
saveContext: HoppRESTSaveContext | HoppGQLSaveContext | undefined
): string | undefined {
if (!saveContext) {
return
}
const { originLocation } = saveContext
if (originLocation === "team-collection") {
return saveContext.collectionID
}
if (originLocation === "user-collection") {
return saveContext.folderPath
}
const requestHandleRef = saveContext.requestHandle?.get()
if (!requestHandleRef || requestHandleRef.value.type === "invalid") {
return
}
// TODO: Remove `collectionID` and obtain it from `requestID`
return requestHandleRef.value.data.collectionID
}
export function updateInheritedPropertiesForAffectedRequests(
path: string,
inheritedProperties: HoppInheritedProperty,
@@ -250,73 +278,20 @@ export function updateInheritedPropertiesForAffectedRequests(
const tabService =
type === "rest" ? getService(RESTTabService) : getService(GQLTabService)
const tabs = tabService.getTabsRefTo((tab) => {
if (tab.document.saveContext?.originLocation === "user-collection") {
return tab.document.saveContext.folderPath.startsWith(path)
}
const effectedTabs = tabService.getTabsRefTo((tab) => {
const saveContext = tab.document.saveContext
if (tab.document.saveContext?.originLocation === "team-collection") {
return Boolean(tab.document.saveContext.collectionID?.startsWith(path))
}
const collectionID = tab.document.saveContext?.requestID
?.split("/")
.slice(0, -1)
.join("/")
return Boolean(collectionID?.startsWith(path))
const collectionID = getSaveContextCollectionID(saveContext)
return collectionID?.startsWith(path) ?? false
})
const tabsEffectedByAuth = tabs.filter((tab) => {
if (tab.value.document.saveContext?.originLocation === "user-collection") {
return (
tab.value.document.saveContext.folderPath.startsWith(path) &&
path ===
folderPathCloseToSaveContext(
tab.value.document.inheritedProperties?.auth.parentID,
path,
tab.value.document.saveContext.folderPath
)
)
}
if (
tab.value.document.saveContext?.originLocation !==
"workspace-user-collection"
) {
return false
}
return (
tab.value.document.saveContext.folderPath.startsWith(path) &&
path ===
folderPathCloseToSaveContext(
tab.value.document.inheritedProperties?.auth.parentID,
path,
tab.value.document.saveContext.folderPath
)
)
})
tabsEffectedByAuth.map((tab) => {
effectedTabs.map((tab) => {
const inheritedParentID =
tab.value.document.inheritedProperties?.auth.parentID
let contextPath = ""
if (tab.value.document.saveContext?.originLocation === "user-collection") {
contextPath = tab.value.document.saveContext.folderPath
} else if (
tab.value.document.saveContext?.originLocation ===
"workspace-user-collection"
) {
const requestHandle = ref(tab.value.document.saveContext.requestHandle)
if (requestHandle.value?.type === "ok") {
contextPath = requestHandle.value.data.collectionID
}
} else {
contextPath = tab.value.document.saveContext?.collectionID ?? ""
}
const contextPath = getSaveContextCollectionID(
tab.value.document.saveContext
)
const effectedPath = folderPathCloseToSaveContext(
inheritedParentID,

View File

@@ -1,7 +1,5 @@
import {
HoppCollection,
HoppRESTAuth,
HoppRESTHeaders,
HoppRESTRequest,
makeCollection,
} from "@hoppscotch/data"
@@ -369,7 +367,8 @@ export class PersonalWorkspaceProviderService
return
}
if (handle.value.data.requestID.startsWith(removedCollectionID)) {
// TODO: Obtain collection ID from request ID instead
if (handle.value.data.collectionID.startsWith(removedCollectionID)) {
handle.value = {
type: "invalid",
reason: "REQUEST_INVALIDATED",
@@ -1623,8 +1622,8 @@ export class PersonalWorkspaceProviderService
return { type: "ok", data: { auth, headers } }
}
const parentFolderAuth: HoppRESTAuth = parentFolder.auth
const parentFolderHeaders: HoppRESTHeaders = parentFolder.headers
const { auth: parentFolderAuth, headers: parentFolderHeaders } =
parentFolder
// check if the parent folder has authType 'inherit' and if it is the root folder
if (