SaveRequest.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <SmartModal v-if="show" :title="$t('collection.save_as')" @close="hideModal">
  3. <template #body>
  4. <div class="flex flex-col px-2">
  5. <div class="flex relative">
  6. <input
  7. id="selectLabelSaveReq"
  8. v-model="requestName"
  9. v-focus
  10. class="input floating-input"
  11. placeholder=" "
  12. type="text"
  13. autocomplete="off"
  14. @keyup.enter="saveRequestAs"
  15. />
  16. <label for="selectLabelSaveReq">
  17. {{ $t("request.name") }}
  18. </label>
  19. </div>
  20. <label class="px-4 pt-4 pb-4">
  21. {{ $t("collection.select_location") }}
  22. </label>
  23. <CollectionsGraphql
  24. v-if="mode === 'graphql'"
  25. :doc="false"
  26. :show-coll-actions="false"
  27. :picked="picked"
  28. :saving-mode="true"
  29. @select="onSelect"
  30. />
  31. <Collections
  32. v-else
  33. :picked="picked"
  34. :save-request="true"
  35. @select="onSelect"
  36. @update-collection="collectionsType.type = $event"
  37. @update-coll-type="onUpdateCollType"
  38. />
  39. </div>
  40. </template>
  41. <template #footer>
  42. <span>
  43. <ButtonPrimary
  44. :label="$t('action.save')"
  45. @click.native="saveRequestAs"
  46. />
  47. <ButtonSecondary
  48. :label="$t('action.cancel')"
  49. @click.native="hideModal"
  50. />
  51. </span>
  52. </template>
  53. </SmartModal>
  54. </template>
  55. <script>
  56. import { defineComponent } from "@nuxtjs/composition-api"
  57. import * as teamUtils from "~/helpers/teams/utils"
  58. import {
  59. saveRESTRequestAs,
  60. editRESTRequest,
  61. editGraphqlRequest,
  62. saveGraphqlRequestAs,
  63. } from "~/newstore/collections"
  64. import { getGQLSession, useGQLRequestName } from "~/newstore/GQLSession"
  65. import {
  66. getRESTRequest,
  67. useRESTRequestName,
  68. setRESTSaveContext,
  69. } from "~/newstore/RESTSession"
  70. export default defineComponent({
  71. props: {
  72. // mode can be either "graphql" or "rest"
  73. mode: { type: String, default: "rest" },
  74. show: Boolean,
  75. },
  76. setup(props) {
  77. return {
  78. requestName:
  79. props.mode === "rest" ? useRESTRequestName() : useGQLRequestName(),
  80. }
  81. },
  82. data() {
  83. return {
  84. requestData: {
  85. name: this.requestName,
  86. collectionIndex: undefined,
  87. folderName: undefined,
  88. requestIndex: undefined,
  89. },
  90. collectionsType: {
  91. type: "my-collections",
  92. selectedTeam: undefined,
  93. },
  94. picked: null,
  95. }
  96. },
  97. watch: {
  98. "requestData.collectionIndex": function resetFolderAndRequestIndex() {
  99. // if user has chosen some folder, than selected other collection, which doesn't have any folders
  100. // than `requestUpdateData.folderName` won't be reseted
  101. this.$data.requestData.folderName = undefined
  102. this.$data.requestData.requestIndex = undefined
  103. },
  104. "requestData.folderName": function resetRequestIndex() {
  105. this.$data.requestData.requestIndex = undefined
  106. },
  107. editingRequest({ name }) {
  108. this.$data.requestData.name = name || this.$data.defaultRequestName
  109. },
  110. },
  111. methods: {
  112. onUpdateCollType(newCollType) {
  113. this.collectionsType = newCollType
  114. },
  115. onSelect({ picked }) {
  116. this.picked = picked
  117. },
  118. async saveRequestAs() {
  119. if (!this.requestName) {
  120. this.$toast.error(this.$t("error.empty_req_name"), {
  121. icon: "error_outline",
  122. })
  123. return
  124. }
  125. if (this.picked == null) {
  126. this.$toast.error(this.$t("collection.select"), {
  127. icon: "error_outline",
  128. })
  129. return
  130. }
  131. const requestUpdated =
  132. this.mode === "rest" ? getRESTRequest() : getGQLSession()
  133. // Filter out all REST file inputs
  134. if (this.mode === "rest" && requestUpdated.bodyParams) {
  135. requestUpdated.bodyParams = requestUpdated.bodyParams.map((param) =>
  136. param?.value?.[0] instanceof File ? { ...param, value: "" } : param
  137. )
  138. }
  139. if (this.picked.pickedType === "my-request") {
  140. editRESTRequest(
  141. this.picked.folderPath,
  142. this.picked.requestIndex,
  143. requestUpdated
  144. )
  145. setRESTSaveContext({
  146. originLocation: "user-collection",
  147. folderPath: this.picked.folderPath,
  148. requestIndex: this.picked.requestIndex,
  149. })
  150. } else if (this.picked.pickedType === "my-folder") {
  151. const insertionIndex = saveRESTRequestAs(
  152. this.picked.folderPath,
  153. requestUpdated
  154. )
  155. setRESTSaveContext({
  156. originLocation: "user-collection",
  157. folderPath: this.picked.folderPath,
  158. requestIndex: insertionIndex,
  159. })
  160. } else if (this.picked.pickedType === "my-collection") {
  161. const insertionIndex = saveRESTRequestAs(
  162. `${this.picked.collectionIndex}`,
  163. requestUpdated
  164. )
  165. setRESTSaveContext({
  166. originLocation: "user-collection",
  167. folderPath: `${this.picked.collectionIndex}`,
  168. requestIndex: insertionIndex,
  169. })
  170. } else if (this.picked.pickedType === "teams-request") {
  171. teamUtils.overwriteRequestTeams(
  172. this.$apollo,
  173. JSON.stringify(requestUpdated),
  174. requestUpdated.name,
  175. this.picked.requestID
  176. )
  177. setRESTSaveContext({
  178. originLocation: "team-collection",
  179. requestID: this.picked.requestID,
  180. })
  181. } else if (this.picked.pickedType === "teams-folder") {
  182. const req = await teamUtils.saveRequestAsTeams(
  183. this.$apollo,
  184. JSON.stringify(requestUpdated),
  185. requestUpdated.name,
  186. this.collectionsType.selectedTeam.id,
  187. this.picked.folderID
  188. )
  189. if (req && req.id) {
  190. setRESTSaveContext({
  191. originLocation: "team-collection",
  192. requestID: req.id,
  193. teamID: this.collectionsType.selectedTeam.id,
  194. collectionID: this.picked.folderID,
  195. })
  196. }
  197. } else if (this.picked.pickedType === "teams-collection") {
  198. const req = await teamUtils.saveRequestAsTeams(
  199. this.$apollo,
  200. JSON.stringify(requestUpdated),
  201. requestUpdated.name,
  202. this.collectionsType.selectedTeam.id,
  203. this.picked.collectionID
  204. )
  205. if (req && req.id) {
  206. setRESTSaveContext({
  207. originLocation: "team-collection",
  208. requestID: req.id,
  209. teamID: this.collectionsType.selectedTeam.id,
  210. collectionID: this.picked.collectionID,
  211. })
  212. }
  213. } else if (this.picked.pickedType === "gql-my-request") {
  214. editGraphqlRequest(
  215. this.picked.folderPath,
  216. this.picked.requestIndex,
  217. requestUpdated
  218. )
  219. } else if (this.picked.pickedType === "gql-my-folder") {
  220. saveGraphqlRequestAs(this.picked.folderPath, requestUpdated)
  221. } else if (this.picked.pickedType === "gql-my-collection") {
  222. saveGraphqlRequestAs(`${this.picked.collectionIndex}`, requestUpdated)
  223. }
  224. this.$toast.success(this.$t("request.added"), {
  225. icon: "post_add",
  226. })
  227. this.hideModal()
  228. },
  229. hideModal() {
  230. this.picked = null
  231. this.$emit("hide-modal")
  232. },
  233. },
  234. })
  235. </script>