refactor: inherit properties when saving

This commit is contained in:
nivedin
2023-11-30 14:16:09 +05:30
committed by Andrew Bastin
parent 7c3a84246d
commit b893607ad1
3 changed files with 92 additions and 57 deletions

View File

@@ -74,6 +74,7 @@ import { Picked } from "~/helpers/types/HoppPicked"
import { useI18n } from "@composables/i18n" import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast" import { useToast } from "@composables/toast"
import { import {
cascaseParentCollectionForHeaderAuth,
editGraphqlRequest, editGraphqlRequest,
editRESTRequest, editRESTRequest,
saveGraphqlRequestAs, saveGraphqlRequestAs,
@@ -239,6 +240,17 @@ const saveRequestAs = async () => {
}, },
} }
const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
`${picked.value.collectionIndex}`
)
RESTTabs.currentActiveTab.value.document.inheritedProperties = {
auth,
headers,
parentId: `${picked.value.collectionIndex}`,
parentName: name,
}
platform.analytics?.logEvent({ platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST", type: "HOPP_SAVE_REQUEST",
createdNow: true, createdNow: true,
@@ -266,6 +278,17 @@ const saveRequestAs = async () => {
}, },
} }
const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
picked.value.folderPath
)
RESTTabs.currentActiveTab.value.document.inheritedProperties = {
auth,
headers,
parentId: picked.value.folderPath,
parentName: name,
}
platform.analytics?.logEvent({ platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST", type: "HOPP_SAVE_REQUEST",
createdNow: true, createdNow: true,
@@ -294,6 +317,17 @@ const saveRequestAs = async () => {
}, },
} }
const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
picked.value.folderPath
)
RESTTabs.currentActiveTab.value.document.inheritedProperties = {
auth,
headers,
parentId: picked.value.folderPath,
parentName: name,
}
platform.analytics?.logEvent({ platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST", type: "HOPP_SAVE_REQUEST",
createdNow: false, createdNow: false,

View File

@@ -189,6 +189,7 @@ import {
moveRESTFolder, moveRESTFolder,
navigateToFolderWithIndexPath, navigateToFolderWithIndexPath,
restCollectionStore, restCollectionStore,
cascaseParentCollectionForHeaderAuth,
} from "~/newstore/collections" } from "~/newstore/collections"
import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter" import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter"
import { import {
@@ -1314,63 +1315,6 @@ const selectPicked = (payload: Picked | null) => {
emit("select", payload) emit("select", payload)
} }
const cascaseParentCollectionForHeaderAuth = (
folderPath: string | undefined
) => {
let auth: HoppRESTRequest["auth"] = {
authType: "none",
authActive: false,
}
const headers: HoppRESTRequest["headers"] = []
let name = ""
if (!folderPath) return { auth, headers, name: "" }
const path = folderPath.split("/").map((i) => parseInt(i))
// Check if the path is empty or invalid
if (!path || path.length === 0) {
console.error("Invalid path:", folderPath)
return { auth, headers, name: "" }
}
// Loop through the path and get the last parent folder with authType other than 'inherit'
for (let i = 0; i < path.length; i++) {
const parentFolder = navigateToFolderWithIndexPath(
restCollectionStore.value.state,
[...path.slice(0, i + 1)] // Create a copy of the path array
)
// Check if parentFolder is undefined or null
if (!parentFolder) {
console.error("Parent folder not found for path:", path)
return { auth, headers, name: "" }
}
const parentFolderAuth = parentFolder.auth
const parentFolderHeaders = parentFolder.headers
// Check if authType is not 'inherit'
if (parentFolderAuth?.authType !== "inherit") {
auth = parentFolderAuth || auth
name = parentFolder.name
}
// Update headers, overwriting duplicates by key
if (parentFolderHeaders) {
const activeHeaders = parentFolderHeaders.filter((h) => h.active)
activeHeaders.forEach((header) => {
const index = headers.findIndex((h) => h.key === header.key)
if (index !== -1) {
// Replace the existing header with the same key
headers[index] = header
} else {
headers.push(header)
}
})
}
}
return { auth, headers, name }
}
/** /**
* This function is called when the user clicks on a request * This function is called when the user clicks on a request
* @param selectedRequest The request that the user clicked on emited from the collection tree * @param selectedRequest The request that the user clicked on emited from the collection tree

View File

@@ -62,6 +62,63 @@ export function navigateToFolderWithIndexPath(
return target !== undefined ? target : null return target !== undefined ? target : null
} }
export function cascaseParentCollectionForHeaderAuth(
folderPath: string | undefined
) {
let auth: HoppRESTRequest["auth"] = {
authType: "none",
authActive: false,
}
const headers: HoppRESTRequest["headers"] = []
let name = ""
if (!folderPath) return { auth, headers, name: "" }
const path = folderPath.split("/").map((i) => parseInt(i))
// Check if the path is empty or invalid
if (!path || path.length === 0) {
console.error("Invalid path:", folderPath)
return { auth, headers, name: "" }
}
// Loop through the path and get the last parent folder with authType other than 'inherit'
for (let i = 0; i < path.length; i++) {
const parentFolder = navigateToFolderWithIndexPath(
restCollectionStore.value.state,
[...path.slice(0, i + 1)] // Create a copy of the path array
)
// Check if parentFolder is undefined or null
if (!parentFolder) {
console.error("Parent folder not found for path:", path)
return { auth, headers, name: "" }
}
const parentFolderAuth = parentFolder.auth
const parentFolderHeaders = parentFolder.headers
// Check if authType is not 'inherit'
if (parentFolderAuth?.authType !== "inherit") {
auth = parentFolderAuth || auth
name = parentFolder.name
}
// Update headers, overwriting duplicates by key
if (parentFolderHeaders) {
const activeHeaders = parentFolderHeaders.filter((h) => h.active)
activeHeaders.forEach((header) => {
const index = headers.findIndex((h) => h.key === header.key)
if (index !== -1) {
// Replace the existing header with the same key
headers[index] = header
} else {
headers.push(header)
}
})
}
}
return { auth, headers, name }
}
function reorderItems(array: unknown[], from: number, to: number) { function reorderItems(array: unknown[], from: number, to: number) {
const item = array.splice(from, 1)[0] const item = array.splice(from, 1)[0]
if (from < to) { if (from < to) {