Skip to content

Commit

Permalink
fix: resolve GraphQL collection tree and tab dissociation issues (#4537)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 authored Nov 22, 2024
1 parent 73f3e54 commit e040f44
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import {
useSubmitFeedback,
} from "~/composables/ai-experiments"
import { GQLError } from "~/helpers/backend/GQLClient"
import { ReqType } from "~/helpers/backend/graphql"
import {
createRequestInCollection,
updateTeamRequest,
Expand Down Expand Up @@ -521,6 +522,16 @@ const saveRequestAs = async () => {
requestUpdated as HoppGQLRequest
)

GQLTabs.currentActiveTab.value.document = {
request: requestUpdated as HoppGQLRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: picked.value.folderPath,
requestIndex: picked.value.requestIndex,
},
}

platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: false,
Expand All @@ -538,14 +549,24 @@ const saveRequestAs = async () => {
headers,
}

requestSaved()
requestSaved(ReqType.Gql)
} else if (picked.value.pickedType === "gql-my-folder") {
// TODO: Check for GQL request ?
saveGraphqlRequestAs(
const insertionIndex = saveGraphqlRequestAs(
picked.value.folderPath,
requestUpdated as HoppGQLRequest
)

GQLTabs.currentActiveTab.value.document = {
request: requestUpdated as HoppGQLRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: picked.value.folderPath,
requestIndex: insertionIndex,
},
}

platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: true,
Expand All @@ -563,14 +584,24 @@ const saveRequestAs = async () => {
headers,
}

requestSaved()
requestSaved(ReqType.Gql)
} else if (picked.value.pickedType === "gql-my-collection") {
// TODO: Check for GQL request ?
saveGraphqlRequestAs(
const insertionIndex = saveGraphqlRequestAs(
`${picked.value.collectionIndex}`,
requestUpdated as HoppGQLRequest
)

GQLTabs.currentActiveTab.value.document = {
request: requestUpdated as HoppGQLRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: `${picked.value.collectionIndex}`,
requestIndex: insertionIndex,
},
}

platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: true,
Expand All @@ -588,7 +619,7 @@ const saveRequestAs = async () => {
headers,
}

requestSaved()
requestSaved(ReqType.Gql)
}
}

Expand Down Expand Up @@ -643,10 +674,14 @@ const updateTeamCollectionOrFolder = (
)()
}

const requestSaved = () => {
const requestSaved = (tab: ReqType = ReqType.Rest) => {
toast.success(`${t("request.added")}`)
nextTick(() => {
RESTTabs.currentActiveTab.value.document.isDirty = false
if (tab === ReqType.Rest) {
RESTTabs.currentActiveTab.value.document.isDirty = false
} else {
GQLTabs.currentActiveTab.value.document.isDirty = false
}
})
hideModal()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<script setup lang="ts">
import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast"
import { HoppRESTRequest } from "@hoppscotch/data"
import { HoppGQLRequest } from "@hoppscotch/data"
import { useService } from "dioc/vue"
import { ref, watch } from "vue"
Expand All @@ -106,8 +106,8 @@ import {
} from "~/composables/ai-experiments"
import { GQLTabService } from "~/services/tab/graphql"
import IconSparkle from "~icons/lucide/sparkles"
import IconThumbsUp from "~icons/lucide/thumbs-up"
import IconThumbsDown from "~icons/lucide/thumbs-down"
import IconThumbsUp from "~icons/lucide/thumbs-up"
const toast = useToast()
const t = useI18n()
Expand All @@ -117,7 +117,7 @@ const tabs = useService(GQLTabService)
const props = defineProps<{
show: boolean
folderPath?: string
requestContext: HoppRESTRequest | null
requestContext: HoppGQLRequest | null
}>()
const emit = defineEmits<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,21 @@
import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast"
import { HoppGQLRequest } from "@hoppscotch/data"
import { ref, watch } from "vue"
import { nextTick, ref, watch } from "vue"
import { useService } from "dioc/vue"
import {
useRequestNameGeneration,
useSubmitFeedback,
} from "~/composables/ai-experiments"
import { editGraphqlRequest } from "~/newstore/collections"
import { GQLTabService } from "~/services/tab/graphql"
import IconSparkle from "~icons/lucide/sparkles"
import IconThumbsUp from "~icons/lucide/thumbs-up"
import IconThumbsDown from "~icons/lucide/thumbs-down"
const t = useI18n()
const toast = useToast()
const tabs = useService(GQLTabService)
const props = defineProps<{
show: boolean
Expand Down Expand Up @@ -163,8 +166,23 @@ const saveRequest = () => {
name: editingName.value || (props.request as any).name,
}
// Future TODO: Move below store and follow up tab updates to the page level
const possibleActiveTab = tabs.getTabRefWithSaveContext({
originLocation: "user-collection",
requestIndex: props.requestIndex!,
folderPath: props.folderPath!,
})
editGraphqlRequest(props.folderPath, props.requestIndex, requestUpdated)
if (possibleActiveTab) {
possibleActiveTab.value.document.request.name = requestUpdated.name
nextTick(() => {
possibleActiveTab.value.document.isDirty = false
})
}
hideModal()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,13 @@ const duplicateCollection = ({
collectionSyncID?: string
}) => duplicateGraphQLCollection(path, collectionSyncID)
const onAddRequest = ({
name,
path,
index,
}: {
name: string
path: string
index: number
}) => {
const onAddRequest = ({ name, path }: { name: string; path: string }) => {
const newRequest = {
...tabs.currentActiveTab.value.document.request,
name,
}
saveGraphqlRequestAs(path, newRequest)
const insertionIndex = saveGraphqlRequestAs(path, newRequest)
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
path,
Expand All @@ -425,7 +417,7 @@ const onAddRequest = ({
saveContext: {
originLocation: "user-collection",
folderPath: path,
requestIndex: index,
requestIndex: insertionIndex,
},
request: newRequest,
isDirty: false,
Expand Down
10 changes: 10 additions & 0 deletions packages/hoppscotch-common/src/newstore/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,13 +1508,23 @@ export function editGraphqlRequest(
}

export function saveGraphqlRequestAs(path: string, request: HoppGQLRequest) {
// For calculating the insertion request index
const targetLocation = navigateToFolderWithIndexPath(
graphqlCollectionStore.value.state,
path.split("/").map((x) => parseInt(x))
)

const insertionIndex = targetLocation!.requests.length

graphqlCollectionStore.dispatch({
dispatcher: "saveRequestAs",
payload: {
path,
request,
},
})

return insertionIndex
}

export function removeGraphqlRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ const HoppGQLSaveContextSchema = z.nullable(
.object({
originLocation: z.literal("user-collection"),
folderPath: z.string(),
// TODO: Investigate why this field is not populated at times
requestIndex: z.optional(z.number()),
requestIndex: z.number(),
})
.strict(),
z
Expand Down

0 comments on commit e040f44

Please sign in to comment.