Request.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. @click="!doc ? selectRequest() : {}"
  15. >
  16. <SmartIcon
  17. class="svg-icons"
  18. :class="{ 'text-accent': isSelected }"
  19. :name="isSelected ? 'check-circle' : 'file'"
  20. />
  21. </span>
  22. <span
  23. class="flex flex-1 min-w-0 py-2 pr-2 cursor-pointer transition group-hover:text-secondaryDark"
  24. @click="!doc ? selectRequest() : {}"
  25. >
  26. <span class="truncate" :class="{ 'text-accent': isSelected }">
  27. {{ request.name }}
  28. </span>
  29. </span>
  30. <div class="flex">
  31. <ButtonSecondary
  32. v-if="!savingMode"
  33. v-tippy="{ theme: 'tooltip' }"
  34. svg="rotate-ccw"
  35. :title="$t('action.restore')"
  36. class="hidden group-hover:inline-flex"
  37. @click.native="!doc ? selectRequest() : {}"
  38. />
  39. <span>
  40. <tippy
  41. ref="options"
  42. interactive
  43. trigger="click"
  44. theme="popover"
  45. arrow
  46. :on-shown="() => tippyActions.focus()"
  47. >
  48. <template #trigger>
  49. <ButtonSecondary
  50. v-tippy="{ theme: 'tooltip' }"
  51. :title="$t('action.more')"
  52. svg="more-vertical"
  53. />
  54. </template>
  55. <div
  56. ref="tippyActions"
  57. class="flex flex-col focus:outline-none"
  58. tabindex="0"
  59. role="menu"
  60. @keyup.e="edit.$el.click()"
  61. @keyup.d="duplicate.$el.click()"
  62. @keyup.delete="deleteAction.$el.click()"
  63. @keyup.escape="options.tippy().hide()"
  64. >
  65. <SmartItem
  66. ref="edit"
  67. svg="edit"
  68. :label="`${$t('action.edit')}`"
  69. :shortcut="['E']"
  70. @click.native="
  71. () => {
  72. $emit('edit-request', {
  73. request,
  74. requestIndex,
  75. folderPath,
  76. })
  77. options.tippy().hide()
  78. }
  79. "
  80. />
  81. <SmartItem
  82. ref="duplicate"
  83. svg="copy"
  84. :label="`${$t('action.duplicate')}`"
  85. :shortcut="['D']"
  86. @click.native="
  87. () => {
  88. $emit('duplicate-request', {
  89. request,
  90. requestIndex,
  91. folderPath,
  92. })
  93. options.tippy().hide()
  94. }
  95. "
  96. />
  97. <SmartItem
  98. ref="deleteAction"
  99. svg="trash-2"
  100. :label="`${$t('action.delete')}`"
  101. :shortcut="['⌫']"
  102. @click.native="
  103. () => {
  104. confirmRemove = true
  105. options.tippy().hide()
  106. }
  107. "
  108. />
  109. </div>
  110. </tippy>
  111. </span>
  112. </div>
  113. </div>
  114. <SmartConfirmModal
  115. :show="confirmRemove"
  116. :title="`${$t('confirm.remove_request')}`"
  117. @hide-modal="confirmRemove = false"
  118. @resolve="removeRequest"
  119. />
  120. </div>
  121. </template>
  122. <script lang="ts">
  123. import { defineComponent, PropType, ref } from "@nuxtjs/composition-api"
  124. import { HoppGQLRequest, makeGQLRequest } from "@hoppscotch/data"
  125. import { cloneDeep } from "lodash"
  126. import { removeGraphqlRequest } from "~/newstore/collections"
  127. import { setGQLSession } from "~/newstore/GQLSession"
  128. export default defineComponent({
  129. props: {
  130. // Whether the object is selected (show the tick mark)
  131. picked: { type: Object, default: null },
  132. // Whether the request is being saved (activate 'select' event)
  133. savingMode: { type: Boolean, default: false },
  134. request: { type: Object as PropType<HoppGQLRequest>, default: () => {} },
  135. folderPath: { type: String, default: null },
  136. requestIndex: { type: Number, default: null },
  137. doc: Boolean,
  138. },
  139. setup() {
  140. return {
  141. tippyActions: ref<any | null>(null),
  142. options: ref<any | null>(null),
  143. edit: ref<any | null>(null),
  144. duplicate: ref<any | null>(null),
  145. deleteAction: ref<any | null>(null),
  146. }
  147. },
  148. data() {
  149. return {
  150. dragging: false,
  151. confirmRemove: false,
  152. }
  153. },
  154. computed: {
  155. isSelected(): boolean {
  156. return (
  157. this.picked &&
  158. this.picked.pickedType === "gql-my-request" &&
  159. this.picked.folderPath === this.folderPath &&
  160. this.picked.requestIndex === this.requestIndex
  161. )
  162. },
  163. },
  164. methods: {
  165. pick() {
  166. this.$emit("select", {
  167. picked: {
  168. pickedType: "gql-my-request",
  169. folderPath: this.folderPath,
  170. requestIndex: this.requestIndex,
  171. },
  172. })
  173. },
  174. selectRequest() {
  175. if (this.savingMode) {
  176. this.pick()
  177. } else {
  178. setGQLSession({
  179. request: cloneDeep(
  180. makeGQLRequest({
  181. name: this.$props.request.name,
  182. url: this.$props.request.url,
  183. query: this.$props.request.query,
  184. headers: this.$props.request.headers,
  185. variables: this.$props.request.variables,
  186. auth: this.$props.request.auth,
  187. })
  188. ),
  189. schema: "",
  190. response: "",
  191. })
  192. }
  193. },
  194. dragStart({ dataTransfer }: any) {
  195. this.dragging = !this.dragging
  196. dataTransfer.setData("folderPath", this.folderPath)
  197. dataTransfer.setData("requestIndex", this.requestIndex)
  198. },
  199. removeRequest() {
  200. // Cancel pick if the picked request is deleted
  201. if (
  202. this.picked &&
  203. this.picked.pickedType === "gql-my-request" &&
  204. this.picked.folderPath === this.folderPath &&
  205. this.picked.requestIndex === this.requestIndex
  206. ) {
  207. this.$emit("select", { picked: null })
  208. }
  209. removeGraphqlRequest(this.folderPath, this.requestIndex)
  210. this.$toast.success(`${this.$t("state.deleted")}`)
  211. },
  212. },
  213. })
  214. </script>