Request.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <div class="flex flex-col" :class="[{ 'bg-primaryLight': dragging }]">
  3. <div
  4. class="flex items-stretch group"
  5. draggable="true"
  6. @dragstart="dragStart"
  7. @dragover.stop
  8. @dragleave="dragging = false"
  9. @dragend="dragging = false"
  10. @contextmenu.prevent="options.tippy().show()"
  11. >
  12. <span
  13. class="flex items-center justify-center w-16 px-2 truncate cursor-pointer"
  14. :class="getRequestLabelColor(request.method)"
  15. @click="!doc ? selectRequest() : {}"
  16. >
  17. <SmartIcon
  18. v-if="isSelected"
  19. class="svg-icons"
  20. :class="{ 'text-accent': isSelected }"
  21. name="check-circle"
  22. />
  23. <span v-else class="truncate">
  24. {{ request.method }}
  25. </span>
  26. </span>
  27. <span
  28. class="flex items-center flex-1 min-w-0 py-2 pr-2 cursor-pointer transition group-hover:text-secondaryDark"
  29. @click="!doc ? selectRequest() : {}"
  30. >
  31. <span class="truncate" :class="{ 'text-accent': isSelected }">
  32. {{ request.name }}
  33. </span>
  34. <span
  35. v-if="isActive"
  36. v-tippy="{ theme: 'tooltip' }"
  37. class="relative h-1.5 w-1.5 flex flex-shrink-0 mx-3"
  38. :title="`${$t('collection.request_in_use')}`"
  39. >
  40. <span
  41. class="absolute inline-flex flex-shrink-0 w-full h-full bg-green-500 rounded-full opacity-75 animate-ping"
  42. >
  43. </span>
  44. <span
  45. class="relative inline-flex flex-shrink-0 rounded-full h-1.5 w-1.5 bg-green-500"
  46. ></span>
  47. </span>
  48. </span>
  49. <div class="flex">
  50. <ButtonSecondary
  51. v-if="!saveRequest && !doc"
  52. v-tippy="{ theme: 'tooltip' }"
  53. svg="rotate-ccw"
  54. :title="$t('action.restore')"
  55. class="hidden group-hover:inline-flex"
  56. @click.native="!doc ? selectRequest() : {}"
  57. />
  58. <span>
  59. <tippy
  60. v-if="collectionsType.selectedTeam.myRole !== 'VIEWER'"
  61. ref="options"
  62. interactive
  63. trigger="click"
  64. theme="popover"
  65. arrow
  66. :on-shown="() => tippyActions.focus()"
  67. >
  68. <template #trigger>
  69. <ButtonSecondary
  70. v-tippy="{ theme: 'tooltip' }"
  71. :title="$t('action.more')"
  72. svg="more-vertical"
  73. />
  74. </template>
  75. <div
  76. ref="tippyActions"
  77. class="flex flex-col focus:outline-none"
  78. tabindex="0"
  79. role="menu"
  80. @keyup.e="edit.$el.click()"
  81. @keyup.d="duplicate.$el.click()"
  82. @keyup.delete="deleteAction.$el.click()"
  83. @keyup.escape="options.tippy().hide()"
  84. >
  85. <SmartItem
  86. ref="edit"
  87. svg="edit"
  88. :label="$t('action.edit')"
  89. :shortcut="['E']"
  90. @click.native="
  91. () => {
  92. emit('edit-request', {
  93. collectionIndex,
  94. folderIndex,
  95. folderName,
  96. request,
  97. requestIndex,
  98. })
  99. options.tippy().hide()
  100. }
  101. "
  102. />
  103. <SmartItem
  104. ref="duplicate"
  105. svg="copy"
  106. :label="$t('action.duplicate')"
  107. :shortcut="['D']"
  108. @click.native="
  109. () => {
  110. emit('duplicate-request', {
  111. request,
  112. requestIndex,
  113. collectionID,
  114. })
  115. options.tippy().hide()
  116. }
  117. "
  118. />
  119. <SmartItem
  120. ref="deleteAction"
  121. svg="trash-2"
  122. :label="$t('action.delete')"
  123. :shortcut="['⌫']"
  124. @click.native="
  125. () => {
  126. removeRequest()
  127. options.tippy().hide()
  128. }
  129. "
  130. />
  131. </div>
  132. </tippy>
  133. </span>
  134. </div>
  135. </div>
  136. <HttpReqChangeConfirmModal
  137. :show="confirmChange"
  138. @hide-modal="confirmChange = false"
  139. @save-change="saveRequestChange"
  140. @discard-change="discardRequestChange"
  141. />
  142. <CollectionsSaveRequest
  143. mode="rest"
  144. :show="showSaveRequestModal"
  145. @hide-modal="showSaveRequestModal = false"
  146. />
  147. </div>
  148. </template>
  149. <script setup lang="ts">
  150. import { ref, computed } from "@nuxtjs/composition-api"
  151. import {
  152. HoppRESTRequest,
  153. isEqualHoppRESTRequest,
  154. safelyExtractRESTRequest,
  155. translateToNewRequest,
  156. } from "@hoppscotch/data"
  157. import * as E from "fp-ts/Either"
  158. import {
  159. useI18n,
  160. useToast,
  161. useReadonlyStream,
  162. } from "~/helpers/utils/composables"
  163. import {
  164. getDefaultRESTRequest,
  165. restSaveContext$,
  166. setRESTRequest,
  167. setRESTSaveContext,
  168. getRESTSaveContext,
  169. getRESTRequest,
  170. } from "~/newstore/RESTSession"
  171. import { editRESTRequest } from "~/newstore/collections"
  172. import { runMutation } from "~/helpers/backend/GQLClient"
  173. import { Team, UpdateRequestDocument } from "~/helpers/backend/graphql"
  174. import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
  175. const props = defineProps<{
  176. request: HoppRESTRequest
  177. collectionIndex: number
  178. folderIndex: number
  179. folderName?: string
  180. requestIndex: string
  181. doc: boolean
  182. saveRequest: boolean
  183. collectionsType: {
  184. type: "my-collections" | "team-collections"
  185. selectedTeam: Team | undefined
  186. }
  187. collectionID: string
  188. picked?: {
  189. pickedType: string
  190. requestID: string
  191. }
  192. }>()
  193. const emit = defineEmits<{
  194. (
  195. e: "select",
  196. data:
  197. | {
  198. picked: {
  199. pickedType: string
  200. requestID: string
  201. }
  202. }
  203. | undefined
  204. ): void
  205. (
  206. e: "remove-request",
  207. data: {
  208. folderPath: string | undefined
  209. requestIndex: string
  210. }
  211. ): void
  212. (
  213. e: "edit-request",
  214. data: {
  215. collectionIndex: number
  216. folderIndex: number
  217. folderName: string | undefined
  218. requestIndex: string
  219. request: HoppRESTRequest
  220. }
  221. ): void
  222. (
  223. e: "duplicate-request",
  224. data: {
  225. collectionID: number | string
  226. requestIndex: string
  227. request: HoppRESTRequest
  228. }
  229. ): void
  230. }>()
  231. const t = useI18n()
  232. const toast = useToast()
  233. const dragging = ref(false)
  234. const requestMethodLabels = {
  235. get: "text-green-500",
  236. post: "text-yellow-500",
  237. put: "text-blue-500",
  238. delete: "text-red-500",
  239. default: "text-gray-500",
  240. }
  241. const confirmChange = ref(false)
  242. const showSaveRequestModal = ref(false)
  243. // Template refs
  244. const tippyActions = ref<any | null>(null)
  245. const options = ref<any | null>(null)
  246. const edit = ref<any | null>(null)
  247. const duplicate = ref<any | null>(null)
  248. const deleteAction = ref<any | null>(null)
  249. const active = useReadonlyStream(restSaveContext$, null)
  250. const isSelected = computed(
  251. () =>
  252. props.picked &&
  253. props.picked.pickedType === "teams-request" &&
  254. props.picked.requestID === props.requestIndex
  255. )
  256. const isActive = computed(
  257. () =>
  258. active.value &&
  259. active.value.originLocation === "team-collection" &&
  260. active.value.requestID === props.requestIndex
  261. )
  262. const dragStart = ({ dataTransfer }: DragEvent) => {
  263. if (dataTransfer) {
  264. dragging.value = !dragging.value
  265. dataTransfer.setData("requestIndex", props.requestIndex)
  266. }
  267. }
  268. const removeRequest = () => {
  269. emit("remove-request", {
  270. folderPath: props.folderName,
  271. requestIndex: props.requestIndex,
  272. })
  273. }
  274. const getRequestLabelColor = (method: string): string => {
  275. return (
  276. (requestMethodLabels as any)[method.toLowerCase()] ||
  277. requestMethodLabels.default
  278. )
  279. }
  280. const setRestReq = (request: HoppRESTRequest) => {
  281. setRESTRequest(
  282. safelyExtractRESTRequest(
  283. translateToNewRequest(request),
  284. getDefaultRESTRequest()
  285. ),
  286. {
  287. originLocation: "team-collection",
  288. requestID: props.requestIndex,
  289. req: request,
  290. }
  291. )
  292. }
  293. const selectRequest = () => {
  294. // Check if this is a save as request popup, if so we don't need to prompt the confirm change popup.
  295. if (props.saveRequest) {
  296. emit("select", {
  297. picked: {
  298. pickedType: "teams-request",
  299. requestID: props.requestIndex,
  300. },
  301. })
  302. } else if (isEqualHoppRESTRequest(props.request, getDefaultRESTRequest())) {
  303. confirmChange.value = false
  304. setRestReq(props.request)
  305. } else if (!active.value) {
  306. confirmChange.value = true
  307. } else {
  308. const currentReqWithNoChange = active.value.req
  309. const currentFullReq = getRESTRequest()
  310. // Check if whether user clicked the same request or not
  311. if (!isActive.value && currentReqWithNoChange) {
  312. // Check if there is any changes done on the current request
  313. if (isEqualHoppRESTRequest(currentReqWithNoChange, currentFullReq)) {
  314. setRestReq(props.request)
  315. } else {
  316. confirmChange.value = true
  317. }
  318. } else {
  319. setRESTSaveContext(null)
  320. }
  321. }
  322. }
  323. /** Save current request to the collection */
  324. const saveRequestChange = () => {
  325. const saveCtx = getRESTSaveContext()
  326. saveCurrentRequest(saveCtx)
  327. confirmChange.value = false
  328. }
  329. /** Discard changes and change the current request and context */
  330. const discardRequestChange = () => {
  331. setRestReq(props.request)
  332. if (!isActive.value) {
  333. setRESTSaveContext({
  334. originLocation: "team-collection",
  335. requestID: props.requestIndex,
  336. req: props.request,
  337. })
  338. }
  339. confirmChange.value = false
  340. }
  341. const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
  342. if (!saveCtx) {
  343. showSaveRequestModal.value = true
  344. return
  345. }
  346. if (saveCtx.originLocation === "team-collection") {
  347. const req = getRESTRequest()
  348. try {
  349. runMutation(UpdateRequestDocument, {
  350. requestID: saveCtx.requestID,
  351. data: {
  352. title: req.name,
  353. request: JSON.stringify(req),
  354. },
  355. })().then((result) => {
  356. if (E.isLeft(result)) {
  357. toast.error(`${t("profile.no_permission")}`)
  358. } else {
  359. toast.success(`${t("request.saved")}`)
  360. }
  361. })
  362. setRestReq(props.request)
  363. } catch (error) {
  364. showSaveRequestModal.value = true
  365. toast.error(`${t("error.something_went_wrong")}`)
  366. console.error(error)
  367. }
  368. } else if (saveCtx.originLocation === "user-collection") {
  369. try {
  370. editRESTRequest(
  371. saveCtx.folderPath,
  372. saveCtx.requestIndex,
  373. getRESTRequest()
  374. )
  375. setRestReq(props.request)
  376. toast.success(`${t("request.saved")}`)
  377. } catch (e) {
  378. setRESTSaveContext(null)
  379. saveCurrentRequest(null)
  380. }
  381. }
  382. }
  383. </script>