Request.vue 5.0 KB

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