Request.vue 5.5 KB

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