Request.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. () => {
  72. $emit('edit-request', {
  73. request,
  74. requestIndex,
  75. folderPath,
  76. })
  77. $refs.options.tippy().hide()
  78. }
  79. "
  80. />
  81. <SmartItem
  82. svg="trash-2"
  83. color="red"
  84. :label="`${$t('action.delete')}`"
  85. @click.native="
  86. () => {
  87. confirmRemove = true
  88. $refs.options.tippy().hide()
  89. }
  90. "
  91. />
  92. </tippy>
  93. </span>
  94. </div>
  95. </div>
  96. <SmartConfirmModal
  97. :show="confirmRemove"
  98. :title="`${$t('confirm.remove_request')}`"
  99. @hide-modal="confirmRemove = false"
  100. @resolve="removeRequest"
  101. />
  102. </div>
  103. </template>
  104. <script lang="ts">
  105. import { defineComponent, PropType } from "@nuxtjs/composition-api"
  106. import { HoppGQLRequest, makeGQLRequest } from "~/helpers/types/HoppGQLRequest"
  107. import { removeGraphqlRequest } from "~/newstore/collections"
  108. import { setGQLSession } from "~/newstore/GQLSession"
  109. export default defineComponent({
  110. props: {
  111. // Whether the object is selected (show the tick mark)
  112. picked: { type: Object, default: null },
  113. // Whether the request is being saved (activate 'select' event)
  114. savingMode: { type: Boolean, default: false },
  115. request: { type: Object as PropType<HoppGQLRequest>, default: () => {} },
  116. folderPath: { type: String, default: null },
  117. requestIndex: { type: Number, default: null },
  118. doc: Boolean,
  119. },
  120. data() {
  121. return {
  122. dragging: false,
  123. confirmRemove: false,
  124. }
  125. },
  126. computed: {
  127. isSelected(): boolean {
  128. return (
  129. this.picked &&
  130. this.picked.pickedType === "gql-my-request" &&
  131. this.picked.folderPath === this.folderPath &&
  132. this.picked.requestIndex === this.requestIndex
  133. )
  134. },
  135. },
  136. methods: {
  137. pick() {
  138. this.$emit("select", {
  139. picked: {
  140. pickedType: "gql-my-request",
  141. folderPath: this.folderPath,
  142. requestIndex: this.requestIndex,
  143. },
  144. })
  145. },
  146. selectRequest() {
  147. if (this.savingMode) {
  148. this.pick()
  149. } else {
  150. setGQLSession({
  151. request: makeGQLRequest({
  152. name: this.$props.request.name,
  153. url: this.$props.request.url,
  154. query: this.$props.request.query,
  155. headers: this.$props.request.headers,
  156. variables: this.$props.request.variables,
  157. }),
  158. schema: "",
  159. response: "",
  160. })
  161. }
  162. },
  163. dragStart({ dataTransfer }: any) {
  164. this.dragging = !this.dragging
  165. dataTransfer.setData("folderPath", this.folderPath)
  166. dataTransfer.setData("requestIndex", this.requestIndex)
  167. },
  168. removeRequest() {
  169. // Cancel pick if the picked request is deleted
  170. if (
  171. this.picked &&
  172. this.picked.pickedType === "gql-my-request" &&
  173. this.picked.folderPath === this.folderPath &&
  174. this.picked.requestIndex === this.requestIndex
  175. ) {
  176. this.$emit("select", { picked: null })
  177. }
  178. removeGraphqlRequest(this.folderPath, this.requestIndex)
  179. this.$toast.success(`${this.$t("state.deleted")}`, {
  180. icon: "delete",
  181. })
  182. },
  183. },
  184. })
  185. </script>