Request.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. confirmRemove = true
  127. options.tippy().hide()
  128. }
  129. "
  130. />
  131. </div>
  132. </tippy>
  133. </span>
  134. </div>
  135. </div>
  136. <SmartConfirmModal
  137. :show="confirmRemove"
  138. :title="$t('confirm.remove_request')"
  139. @hide-modal="confirmRemove = false"
  140. @resolve="removeRequest"
  141. />
  142. <HttpReqChangeConfirmModal
  143. :show="confirmChange"
  144. @hide-modal="confirmChange = false"
  145. @save-change="saveRequestChange"
  146. @discard-change="discardRequestChange"
  147. />
  148. <CollectionsSaveRequest
  149. mode="rest"
  150. :show="showSaveRequestModal"
  151. @hide-modal="showSaveRequestModal = false"
  152. />
  153. </div>
  154. </template>
  155. <script setup lang="ts">
  156. import { ref, computed } from "@nuxtjs/composition-api"
  157. import {
  158. HoppRESTRequest,
  159. isEqualHoppRESTRequest,
  160. safelyExtractRESTRequest,
  161. translateToNewRequest,
  162. } from "@hoppscotch/data"
  163. import * as E from "fp-ts/Either"
  164. import {
  165. useI18n,
  166. useToast,
  167. useReadonlyStream,
  168. } from "~/helpers/utils/composables"
  169. import {
  170. getDefaultRESTRequest,
  171. restSaveContext$,
  172. setRESTRequest,
  173. setRESTSaveContext,
  174. getRESTSaveContext,
  175. getRESTRequest,
  176. } from "~/newstore/RESTSession"
  177. import { editRESTRequest } from "~/newstore/collections"
  178. import { runMutation } from "~/helpers/backend/GQLClient"
  179. import { Team, UpdateRequestDocument } from "~/helpers/backend/graphql"
  180. import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
  181. const props = defineProps<{
  182. request: HoppRESTRequest
  183. collectionIndex: number
  184. folderIndex: number
  185. folderName?: string
  186. requestIndex: string
  187. doc: boolean
  188. saveRequest: boolean
  189. collectionsType: {
  190. type: "my-collections" | "team-collections"
  191. selectedTeam: Team | undefined
  192. }
  193. collectionID: string
  194. picked?: {
  195. pickedType: string
  196. requestID: string
  197. }
  198. }>()
  199. const emit = defineEmits<{
  200. (
  201. e: "select",
  202. data:
  203. | {
  204. picked: {
  205. pickedType: string
  206. requestID: string
  207. }
  208. }
  209. | undefined
  210. ): void
  211. (
  212. e: "remove-request",
  213. data: {
  214. collectionIndex: number
  215. folderName: string | undefined
  216. requestIndex: string
  217. }
  218. ): void
  219. (
  220. e: "edit-request",
  221. data: {
  222. collectionIndex: number
  223. folderIndex: number
  224. folderName: string | undefined
  225. requestIndex: string
  226. request: HoppRESTRequest
  227. }
  228. ): void
  229. (
  230. e: "duplicate-request",
  231. data: {
  232. collectionID: number | string
  233. requestIndex: string
  234. request: HoppRESTRequest
  235. }
  236. ): void
  237. }>()
  238. const t = useI18n()
  239. const toast = useToast()
  240. const dragging = ref(false)
  241. const requestMethodLabels = {
  242. get: "text-green-500",
  243. post: "text-yellow-500",
  244. put: "text-blue-500",
  245. delete: "text-red-500",
  246. default: "text-gray-500",
  247. }
  248. const confirmRemove = ref(false)
  249. const confirmChange = ref(false)
  250. const showSaveRequestModal = ref(false)
  251. // Template refs
  252. const tippyActions = ref<any | null>(null)
  253. const options = ref<any | null>(null)
  254. const edit = ref<any | null>(null)
  255. const duplicate = ref<any | null>(null)
  256. const deleteAction = ref<any | null>(null)
  257. const active = useReadonlyStream(restSaveContext$, null)
  258. const isSelected = computed(
  259. () =>
  260. props.picked &&
  261. props.picked.pickedType === "team-collection" &&
  262. props.picked.requestID === props.requestIndex
  263. )
  264. const isActive = computed(
  265. () =>
  266. active.value &&
  267. active.value.originLocation === "team-collection" &&
  268. active.value.requestID === props.requestIndex
  269. )
  270. const dragStart = ({ dataTransfer }: DragEvent) => {
  271. if (dataTransfer) {
  272. dragging.value = !dragging.value
  273. dataTransfer.setData("requestIndex", props.requestIndex)
  274. }
  275. }
  276. const removeRequest = () => {
  277. emit("remove-request", {
  278. collectionIndex: props.collectionIndex,
  279. folderName: props.folderName,
  280. requestIndex: props.requestIndex,
  281. })
  282. }
  283. const getRequestLabelColor = (method: string): string => {
  284. return (
  285. (requestMethodLabels as any)[method.toLowerCase()] ||
  286. requestMethodLabels.default
  287. )
  288. }
  289. const setRestReq = (request: HoppRESTRequest) => {
  290. setRESTRequest(
  291. safelyExtractRESTRequest(
  292. translateToNewRequest(request),
  293. getDefaultRESTRequest()
  294. ),
  295. {
  296. originLocation: "team-collection",
  297. requestID: props.requestIndex,
  298. req: request,
  299. }
  300. )
  301. }
  302. const selectRequest = () => {
  303. if (!active.value) {
  304. confirmChange.value = true
  305. if (props.saveRequest)
  306. emit("select", {
  307. picked: {
  308. pickedType: "team-collection",
  309. requestID: props.requestIndex,
  310. },
  311. })
  312. } else {
  313. const currentReqWithNoChange = active.value.req
  314. const currentFullReq = getRESTRequest()
  315. // Check if whether user clicked the same request or not
  316. if (!isActive.value && currentReqWithNoChange) {
  317. // Check if there is any changes done on the current request
  318. if (isEqualHoppRESTRequest(currentReqWithNoChange, currentFullReq)) {
  319. setRestReq(props.request)
  320. if (props.saveRequest)
  321. emit("select", {
  322. picked: {
  323. pickedType: "team-collection",
  324. requestID: props.requestIndex,
  325. },
  326. })
  327. } else {
  328. confirmChange.value = true
  329. }
  330. } else {
  331. setRESTSaveContext(null)
  332. }
  333. }
  334. }
  335. /** Save current request to the collection */
  336. const saveRequestChange = () => {
  337. const saveCtx = getRESTSaveContext()
  338. saveCurrentRequest(saveCtx)
  339. confirmChange.value = false
  340. }
  341. /** Discard changes and change the current request and context */
  342. const discardRequestChange = () => {
  343. setRestReq(props.request)
  344. if (props.saveRequest)
  345. emit("select", {
  346. picked: {
  347. pickedType: "team-collection",
  348. requestID: props.requestIndex,
  349. },
  350. })
  351. if (!isActive.value) {
  352. setRESTSaveContext({
  353. originLocation: "team-collection",
  354. requestID: props.requestIndex,
  355. req: props.request,
  356. })
  357. }
  358. confirmChange.value = false
  359. }
  360. const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
  361. if (!saveCtx) {
  362. showSaveRequestModal.value = true
  363. return
  364. }
  365. if (saveCtx.originLocation === "team-collection") {
  366. const req = getRESTRequest()
  367. try {
  368. runMutation(UpdateRequestDocument, {
  369. requestID: saveCtx.requestID,
  370. data: {
  371. title: req.name,
  372. request: JSON.stringify(req),
  373. },
  374. })().then((result) => {
  375. if (E.isLeft(result)) {
  376. toast.error(`${t("profile.no_permission")}`)
  377. } else {
  378. toast.success(`${t("request.saved")}`)
  379. }
  380. })
  381. setRestReq(props.request)
  382. } catch (error) {
  383. showSaveRequestModal.value = true
  384. toast.error(`${t("error.something_went_wrong")}`)
  385. console.error(error)
  386. }
  387. } else if (saveCtx.originLocation === "user-collection") {
  388. try {
  389. editRESTRequest(
  390. saveCtx.folderPath,
  391. saveCtx.requestIndex,
  392. getRESTRequest()
  393. )
  394. setRestReq(props.request)
  395. toast.success(`${t("request.saved")}`)
  396. } catch (e) {
  397. setRESTSaveContext(null)
  398. saveCurrentRequest(null)
  399. }
  400. }
  401. }
  402. </script>