123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <!-- eslint-disable prettier/prettier -->
- <template>
- <HoppSmartModal
- v-if="show"
- dialog
- :title="`${t('collection.save_as')}`"
- @close="hideModal"
- >
- <template #body>
- <div class="flex flex-col">
- <HoppSmartInput
- v-model="requestName"
- styles="relative flex"
- placeholder=" "
- :label="t('request.name')"
- input-styles="floating-input"
- @submit="saveRequestAs"
- />
- <label class="p-4">
- {{ t("collection.select_location") }}
- </label>
- <!-- <CollectionsGraphql
- v-if="mode === 'graphql'"
- :picked="picked"
- :save-request="true"
- @select="onSelect"
- /> -->
- <!-- <Collections
- v-else
- :picked="picked"
- :save-request="true"
- @select="onSelect"
- @update-team="updateTeam"
- @update-collection-type="updateCollectionType"
- /> -->
- <NewCollections
- :picked="picked"
- :save-request="true"
- platform="rest"
- @select="onSelect"
- />
- </div>
- </template>
- <template #footer>
- <span class="flex space-x-2">
- <HoppButtonPrimary
- :label="`${t('action.save')}`"
- :loading="modalLoadingState"
- outline
- @click="saveRequestAs"
- />
- <HoppButtonSecondary
- :label="`${t('action.cancel')}`"
- outline
- filled
- @click="hideModal"
- />
- </span>
- </template>
- </HoppSmartModal>
- </template>
- <script setup lang="ts">
- import { useI18n } from "@composables/i18n"
- import { useToast } from "@composables/toast"
- import {
- HoppGQLRequest,
- HoppRESTRequest,
- isHoppRESTRequest,
- } from "@hoppscotch/data"
- import { computedWithControl } from "@vueuse/core"
- import { useService } from "dioc/vue"
- import * as E from "fp-ts/Either"
- import { cloneDeep } from "lodash-es"
- import { computed, nextTick, reactive, ref, watch } from "vue"
- import { Picked } from "~/helpers/types/HoppPicked"
- import { cascadeParentCollectionForHeaderAuth } from "~/newstore/collections"
- import { NewWorkspaceService } from "~/services/new-workspace"
- import { GQLTabService } from "~/services/tab/graphql"
- import { RESTTabService } from "~/services/tab/rest"
- const t = useI18n()
- const toast = useToast()
- const RESTTabs = useService(RESTTabService)
- const GQLTabs = useService(GQLTabService)
- const workspaceService = useService(NewWorkspaceService)
- // type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
- // type CollectionType =
- // | {
- // type: "team-collections"
- // selectedTeam: SelectedTeam
- // }
- // | { type: "my-collections"; selectedTeam: undefined }
- const props = withDefaults(
- defineProps<{
- show: boolean
- mode: "rest" | "graphql"
- request?: HoppRESTRequest | HoppGQLRequest | null
- }>(),
- {
- show: false,
- mode: "rest",
- request: null,
- }
- )
- const emit = defineEmits<{
- (
- event: "edit-request",
- payload: {
- folderPath: string
- requestIndex: string
- request: HoppRESTRequest
- }
- ): void
- (e: "hide-modal"): void
- }>()
- const gqlRequestName = computedWithControl(
- () => GQLTabs.currentActiveTab.value,
- () => GQLTabs.currentActiveTab.value.document.request.name
- )
- const restRequestName = computedWithControl(
- () => RESTTabs.currentActiveTab.value,
- () => RESTTabs.currentActiveTab.value.document.request.name
- )
- const reqName = computed(() => {
- if (props.request) {
- return props.request.name
- } else if (props.mode === "rest") {
- return restRequestName.value
- }
- return gqlRequestName.value
- })
- const requestName = ref(reqName.value)
- watch(
- () => [RESTTabs.currentActiveTab.value, GQLTabs.currentActiveTab.value],
- () => {
- if (props.mode === "rest") {
- requestName.value =
- RESTTabs.currentActiveTab.value?.document.request.name ?? ""
- } else {
- requestName.value =
- GQLTabs.currentActiveTab.value?.document.request.name ?? ""
- }
- }
- )
- const requestData = reactive({
- name: requestName,
- collectionIndex: undefined as number | undefined,
- folderName: undefined as number | undefined,
- requestIndex: undefined as number | undefined,
- })
- // const collectionsType = ref<CollectionType>({
- // type: "my-collections",
- // selectedTeam: undefined,
- // })
- const picked = ref<Picked | null>(null)
- const modalLoadingState = ref(false)
- // Resets
- watch(
- () => requestData.collectionIndex,
- () => {
- requestData.folderName = undefined
- requestData.requestIndex = undefined
- }
- )
- watch(
- () => requestData.folderName,
- () => {
- requestData.requestIndex = undefined
- }
- )
- // TODO: To be removed
- // const updateTeam = (newTeam: SelectedTeam) => {
- // collectionsType.value.selectedTeam = newTeam
- // }
- // const updateCollectionType = (type: CollectionType["type"]) => {
- // collectionsType.value.type = type
- // }
- const onSelect = (pickedVal: Picked | null) => {
- picked.value = pickedVal
- }
- const saveRequestAs = async () => {
- if (!requestName.value) {
- toast.error(`${t("error.empty_req_name")}`)
- return
- }
- if (picked.value === null) {
- toast.error(`${t("collection.select")}`)
- return
- }
- const updatedRequest =
- props.mode === "rest"
- ? cloneDeep(RESTTabs.currentActiveTab.value.document.request)
- : cloneDeep(GQLTabs.currentActiveTab.value.document.request)
- updatedRequest.name = requestName.value
- if (!workspaceService.activeWorkspaceHandle.value) {
- return
- }
- if (
- picked.value.pickedType === "my-collection" ||
- picked.value.pickedType === "my-folder"
- ) {
- if (!isHoppRESTRequest(updatedRequest))
- throw new Error("requestUpdated is not a REST Request")
- const collectionPathIndex =
- picked.value.pickedType === "my-collection"
- ? picked.value.collectionIndex.toString()
- : picked.value.folderPath
- const collectionHandleResult = await workspaceService.getCollectionHandle(
- workspaceService.activeWorkspaceHandle.value,
- collectionPathIndex
- )
- if (E.isLeft(collectionHandleResult)) {
- // INVALID_WORKSPACE_HANDLE | INVALID_COLLECTION_ID | INVALID_PATH
- return
- }
- const collectionHandle = collectionHandleResult.right
- const requestHandleResult = await workspaceService.createRESTRequest(
- collectionHandle,
- updatedRequest
- )
- if (E.isLeft(requestHandleResult)) {
- // WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
- return
- }
- const requestHandle = requestHandleResult.right
- const requestHandleRef = requestHandle.get()
- if (requestHandleRef.value.type === "invalid") {
- // WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
- return
- }
- RESTTabs.currentActiveTab.value.document = {
- request: updatedRequest,
- isDirty: false,
- saveContext: {
- originLocation: "workspace-user-collection",
- requestHandle,
- },
- }
- requestSaved()
- } else if (picked.value.pickedType === "my-request") {
- if (!isHoppRESTRequest(updatedRequest))
- throw new Error("requestUpdated is not a REST Request")
- const requestHandleResult = await workspaceService.getRequestHandle(
- workspaceService.activeWorkspaceHandle.value,
- `${picked.value.folderPath}/${picked.value.requestIndex.toString()}`
- )
- if (E.isLeft(requestHandleResult)) {
- // INVALID_COLLECTION_HANDLE | INVALID_REQUEST_ID | REQUEST_NOT_FOUND
- return
- }
- const requestHandle = requestHandleResult.right
- const requestHandleRef = requestHandle.get()
- if (requestHandleRef.value.type === "invalid") {
- // WORKSPACE_INVALIDATED
- return
- }
- const updateRequestResult = await workspaceService.updateRESTRequest(
- requestHandle,
- updatedRequest
- )
- if (E.isLeft(updateRequestResult)) {
- // WORKSPACE_INVALIDATED | INVALID_REQUEST_HANDLE
- return
- }
- RESTTabs.currentActiveTab.value.document = {
- request: updatedRequest,
- isDirty: false,
- saveContext: {
- originLocation: "workspace-user-collection",
- requestHandle,
- },
- }
- const { auth, headers } = cascadeParentCollectionForHeaderAuth(
- picked.value.folderPath,
- "rest"
- )
- RESTTabs.currentActiveTab.value.document.inheritedProperties = {
- auth,
- headers,
- }
- requestSaved()
- }
- // TODO: To be removed
- // else if (picked.value.pickedType === "teams-collection") {
- // if (!isHoppRESTRequest(updatedRequest))
- // throw new Error("requestUpdated is not a REST Request")
- // updateTeamCollectionOrFolder(picked.value.collectionID, updatedRequest)
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: true,
- // platform: "rest",
- // workspaceType: "team",
- // })
- // } else if (picked.value.pickedType === "teams-folder") {
- // if (!isHoppRESTRequest(updatedRequest))
- // throw new Error("requestUpdated is not a REST Request")
- // updateTeamCollectionOrFolder(picked.value.folderID, updatedRequest)
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: true,
- // platform: "rest",
- // workspaceType: "team",
- // })
- // } else if (picked.value.pickedType === "teams-request") {
- // if (!isHoppRESTRequest(updatedRequest))
- // throw new Error("requestUpdated is not a REST Request")
- // if (
- // collectionsType.value.type !== "team-collections" ||
- // !collectionsType.value.selectedTeam
- // )
- // throw new Error("Collections Type mismatch")
- // modalLoadingState.value = true
- // const data = {
- // request: JSON.stringify(updatedRequest),
- // title: updatedRequest.name,
- // }
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: false,
- // platform: "rest",
- // workspaceType: "team",
- // })
- // pipe(
- // updateTeamRequest(picked.value.requestID, data),
- // TE.match(
- // (err: GQLError<string>) => {
- // toast.error(`${getErrorMessage(err)}`)
- // modalLoadingState.value = false
- // },
- // () => {
- // modalLoadingState.value = false
- // requestSaved()
- // }
- // )
- // )()
- // } else if (picked.value.pickedType === "gql-my-request") {
- // // TODO: Check for GQL request ?
- // editGraphqlRequest(
- // picked.value.folderPath,
- // picked.value.requestIndex,
- // updatedRequest as HoppGQLRequest
- // )
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: false,
- // platform: "gql",
- // workspaceType: "team",
- // })
- // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
- // picked.value.folderPath,
- // "graphql"
- // )
- // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
- // auth,
- // headers,
- // }
- // requestSaved()
- // } else if (picked.value.pickedType === "gql-my-folder") {
- // // TODO: Check for GQL request ?
- // saveGraphqlRequestAs(
- // picked.value.folderPath,
- // updatedRequest as HoppGQLRequest
- // )
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: true,
- // platform: "gql",
- // workspaceType: "team",
- // })
- // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
- // picked.value.folderPath,
- // "graphql"
- // )
- // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
- // auth,
- // headers,
- // }
- // requestSaved()
- // } else if (picked.value.pickedType === "gql-my-collection") {
- // // TODO: Check for GQL request ?
- // saveGraphqlRequestAs(
- // `${picked.value.collectionIndex}`,
- // updatedRequest as HoppGQLRequest
- // )
- // platform.analytics?.logEvent({
- // type: "HOPP_SAVE_REQUEST",
- // createdNow: true,
- // platform: "gql",
- // workspaceType: "team",
- // })
- // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
- // `${picked.value.collectionIndex}`,
- // "graphql"
- // )
- // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
- // auth,
- // headers,
- // }
- // requestSaved()
- // }
- }
- /**
- * Updates a team collection or folder and sets the save context to the updated request
- * @param collectionID - ID of the collection or folder
- * @param requestUpdated - Updated request
- */
- // const updateTeamCollectionOrFolder = (
- // collectionID: string,
- // requestUpdated: HoppRESTRequest
- // ) => {
- // if (
- // collectionsType.value.type !== "team-collections" ||
- // !collectionsType.value.selectedTeam
- // )
- // throw new Error("Collections Type mismatch")
- // modalLoadingState.value = true
- // const data = {
- // title: requestUpdated.name,
- // request: JSON.stringify(requestUpdated),
- // teamID: collectionsType.value.selectedTeam.id,
- // }
- // pipe(
- // createRequestInCollection(collectionID, data),
- // TE.match(
- // (err: GQLError<string>) => {
- // toast.error(`${getErrorMessage(err)}`)
- // modalLoadingState.value = false
- // },
- // (result) => {
- // const { createRequestInCollection } = result
- // RESTTabs.currentActiveTab.value.document = {
- // request: requestUpdated,
- // isDirty: false,
- // saveContext: {
- // originLocation: "team-collection",
- // requestID: createRequestInCollection.id,
- // collectionID: createRequestInCollection.collection.id,
- // teamID: createRequestInCollection.collection.team.id,
- // },
- // }
- // modalLoadingState.value = false
- // requestSaved()
- // }
- // )
- // )()
- // }
- const requestSaved = () => {
- toast.success(`${t("request.added")}`)
- nextTick(() => {
- RESTTabs.currentActiveTab.value.document.isDirty = false
- })
- hideModal()
- }
- const hideModal = () => {
- picked.value = null
- emit("hide-modal")
- }
- // const getErrorMessage = (err: GQLError<string>) => {
- // console.error(err)
- // if (err.type === "network_error") {
- // return t("error.network_error")
- // }
- // switch (err.error) {
- // case "team_coll/short_title":
- // return t("collection.name_length_insufficient")
- // case "team/invalid_coll_id":
- // return t("team.invalid_id")
- // case "team/not_required_role":
- // return t("profile.no_permission")
- // case "team_req/not_required_role":
- // return t("profile.no_permission")
- // case "Forbidden resource":
- // return t("profile.no_permission")
- // default:
- // return t("error.something_went_wrong")
- // }
- // }
- </script>
|