Request.vue 5.3 KB

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