Request.vue 5.3 KB

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