Request.vue 12 KB

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