Collection.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <div class="flex flex-col">
  3. <div
  4. class="flex items-stretch group"
  5. @dragover.prevent
  6. @drop.prevent="dropEvent"
  7. @dragover="dragging = true"
  8. @drop="dragging = false"
  9. @dragleave="dragging = false"
  10. @dragend="dragging = false"
  11. @contextmenu.prevent="options.tippy().show()"
  12. >
  13. <span
  14. class="flex items-center justify-center px-4 cursor-pointer"
  15. @click="toggleShowChildren()"
  16. >
  17. <SmartIcon
  18. class="svg-icons"
  19. :class="{ 'text-accent': isSelected }"
  20. :name="getCollectionIcon"
  21. />
  22. </span>
  23. <span
  24. class="flex flex-1 min-w-0 py-2 pr-2 cursor-pointer transition group-hover:text-secondaryDark"
  25. @click="toggleShowChildren()"
  26. >
  27. <span class="truncate" :class="{ 'text-accent': isSelected }">
  28. {{ collection.title }}
  29. </span>
  30. </span>
  31. <div class="flex">
  32. <ButtonSecondary
  33. v-if="doc && !selected"
  34. v-tippy="{ theme: 'tooltip' }"
  35. :title="t('import.title')"
  36. svg="circle"
  37. color="green"
  38. @click.native="$emit('select-collection')"
  39. />
  40. <ButtonSecondary
  41. v-if="doc && selected"
  42. v-tippy="{ theme: 'tooltip' }"
  43. :title="t('action.remove')"
  44. svg="check-circle"
  45. color="green"
  46. @click.native="$emit('unselect-collection')"
  47. />
  48. <ButtonSecondary
  49. v-if="collectionsType.selectedTeam.myRole !== 'VIEWER'"
  50. v-tippy="{ theme: 'tooltip' }"
  51. svg="folder-plus"
  52. :title="t('folder.new')"
  53. class="hidden group-hover:inline-flex"
  54. @click.native="
  55. $emit('add-folder', {
  56. folder: collection,
  57. path: `${collectionIndex}`,
  58. })
  59. "
  60. />
  61. <span>
  62. <tippy
  63. v-if="collectionsType.selectedTeam.myRole !== 'VIEWER'"
  64. ref="options"
  65. interactive
  66. trigger="click"
  67. theme="popover"
  68. arrow
  69. :on-shown="() => tippyActions.focus()"
  70. >
  71. <template #trigger>
  72. <ButtonSecondary
  73. v-tippy="{ theme: 'tooltip' }"
  74. :title="t('action.more')"
  75. svg="more-vertical"
  76. />
  77. </template>
  78. <div
  79. ref="tippyActions"
  80. class="flex flex-col focus:outline-none"
  81. tabindex="0"
  82. role="menu"
  83. @keyup.n="folderAction.$el.click()"
  84. @keyup.e="edit.$el.click()"
  85. @keyup.delete="deleteAction.$el.click()"
  86. @keyup.x="exportAction.$el.click()"
  87. @keyup.escape="options.tippy().hide()"
  88. >
  89. <SmartItem
  90. ref="folderAction"
  91. svg="folder-plus"
  92. :label="t('folder.new')"
  93. :shortcut="['N']"
  94. @click.native="
  95. () => {
  96. $emit('add-folder', {
  97. folder: collection,
  98. path: `${collectionIndex}`,
  99. })
  100. options.tippy().hide()
  101. }
  102. "
  103. />
  104. <SmartItem
  105. ref="edit"
  106. svg="edit"
  107. :label="t('action.edit')"
  108. :shortcut="['E']"
  109. @click.native="
  110. () => {
  111. $emit('edit-collection')
  112. options.tippy().hide()
  113. }
  114. "
  115. />
  116. <SmartItem
  117. ref="exportAction"
  118. svg="download"
  119. :label="$t('export.title')"
  120. :shortcut="['X']"
  121. :loading="exportLoading"
  122. @click.native="exportCollection"
  123. />
  124. <SmartItem
  125. ref="deleteAction"
  126. svg="trash-2"
  127. :label="t('action.delete')"
  128. :shortcut="['⌫']"
  129. @click.native="
  130. () => {
  131. confirmRemove = true
  132. options.tippy().hide()
  133. }
  134. "
  135. />
  136. </div>
  137. </tippy>
  138. </span>
  139. </div>
  140. </div>
  141. <div v-if="showChildren || isFiltered" class="flex">
  142. <div
  143. class="bg-dividerLight cursor-nsResize flex ml-5.5 transform transition w-1 hover:bg-dividerDark hover:scale-x-125"
  144. @click="toggleShowChildren()"
  145. ></div>
  146. <div class="flex flex-col flex-1 truncate">
  147. <CollectionsTeamsFolder
  148. v-for="(folder, index) in collection.children"
  149. :key="`folder-${index}`"
  150. :folder="folder"
  151. :folder-index="index"
  152. :folder-path="`${collectionIndex}/${index}`"
  153. :collection-index="collectionIndex"
  154. :doc="doc"
  155. :save-request="saveRequest"
  156. :collections-type="collectionsType"
  157. :is-filtered="isFiltered"
  158. :picked="picked"
  159. :loading-collection-i-ds="loadingCollectionIDs"
  160. @add-folder="$emit('add-folder', $event)"
  161. @edit-folder="$emit('edit-folder', $event)"
  162. @edit-request="$emit('edit-request', $event)"
  163. @select="$emit('select', $event)"
  164. @expand-collection="expandCollection"
  165. @remove-request="removeRequest"
  166. @duplicate-request="$emit('duplicate-request', $event)"
  167. />
  168. <CollectionsTeamsRequest
  169. v-for="(request, index) in collection.requests"
  170. :key="`request-${index}`"
  171. :request="request.request"
  172. :collection-index="collectionIndex"
  173. :folder-index="-1"
  174. :folder-name="collection.name"
  175. :request-index="request.id"
  176. :doc="doc"
  177. :save-request="saveRequest"
  178. :collection-i-d="collection.id"
  179. :collections-type="collectionsType"
  180. :picked="picked"
  181. @edit-request="editRequest($event)"
  182. @select="$emit('select', $event)"
  183. @remove-request="removeRequest"
  184. @duplicate-request="$emit('duplicate-request', $event)"
  185. />
  186. <div
  187. v-if="loadingCollectionIDs.includes(collection.id)"
  188. class="flex flex-col items-center justify-center p-4"
  189. >
  190. <SmartSpinner class="my-4" />
  191. <span class="text-secondaryLight">{{ $t("state.loading") }}</span>
  192. </div>
  193. <div
  194. v-else-if="
  195. (collection.children == undefined ||
  196. collection.children.length === 0) &&
  197. (collection.requests == undefined ||
  198. collection.requests.length === 0)
  199. "
  200. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  201. >
  202. <img
  203. :src="`/images/states/${$colorMode.value}/pack.svg`"
  204. loading="lazy"
  205. class="inline-flex flex-col object-contain object-center w-16 h-16 mb-4"
  206. :alt="`${t('empty.collection')}`"
  207. />
  208. <span class="text-center">
  209. {{ t("empty.collection") }}
  210. </span>
  211. </div>
  212. </div>
  213. </div>
  214. <SmartConfirmModal
  215. :show="confirmRemove"
  216. :title="t('confirm.remove_collection')"
  217. @hide-modal="confirmRemove = false"
  218. @resolve="removeCollection"
  219. />
  220. </div>
  221. </template>
  222. <script lang="ts">
  223. import { defineComponent, ref } from "@nuxtjs/composition-api"
  224. import * as E from "fp-ts/Either"
  225. import {
  226. getCompleteCollectionTree,
  227. teamCollToHoppRESTColl,
  228. } from "~/helpers/backend/helpers"
  229. import { moveRESTTeamRequest } from "~/helpers/backend/mutations/TeamRequest"
  230. import { useI18n } from "~/helpers/utils/composables"
  231. export default defineComponent({
  232. props: {
  233. collectionIndex: { type: Number, default: null },
  234. collection: { type: Object, default: () => {} },
  235. doc: Boolean,
  236. isFiltered: Boolean,
  237. selected: Boolean,
  238. saveRequest: Boolean,
  239. collectionsType: { type: Object, default: () => {} },
  240. picked: { type: Object, default: () => {} },
  241. loadingCollectionIDs: { type: Array, default: () => [] },
  242. },
  243. setup() {
  244. const t = useI18n()
  245. return {
  246. tippyActions: ref<any | null>(null),
  247. options: ref<any | null>(null),
  248. folderAction: ref<any | null>(null),
  249. edit: ref<any | null>(null),
  250. deleteAction: ref<any | null>(null),
  251. exportAction: ref<any | null>(null),
  252. exportLoading: ref<boolean>(false),
  253. t,
  254. }
  255. },
  256. data() {
  257. return {
  258. showChildren: false,
  259. dragging: false,
  260. selectedFolder: {},
  261. confirmRemove: false,
  262. prevCursor: "",
  263. cursor: "",
  264. pageNo: 0,
  265. }
  266. },
  267. computed: {
  268. isSelected(): boolean {
  269. return (
  270. this.picked &&
  271. this.picked.pickedType === "teams-collection" &&
  272. this.picked.collectionID === this.collection.id
  273. )
  274. },
  275. getCollectionIcon() {
  276. if (this.isSelected) return "check-circle"
  277. else if (!this.showChildren && !this.isFiltered) return "folder"
  278. else if (this.showChildren || this.isFiltered) return "folder-open"
  279. else return "folder"
  280. },
  281. },
  282. methods: {
  283. async exportCollection() {
  284. this.exportLoading = true
  285. const result = await getCompleteCollectionTree(this.collection.id)()
  286. if (E.isLeft(result)) {
  287. this.$toast.error(this.$t("error.something_went_wrong").toString())
  288. console.log(result.left)
  289. this.exportLoading = false
  290. this.options.tippy().hide()
  291. return
  292. }
  293. const hoppColl = teamCollToHoppRESTColl(result.right)
  294. const collectionJSON = JSON.stringify(hoppColl)
  295. const file = new Blob([collectionJSON], { type: "application/json" })
  296. const a = document.createElement("a")
  297. const url = URL.createObjectURL(file)
  298. a.href = url
  299. a.download = `${hoppColl.name}.json`
  300. document.body.appendChild(a)
  301. a.click()
  302. this.$toast.success(this.$t("state.download_started").toString())
  303. setTimeout(() => {
  304. document.body.removeChild(a)
  305. URL.revokeObjectURL(url)
  306. }, 1000)
  307. this.exportLoading = false
  308. this.options.tippy().hide()
  309. },
  310. editRequest(event: any) {
  311. this.$emit("edit-request", event)
  312. if (this.$props.saveRequest)
  313. this.$emit("select", {
  314. picked: {
  315. pickedType: "teams-collection",
  316. collectionID: this.collection.id,
  317. },
  318. })
  319. },
  320. toggleShowChildren() {
  321. if (this.$props.saveRequest)
  322. this.$emit("select", {
  323. picked: {
  324. pickedType: "teams-collection",
  325. collectionID: this.collection.id,
  326. },
  327. })
  328. this.$emit("expand-collection", this.collection.id)
  329. this.showChildren = !this.showChildren
  330. },
  331. removeCollection() {
  332. this.$emit("remove-collection", {
  333. collectionsType: this.collectionsType,
  334. collectionIndex: this.collectionIndex,
  335. collectionID: this.collection.id,
  336. })
  337. },
  338. expandCollection(collectionID: string) {
  339. this.$emit("expand-collection", collectionID)
  340. },
  341. async dropEvent({ dataTransfer }: any) {
  342. this.dragging = !this.dragging
  343. const requestIndex = dataTransfer.getData("requestIndex")
  344. const moveRequestResult = await moveRESTTeamRequest(
  345. requestIndex,
  346. this.collection.id
  347. )()
  348. if (E.isLeft(moveRequestResult))
  349. this.$toast.error(`${this.$t("error.something_went_wrong")}`)
  350. },
  351. removeRequest({ collectionIndex, folderName, requestIndex }: any) {
  352. this.$emit("remove-request", {
  353. collectionIndex,
  354. folderName,
  355. requestIndex,
  356. })
  357. },
  358. },
  359. })
  360. </script>