feat: save request context

This commit is contained in:
Andrew Bastin
2021-08-19 12:06:56 +05:30
parent 51142e5c77
commit 1fdb6488fd
7 changed files with 169 additions and 10 deletions

View File

@@ -14,11 +14,13 @@ import { useStream } from "~/helpers/utils/composables"
import { HoppTestResult } from "~/helpers/types/HoppTestResult"
import { HoppRESTAuth } from "~/helpers/types/HoppRESTAuth"
import { ValidContentTypes } from "~/helpers/utils/contenttypes"
import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
type RESTSession = {
request: HoppRESTRequest
response: HoppRESTResponse | null
testResults: HoppTestResult | null
saveContext: HoppRequestSaveContext | null
}
const defaultRESTRequest: HoppRESTRequest = {
@@ -45,6 +47,7 @@ const defaultRESTSession: RESTSession = {
request: defaultRESTRequest,
response: null,
testResults: null,
saveContext: null,
}
const dispatchers = defineDispatchers({
@@ -338,6 +341,14 @@ const dispatchers = defineDispatchers({
testResults: newResults,
}
},
setSaveContext(
_,
{ newContext }: { newContext: HoppRequestSaveContext | null }
) {
return {
saveContext: newContext,
}
},
})
const restSessionStore = new DispatchingStore(defaultRESTSession, dispatchers)
@@ -346,13 +357,31 @@ export function getRESTRequest() {
return restSessionStore.subject$.value.request
}
export function setRESTRequest(req: HoppRESTRequest) {
export function setRESTRequest(
req: HoppRESTRequest,
saveContext?: HoppRequestSaveContext | null
) {
restSessionStore.dispatch({
dispatcher: "setRequest",
payload: {
req,
},
})
if (saveContext) setRESTSaveContext(saveContext)
}
export function setRESTSaveContext(saveContext: HoppRequestSaveContext | null) {
restSessionStore.dispatch({
dispatcher: "setSaveContext",
payload: {
newContext: saveContext,
},
})
}
export function getRESTSaveContext() {
return restSessionStore.value.saveContext
}
export function resetRESTRequest() {

View File

@@ -1,5 +1,6 @@
import { pluck } from "rxjs/operators"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
import { getRESTSaveContext, setRESTSaveContext } from "./RESTSession"
interface Collection {
name: string
@@ -255,6 +256,16 @@ const collectionDispatchers = defineDispatchers({
targetLocation.requests.splice(requestIndex, 1)
// If the save context is set and is set to the same source, we invalidate it
const saveCtx = getRESTSaveContext()
if (
saveCtx?.originLocation === "user-collection" &&
saveCtx.folderPath === path &&
saveCtx.requestIndex === requestIndex
) {
setRESTSaveContext(null)
}
return {
state: newState,
}
@@ -416,6 +427,15 @@ export function editRESTRequest(
}
export function saveRESTRequestAs(path: string, request: any) {
// For calculating the insertion request index
debugger
const targetLocation = navigateToFolderWithIndexPath(
restCollectionStore.value.state,
path.split("/").map((x) => parseInt(x))
)
const insertionIndex = targetLocation!.requests.length
restCollectionStore.dispatch({
dispatcher: "saveRequestAs",
payload: {
@@ -423,6 +443,8 @@ export function saveRESTRequestAs(path: string, request: any) {
request,
},
})
return insertionIndex
}
export function removeRESTRequest(path: string, requestIndex: number) {