Request.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. ref="options"
  61. interactive
  62. trigger="click"
  63. theme="popover"
  64. arrow
  65. :on-shown="() => tippyActions.focus()"
  66. >
  67. <template #trigger>
  68. <ButtonSecondary
  69. v-tippy="{ theme: 'tooltip' }"
  70. :title="t('action.more')"
  71. svg="more-vertical"
  72. />
  73. </template>
  74. <div
  75. ref="tippyActions"
  76. class="flex flex-col focus:outline-none"
  77. tabindex="0"
  78. role="menu"
  79. @keyup.e="edit.$el.click()"
  80. @keyup.d="duplicate.$el.click()"
  81. @keyup.delete="deleteAction.$el.click()"
  82. @keyup.escape="options.tippy().hide()"
  83. >
  84. <SmartItem
  85. ref="edit"
  86. svg="edit"
  87. :label="t('action.edit')"
  88. :shortcut="['E']"
  89. @click.native="
  90. () => {
  91. emit('edit-request', {
  92. collectionIndex,
  93. folderIndex,
  94. folderName,
  95. request,
  96. requestIndex,
  97. folderPath,
  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. collectionIndex,
  112. folderIndex,
  113. folderName,
  114. request,
  115. requestIndex,
  116. folderPath,
  117. })
  118. options.tippy().hide()
  119. }
  120. "
  121. />
  122. <SmartItem
  123. ref="deleteAction"
  124. svg="trash-2"
  125. :label="t('action.delete')"
  126. :shortcut="['⌫']"
  127. @click.native="
  128. () => {
  129. confirmRemove = true
  130. options.tippy().hide()
  131. }
  132. "
  133. />
  134. </div>
  135. </tippy>
  136. </span>
  137. </div>
  138. </div>
  139. <SmartConfirmModal
  140. :show="confirmRemove"
  141. :title="t('confirm.remove_request')"
  142. @hide-modal="confirmRemove = false"
  143. @resolve="removeRequest"
  144. />
  145. <HttpReqChangeConfirmModal
  146. :show="confirmChange"
  147. @hide-modal="confirmChange = false"
  148. @save-change="saveRequestChange"
  149. @discard-change="discardRequestChange"
  150. />
  151. <CollectionsSaveRequest
  152. mode="rest"
  153. :show="showSaveRequestModal"
  154. @hide-modal="showSaveRequestModal = false"
  155. />
  156. </div>
  157. </template>
  158. <script setup lang="ts">
  159. import { ref, computed } from "@nuxtjs/composition-api"
  160. import {
  161. HoppRESTRequest,
  162. safelyExtractRESTRequest,
  163. translateToNewRequest,
  164. isEqualHoppRESTRequest,
  165. } from "@hoppscotch/data"
  166. import * as E from "fp-ts/Either"
  167. import {
  168. useI18n,
  169. useToast,
  170. useReadonlyStream,
  171. } from "~/helpers/utils/composables"
  172. import {
  173. getDefaultRESTRequest,
  174. getRESTRequest,
  175. restSaveContext$,
  176. setRESTRequest,
  177. setRESTSaveContext,
  178. getRESTSaveContext,
  179. } from "~/newstore/RESTSession"
  180. import { editRESTRequest } from "~/newstore/collections"
  181. import { runMutation } from "~/helpers/backend/GQLClient"
  182. import { UpdateRequestDocument } from "~/helpers/backend/graphql"
  183. import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
  184. const props = defineProps<{
  185. request: HoppRESTRequest
  186. collectionIndex: number
  187. folderIndex: number
  188. folderName: string
  189. requestIndex: number
  190. doc: boolean
  191. saveRequest: boolean
  192. collectionsType: object
  193. folderPath: string
  194. picked?: {
  195. pickedType: string
  196. collectionIndex: number
  197. folderPath: string
  198. folderName: string
  199. requestIndex: number
  200. }
  201. }>()
  202. const emit = defineEmits<{
  203. (
  204. e: "select",
  205. data:
  206. | {
  207. picked: {
  208. pickedType: string
  209. collectionIndex: number
  210. folderPath: string
  211. folderName: string
  212. requestIndex: number
  213. }
  214. }
  215. | undefined
  216. ): void
  217. (
  218. e: "remove-request",
  219. data: {
  220. collectionIndex: number
  221. folderName: string
  222. folderPath: string
  223. requestIndex: number
  224. }
  225. ): void
  226. (
  227. e: "duplicate-request",
  228. data: {
  229. collectionIndex: number
  230. folderIndex: number
  231. folderName: string
  232. request: HoppRESTRequest
  233. folderPath: string
  234. requestIndex: number
  235. }
  236. ): void
  237. (
  238. e: "edit-request",
  239. data: {
  240. collectionIndex: number
  241. folderIndex: number
  242. folderName: string
  243. request: HoppRESTRequest
  244. folderPath: string
  245. requestIndex: number
  246. }
  247. ): void
  248. }>()
  249. const t = useI18n()
  250. const toast = useToast()
  251. const dragging = ref(false)
  252. const requestMethodLabels = {
  253. get: "text-green-500",
  254. post: "text-yellow-500",
  255. put: "text-blue-500",
  256. delete: "text-red-500",
  257. default: "text-gray-500",
  258. }
  259. const confirmRemove = ref(false)
  260. const confirmChange = ref(false)
  261. const showSaveRequestModal = ref(false)
  262. // Template refs
  263. const tippyActions = ref<any | null>(null)
  264. const options = ref<any | null>(null)
  265. const edit = ref<any | null>(null)
  266. const duplicate = ref<any | null>(null)
  267. const deleteAction = ref<any | null>(null)
  268. const active = useReadonlyStream(restSaveContext$, null)
  269. const isSelected = computed(
  270. () =>
  271. props.picked &&
  272. props.picked.pickedType === "my-request" &&
  273. props.picked.folderPath === props.folderPath &&
  274. props.picked.requestIndex === props.requestIndex
  275. )
  276. const isActive = computed(
  277. () =>
  278. active.value &&
  279. active.value.originLocation === "user-collection" &&
  280. active.value.folderPath === props.folderPath &&
  281. active.value.requestIndex === props.requestIndex
  282. )
  283. const dragStart = ({ dataTransfer }: DragEvent) => {
  284. if (dataTransfer) {
  285. dragging.value = !dragging.value
  286. dataTransfer.setData("folderPath", props.folderPath)
  287. dataTransfer.setData("requestIndex", props.requestIndex.toString())
  288. }
  289. }
  290. const removeRequest = () => {
  291. emit("remove-request", {
  292. collectionIndex: props.collectionIndex,
  293. folderName: props.folderName,
  294. folderPath: props.folderPath,
  295. requestIndex: props.requestIndex,
  296. })
  297. }
  298. const getRequestLabelColor = (method: string) =>
  299. requestMethodLabels[
  300. method.toLowerCase() as keyof typeof requestMethodLabels
  301. ] || requestMethodLabels.default
  302. const setRestReq = (request: any) => {
  303. setRESTRequest(
  304. safelyExtractRESTRequest(
  305. translateToNewRequest(request),
  306. getDefaultRESTRequest()
  307. ),
  308. {
  309. originLocation: "user-collection",
  310. folderPath: props.folderPath,
  311. requestIndex: props.requestIndex,
  312. req: request,
  313. }
  314. )
  315. }
  316. const selectRequest = () => {
  317. if (!active.value) {
  318. confirmChange.value = true
  319. if (props.saveRequest)
  320. emit("select", {
  321. picked: {
  322. pickedType: "my-request",
  323. collectionIndex: props.collectionIndex,
  324. folderPath: props.folderPath,
  325. folderName: props.folderName,
  326. requestIndex: props.requestIndex,
  327. },
  328. })
  329. } else {
  330. const currentReqWithNoChange = active.value.req
  331. const currentFullReq = getRESTRequest()
  332. // Check if whether user clicked the same request or not
  333. if (!isActive.value && currentReqWithNoChange !== undefined) {
  334. // Check if there is any changes done on the current request
  335. if (isEqualHoppRESTRequest(currentReqWithNoChange, currentFullReq)) {
  336. setRestReq(props.request)
  337. if (props.saveRequest)
  338. emit("select", {
  339. picked: {
  340. pickedType: "my-request",
  341. collectionIndex: props.collectionIndex,
  342. folderPath: props.folderPath,
  343. folderName: props.folderName,
  344. requestIndex: props.requestIndex,
  345. },
  346. })
  347. } else {
  348. confirmChange.value = true
  349. }
  350. } else {
  351. setRESTSaveContext(null)
  352. }
  353. }
  354. }
  355. /** Save current request to the collection */
  356. const saveRequestChange = () => {
  357. const saveCtx = getRESTSaveContext()
  358. saveCurrentRequest(saveCtx)
  359. confirmChange.value = false
  360. }
  361. /** Discard changes and change the current request and context */
  362. const discardRequestChange = () => {
  363. setRestReq(props.request)
  364. if (props.saveRequest)
  365. emit("select", {
  366. picked: {
  367. pickedType: "my-request",
  368. collectionIndex: props.collectionIndex,
  369. folderPath: props.folderPath,
  370. folderName: props.folderName,
  371. requestIndex: props.requestIndex,
  372. },
  373. })
  374. if (!isActive.value) {
  375. setRESTSaveContext({
  376. originLocation: "user-collection",
  377. folderPath: props.folderPath,
  378. requestIndex: props.requestIndex,
  379. req: props.request,
  380. })
  381. }
  382. confirmChange.value = false
  383. }
  384. const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
  385. if (!saveCtx) {
  386. showSaveRequestModal.value = true
  387. return
  388. }
  389. if (saveCtx.originLocation === "user-collection") {
  390. try {
  391. editRESTRequest(
  392. saveCtx.folderPath,
  393. saveCtx.requestIndex,
  394. getRESTRequest()
  395. )
  396. setRestReq(props.request)
  397. toast.success(`${t("request.saved")}`)
  398. } catch (e) {
  399. setRESTSaveContext(null)
  400. saveCurrentRequest(saveCtx)
  401. }
  402. } else if (saveCtx.originLocation === "team-collection") {
  403. const req = getRESTRequest()
  404. try {
  405. runMutation(UpdateRequestDocument, {
  406. requestID: saveCtx.requestID,
  407. data: {
  408. title: req.name,
  409. request: JSON.stringify(req),
  410. },
  411. })().then((result) => {
  412. if (E.isLeft(result)) {
  413. toast.error(`${t("profile.no_permission")}`)
  414. } else {
  415. toast.success(`${t("request.saved")}`)
  416. }
  417. })
  418. setRestReq(props.request)
  419. } catch (error) {
  420. showSaveRequestModal.value = true
  421. toast.error(`${t("error.something_went_wrong")}`)
  422. console.error(error)
  423. }
  424. }
  425. }
  426. </script>