SaveRequest.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <!-- eslint-disable prettier/prettier -->
  2. <template>
  3. <HoppSmartModal
  4. v-if="show"
  5. dialog
  6. :title="`${t('collection.save_as')}`"
  7. @close="hideModal"
  8. >
  9. <template #body>
  10. <div class="flex flex-col">
  11. <HoppSmartInput
  12. v-model="requestName"
  13. styles="relative flex"
  14. placeholder=" "
  15. :label="t('request.name')"
  16. input-styles="floating-input"
  17. @submit="saveRequestAs"
  18. />
  19. <label class="p-4">
  20. {{ t("collection.select_location") }}
  21. </label>
  22. <!-- <CollectionsGraphql
  23. v-if="mode === 'graphql'"
  24. :picked="picked"
  25. :save-request="true"
  26. @select="onSelect"
  27. /> -->
  28. <!-- <Collections
  29. v-else
  30. :picked="picked"
  31. :save-request="true"
  32. @select="onSelect"
  33. @update-team="updateTeam"
  34. @update-collection-type="updateCollectionType"
  35. /> -->
  36. <NewCollections
  37. :picked="picked"
  38. :save-request="true"
  39. platform="rest"
  40. @select="onSelect"
  41. />
  42. </div>
  43. </template>
  44. <template #footer>
  45. <span class="flex space-x-2">
  46. <HoppButtonPrimary
  47. :label="`${t('action.save')}`"
  48. :loading="modalLoadingState"
  49. outline
  50. @click="saveRequestAs"
  51. />
  52. <HoppButtonSecondary
  53. :label="`${t('action.cancel')}`"
  54. outline
  55. filled
  56. @click="hideModal"
  57. />
  58. </span>
  59. </template>
  60. </HoppSmartModal>
  61. </template>
  62. <script setup lang="ts">
  63. import { useI18n } from "@composables/i18n"
  64. import { useToast } from "@composables/toast"
  65. import {
  66. HoppGQLRequest,
  67. HoppRESTRequest,
  68. isHoppRESTRequest,
  69. } from "@hoppscotch/data"
  70. import { computedWithControl } from "@vueuse/core"
  71. import { useService } from "dioc/vue"
  72. import * as E from "fp-ts/Either"
  73. import { cloneDeep } from "lodash-es"
  74. import { computed, nextTick, reactive, ref, watch } from "vue"
  75. import { Picked } from "~/helpers/types/HoppPicked"
  76. import { cascadeParentCollectionForHeaderAuth } from "~/newstore/collections"
  77. import { NewWorkspaceService } from "~/services/new-workspace"
  78. import { GQLTabService } from "~/services/tab/graphql"
  79. import { RESTTabService } from "~/services/tab/rest"
  80. const t = useI18n()
  81. const toast = useToast()
  82. const RESTTabs = useService(RESTTabService)
  83. const GQLTabs = useService(GQLTabService)
  84. const workspaceService = useService(NewWorkspaceService)
  85. // type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
  86. // type CollectionType =
  87. // | {
  88. // type: "team-collections"
  89. // selectedTeam: SelectedTeam
  90. // }
  91. // | { type: "my-collections"; selectedTeam: undefined }
  92. const props = withDefaults(
  93. defineProps<{
  94. show: boolean
  95. mode: "rest" | "graphql"
  96. request?: HoppRESTRequest | HoppGQLRequest | null
  97. }>(),
  98. {
  99. show: false,
  100. mode: "rest",
  101. request: null,
  102. }
  103. )
  104. const emit = defineEmits<{
  105. (
  106. event: "edit-request",
  107. payload: {
  108. folderPath: string
  109. requestIndex: string
  110. request: HoppRESTRequest
  111. }
  112. ): void
  113. (e: "hide-modal"): void
  114. }>()
  115. const gqlRequestName = computedWithControl(
  116. () => GQLTabs.currentActiveTab.value,
  117. () => GQLTabs.currentActiveTab.value.document.request.name
  118. )
  119. const restRequestName = computedWithControl(
  120. () => RESTTabs.currentActiveTab.value,
  121. () => RESTTabs.currentActiveTab.value.document.request.name
  122. )
  123. const reqName = computed(() => {
  124. if (props.request) {
  125. return props.request.name
  126. } else if (props.mode === "rest") {
  127. return restRequestName.value
  128. }
  129. return gqlRequestName.value
  130. })
  131. const requestName = ref(reqName.value)
  132. watch(
  133. () => [RESTTabs.currentActiveTab.value, GQLTabs.currentActiveTab.value],
  134. () => {
  135. if (props.mode === "rest") {
  136. requestName.value =
  137. RESTTabs.currentActiveTab.value?.document.request.name ?? ""
  138. } else {
  139. requestName.value =
  140. GQLTabs.currentActiveTab.value?.document.request.name ?? ""
  141. }
  142. }
  143. )
  144. const requestData = reactive({
  145. name: requestName,
  146. collectionIndex: undefined as number | undefined,
  147. folderName: undefined as number | undefined,
  148. requestIndex: undefined as number | undefined,
  149. })
  150. // const collectionsType = ref<CollectionType>({
  151. // type: "my-collections",
  152. // selectedTeam: undefined,
  153. // })
  154. const picked = ref<Picked | null>(null)
  155. const modalLoadingState = ref(false)
  156. // Resets
  157. watch(
  158. () => requestData.collectionIndex,
  159. () => {
  160. requestData.folderName = undefined
  161. requestData.requestIndex = undefined
  162. }
  163. )
  164. watch(
  165. () => requestData.folderName,
  166. () => {
  167. requestData.requestIndex = undefined
  168. }
  169. )
  170. // TODO: To be removed
  171. // const updateTeam = (newTeam: SelectedTeam) => {
  172. // collectionsType.value.selectedTeam = newTeam
  173. // }
  174. // const updateCollectionType = (type: CollectionType["type"]) => {
  175. // collectionsType.value.type = type
  176. // }
  177. const onSelect = (pickedVal: Picked | null) => {
  178. picked.value = pickedVal
  179. }
  180. const saveRequestAs = async () => {
  181. if (!requestName.value) {
  182. toast.error(`${t("error.empty_req_name")}`)
  183. return
  184. }
  185. if (picked.value === null) {
  186. toast.error(`${t("collection.select")}`)
  187. return
  188. }
  189. const updatedRequest =
  190. props.mode === "rest"
  191. ? cloneDeep(RESTTabs.currentActiveTab.value.document.request)
  192. : cloneDeep(GQLTabs.currentActiveTab.value.document.request)
  193. updatedRequest.name = requestName.value
  194. if (!workspaceService.activeWorkspaceHandle.value) {
  195. return
  196. }
  197. if (
  198. picked.value.pickedType === "my-collection" ||
  199. picked.value.pickedType === "my-folder"
  200. ) {
  201. if (!isHoppRESTRequest(updatedRequest))
  202. throw new Error("requestUpdated is not a REST Request")
  203. const collectionPathIndex =
  204. picked.value.pickedType === "my-collection"
  205. ? picked.value.collectionIndex.toString()
  206. : picked.value.folderPath
  207. const collectionHandleResult = await workspaceService.getCollectionHandle(
  208. workspaceService.activeWorkspaceHandle.value,
  209. collectionPathIndex
  210. )
  211. if (E.isLeft(collectionHandleResult)) {
  212. // INVALID_WORKSPACE_HANDLE | INVALID_COLLECTION_ID | INVALID_PATH
  213. return
  214. }
  215. const collectionHandle = collectionHandleResult.right
  216. const requestHandleResult = await workspaceService.createRESTRequest(
  217. collectionHandle,
  218. updatedRequest
  219. )
  220. if (E.isLeft(requestHandleResult)) {
  221. // WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
  222. return
  223. }
  224. const requestHandle = requestHandleResult.right
  225. const requestHandleRef = requestHandle.get()
  226. if (requestHandleRef.value.type === "invalid") {
  227. // WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
  228. return
  229. }
  230. RESTTabs.currentActiveTab.value.document = {
  231. request: updatedRequest,
  232. isDirty: false,
  233. saveContext: {
  234. originLocation: "workspace-user-collection",
  235. requestHandle,
  236. },
  237. }
  238. requestSaved()
  239. } else if (picked.value.pickedType === "my-request") {
  240. if (!isHoppRESTRequest(updatedRequest))
  241. throw new Error("requestUpdated is not a REST Request")
  242. const requestHandleResult = await workspaceService.getRequestHandle(
  243. workspaceService.activeWorkspaceHandle.value,
  244. `${picked.value.folderPath}/${picked.value.requestIndex.toString()}`
  245. )
  246. if (E.isLeft(requestHandleResult)) {
  247. // INVALID_COLLECTION_HANDLE | INVALID_REQUEST_ID | REQUEST_NOT_FOUND
  248. return
  249. }
  250. const requestHandle = requestHandleResult.right
  251. const requestHandleRef = requestHandle.get()
  252. if (requestHandleRef.value.type === "invalid") {
  253. // WORKSPACE_INVALIDATED
  254. return
  255. }
  256. const updateRequestResult = await workspaceService.updateRESTRequest(
  257. requestHandle,
  258. updatedRequest
  259. )
  260. if (E.isLeft(updateRequestResult)) {
  261. // WORKSPACE_INVALIDATED | INVALID_REQUEST_HANDLE
  262. return
  263. }
  264. RESTTabs.currentActiveTab.value.document = {
  265. request: updatedRequest,
  266. isDirty: false,
  267. saveContext: {
  268. originLocation: "workspace-user-collection",
  269. requestHandle,
  270. },
  271. }
  272. const { auth, headers } = cascadeParentCollectionForHeaderAuth(
  273. picked.value.folderPath,
  274. "rest"
  275. )
  276. RESTTabs.currentActiveTab.value.document.inheritedProperties = {
  277. auth,
  278. headers,
  279. }
  280. requestSaved()
  281. }
  282. // TODO: To be removed
  283. // else if (picked.value.pickedType === "teams-collection") {
  284. // if (!isHoppRESTRequest(updatedRequest))
  285. // throw new Error("requestUpdated is not a REST Request")
  286. // updateTeamCollectionOrFolder(picked.value.collectionID, updatedRequest)
  287. // platform.analytics?.logEvent({
  288. // type: "HOPP_SAVE_REQUEST",
  289. // createdNow: true,
  290. // platform: "rest",
  291. // workspaceType: "team",
  292. // })
  293. // } else if (picked.value.pickedType === "teams-folder") {
  294. // if (!isHoppRESTRequest(updatedRequest))
  295. // throw new Error("requestUpdated is not a REST Request")
  296. // updateTeamCollectionOrFolder(picked.value.folderID, updatedRequest)
  297. // platform.analytics?.logEvent({
  298. // type: "HOPP_SAVE_REQUEST",
  299. // createdNow: true,
  300. // platform: "rest",
  301. // workspaceType: "team",
  302. // })
  303. // } else if (picked.value.pickedType === "teams-request") {
  304. // if (!isHoppRESTRequest(updatedRequest))
  305. // throw new Error("requestUpdated is not a REST Request")
  306. // if (
  307. // collectionsType.value.type !== "team-collections" ||
  308. // !collectionsType.value.selectedTeam
  309. // )
  310. // throw new Error("Collections Type mismatch")
  311. // modalLoadingState.value = true
  312. // const data = {
  313. // request: JSON.stringify(updatedRequest),
  314. // title: updatedRequest.name,
  315. // }
  316. // platform.analytics?.logEvent({
  317. // type: "HOPP_SAVE_REQUEST",
  318. // createdNow: false,
  319. // platform: "rest",
  320. // workspaceType: "team",
  321. // })
  322. // pipe(
  323. // updateTeamRequest(picked.value.requestID, data),
  324. // TE.match(
  325. // (err: GQLError<string>) => {
  326. // toast.error(`${getErrorMessage(err)}`)
  327. // modalLoadingState.value = false
  328. // },
  329. // () => {
  330. // modalLoadingState.value = false
  331. // requestSaved()
  332. // }
  333. // )
  334. // )()
  335. // } else if (picked.value.pickedType === "gql-my-request") {
  336. // // TODO: Check for GQL request ?
  337. // editGraphqlRequest(
  338. // picked.value.folderPath,
  339. // picked.value.requestIndex,
  340. // updatedRequest as HoppGQLRequest
  341. // )
  342. // platform.analytics?.logEvent({
  343. // type: "HOPP_SAVE_REQUEST",
  344. // createdNow: false,
  345. // platform: "gql",
  346. // workspaceType: "team",
  347. // })
  348. // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
  349. // picked.value.folderPath,
  350. // "graphql"
  351. // )
  352. // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
  353. // auth,
  354. // headers,
  355. // }
  356. // requestSaved()
  357. // } else if (picked.value.pickedType === "gql-my-folder") {
  358. // // TODO: Check for GQL request ?
  359. // saveGraphqlRequestAs(
  360. // picked.value.folderPath,
  361. // updatedRequest as HoppGQLRequest
  362. // )
  363. // platform.analytics?.logEvent({
  364. // type: "HOPP_SAVE_REQUEST",
  365. // createdNow: true,
  366. // platform: "gql",
  367. // workspaceType: "team",
  368. // })
  369. // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
  370. // picked.value.folderPath,
  371. // "graphql"
  372. // )
  373. // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
  374. // auth,
  375. // headers,
  376. // }
  377. // requestSaved()
  378. // } else if (picked.value.pickedType === "gql-my-collection") {
  379. // // TODO: Check for GQL request ?
  380. // saveGraphqlRequestAs(
  381. // `${picked.value.collectionIndex}`,
  382. // updatedRequest as HoppGQLRequest
  383. // )
  384. // platform.analytics?.logEvent({
  385. // type: "HOPP_SAVE_REQUEST",
  386. // createdNow: true,
  387. // platform: "gql",
  388. // workspaceType: "team",
  389. // })
  390. // const { auth, headers } = cascadeParentCollectionForHeaderAuth(
  391. // `${picked.value.collectionIndex}`,
  392. // "graphql"
  393. // )
  394. // GQLTabs.currentActiveTab.value.document.inheritedProperties = {
  395. // auth,
  396. // headers,
  397. // }
  398. // requestSaved()
  399. // }
  400. }
  401. /**
  402. * Updates a team collection or folder and sets the save context to the updated request
  403. * @param collectionID - ID of the collection or folder
  404. * @param requestUpdated - Updated request
  405. */
  406. // const updateTeamCollectionOrFolder = (
  407. // collectionID: string,
  408. // requestUpdated: HoppRESTRequest
  409. // ) => {
  410. // if (
  411. // collectionsType.value.type !== "team-collections" ||
  412. // !collectionsType.value.selectedTeam
  413. // )
  414. // throw new Error("Collections Type mismatch")
  415. // modalLoadingState.value = true
  416. // const data = {
  417. // title: requestUpdated.name,
  418. // request: JSON.stringify(requestUpdated),
  419. // teamID: collectionsType.value.selectedTeam.id,
  420. // }
  421. // pipe(
  422. // createRequestInCollection(collectionID, data),
  423. // TE.match(
  424. // (err: GQLError<string>) => {
  425. // toast.error(`${getErrorMessage(err)}`)
  426. // modalLoadingState.value = false
  427. // },
  428. // (result) => {
  429. // const { createRequestInCollection } = result
  430. // RESTTabs.currentActiveTab.value.document = {
  431. // request: requestUpdated,
  432. // isDirty: false,
  433. // saveContext: {
  434. // originLocation: "team-collection",
  435. // requestID: createRequestInCollection.id,
  436. // collectionID: createRequestInCollection.collection.id,
  437. // teamID: createRequestInCollection.collection.team.id,
  438. // },
  439. // }
  440. // modalLoadingState.value = false
  441. // requestSaved()
  442. // }
  443. // )
  444. // )()
  445. // }
  446. const requestSaved = () => {
  447. toast.success(`${t("request.added")}`)
  448. nextTick(() => {
  449. RESTTabs.currentActiveTab.value.document.isDirty = false
  450. })
  451. hideModal()
  452. }
  453. const hideModal = () => {
  454. picked.value = null
  455. emit("hide-modal")
  456. }
  457. // const getErrorMessage = (err: GQLError<string>) => {
  458. // console.error(err)
  459. // if (err.type === "network_error") {
  460. // return t("error.network_error")
  461. // }
  462. // switch (err.error) {
  463. // case "team_coll/short_title":
  464. // return t("collection.name_length_insufficient")
  465. // case "team/invalid_coll_id":
  466. // return t("team.invalid_id")
  467. // case "team/not_required_role":
  468. // return t("profile.no_permission")
  469. // case "team_req/not_required_role":
  470. // return t("profile.no_permission")
  471. // case "Forbidden resource":
  472. // return t("profile.no_permission")
  473. // default:
  474. // return t("error.something_went_wrong")
  475. // }
  476. // }
  477. </script>