index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <template>
  2. <AppSection
  3. label="collections"
  4. :class="{ 'rounded border border-divider': saveRequest }"
  5. >
  6. <div
  7. class="
  8. divide-y divide-dividerLight
  9. bg-primary
  10. border-b border-dividerLight
  11. rounded-t
  12. flex flex-col
  13. top-0
  14. z-10
  15. sticky
  16. "
  17. >
  18. <div v-if="!saveRequest" class="search-wrappe">
  19. <input
  20. v-model="filterText"
  21. type="search"
  22. autocomplete="off"
  23. :placeholder="$t('action.search')"
  24. class="bg-transparent flex w-full py-2 pr-2 pl-4"
  25. />
  26. </div>
  27. <CollectionsChooseType
  28. :collections-type="collectionsType"
  29. :show="showTeamCollections"
  30. :doc="doc"
  31. @update-collection-type="updateCollectionType"
  32. @update-selected-team="updateSelectedTeam"
  33. />
  34. <div class="flex flex-1 justify-between">
  35. <ButtonSecondary
  36. v-if="
  37. collectionsType.type == 'team-collections' &&
  38. (collectionsType.selectedTeam == undefined ||
  39. collectionsType.selectedTeam.myRole == 'VIEWER')
  40. "
  41. v-tippy="{ theme: 'tooltip' }"
  42. disabled
  43. class="!rounded-none"
  44. svg="plus"
  45. :title="$t('team.no_access')"
  46. :label="$t('action.new')"
  47. />
  48. <ButtonSecondary
  49. v-else
  50. svg="plus"
  51. :label="$t('action.new')"
  52. class="!rounded-none"
  53. @click.native="displayModalAdd(true)"
  54. />
  55. <span class="flex">
  56. <ButtonSecondary
  57. v-tippy="{ theme: 'tooltip' }"
  58. to="https://docs.hoppscotch.io/features/collections"
  59. blank
  60. :title="$t('app.wiki')"
  61. svg="help-circle"
  62. />
  63. <ButtonSecondary
  64. v-if="!saveRequest"
  65. v-tippy="{ theme: 'tooltip' }"
  66. :disabled="
  67. collectionsType.type == 'team-collections' &&
  68. collectionsType.selectedTeam == undefined
  69. "
  70. svg="archive"
  71. :title="$t('modal.import_export')"
  72. @click.native="displayModalImportExport(true)"
  73. />
  74. </span>
  75. </div>
  76. </div>
  77. <div class="flex flex-col">
  78. <component
  79. :is="
  80. collectionsType.type == 'my-collections'
  81. ? 'CollectionsMyCollection'
  82. : 'CollectionsTeamsCollection'
  83. "
  84. v-for="(collection, index) in filteredCollections"
  85. :key="`collection-${index}`"
  86. :collection-index="index"
  87. :collection="collection"
  88. :doc="doc"
  89. :is-filtered="filterText.length > 0"
  90. :selected="selected.some((coll) => coll == collection)"
  91. :save-request="saveRequest"
  92. :collections-type="collectionsType"
  93. :picked="picked"
  94. @edit-collection="editCollection(collection, index)"
  95. @add-folder="addFolder($event)"
  96. @edit-folder="editFolder($event)"
  97. @edit-request="editRequest($event)"
  98. @duplicate-request="duplicateRequest($event)"
  99. @update-team-collections="updateTeamCollections"
  100. @select-collection="$emit('use-collection', collection)"
  101. @unselect-collection="$emit('remove-collection', collection)"
  102. @select="$emit('select', $event)"
  103. @expand-collection="expandCollection"
  104. @remove-collection="removeCollection"
  105. @remove-request="removeRequest"
  106. />
  107. </div>
  108. <div
  109. v-if="filteredCollections.length === 0 && filterText.length === 0"
  110. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  111. >
  112. <img
  113. :src="`/images/states/${$colorMode.value}/pack.svg`"
  114. loading="lazy"
  115. class="flex-col my-4 object-contain object-center h-16 w-16 inline-flex"
  116. :alt="$t('empty.collections')"
  117. />
  118. <span class="text-center pb-4">
  119. {{ $t("empty.collections") }}
  120. </span>
  121. <ButtonSecondary
  122. v-if="
  123. collectionsType.type == 'team-collections' &&
  124. (collectionsType.selectedTeam == undefined ||
  125. collectionsType.selectedTeam.myRole == 'VIEWER')
  126. "
  127. v-tippy="{ theme: 'tooltip' }"
  128. :title="$t('team.no_access')"
  129. :label="$t('add.new')"
  130. class="mb-4"
  131. filled
  132. />
  133. <ButtonSecondary
  134. v-else
  135. :label="$t('add.new')"
  136. filled
  137. class="mb-4"
  138. @click.native="displayModalAdd(true)"
  139. />
  140. </div>
  141. <div
  142. v-if="filterText.length !== 0 && filteredCollections.length === 0"
  143. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  144. >
  145. <i class="opacity-75 pb-2 material-icons">manage_search</i>
  146. <span class="text-center">
  147. {{ $t("state.nothing_found") }} "{{ filterText }}"
  148. </span>
  149. </div>
  150. <CollectionsAdd
  151. :show="showModalAdd"
  152. @submit="addNewRootCollection"
  153. @hide-modal="displayModalAdd(false)"
  154. />
  155. <CollectionsEdit
  156. :show="showModalEdit"
  157. :editing-coll-name="editingCollection ? editingCollection.name : ''"
  158. :placeholder-coll-name="editingCollection ? editingCollection.name : ''"
  159. @hide-modal="displayModalEdit(false)"
  160. @submit="updateEditingCollection"
  161. />
  162. <CollectionsAddFolder
  163. :show="showModalAddFolder"
  164. :folder="editingFolder"
  165. :folder-path="editingFolderPath"
  166. @add-folder="onAddFolder($event)"
  167. @hide-modal="displayModalAddFolder(false)"
  168. />
  169. <CollectionsEditFolder
  170. :show="showModalEditFolder"
  171. @submit="updateEditingFolder"
  172. @hide-modal="displayModalEditFolder(false)"
  173. />
  174. <CollectionsEditRequest
  175. :show="showModalEditRequest"
  176. :placeholder-req-name="editingRequest ? editingRequest.name : ''"
  177. @submit="updateEditingRequest"
  178. @hide-modal="displayModalEditRequest(false)"
  179. />
  180. <CollectionsImportExport
  181. :show="showModalImportExport"
  182. :collections-type="collectionsType"
  183. @hide-modal="displayModalImportExport(false)"
  184. @update-team-collections="updateTeamCollections"
  185. />
  186. </AppSection>
  187. </template>
  188. <script>
  189. import gql from "graphql-tag"
  190. import cloneDeep from "lodash/cloneDeep"
  191. import { defineComponent } from "@nuxtjs/composition-api"
  192. import CollectionsMyCollection from "./my/Collection.vue"
  193. import CollectionsTeamsCollection from "./teams/Collection.vue"
  194. import { currentUser$ } from "~/helpers/fb/auth"
  195. import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter"
  196. import * as teamUtils from "~/helpers/teams/utils"
  197. import {
  198. restCollections$,
  199. addRESTCollection,
  200. editRESTCollection,
  201. addRESTFolder,
  202. removeRESTCollection,
  203. editRESTFolder,
  204. removeRESTRequest,
  205. editRESTRequest,
  206. saveRESTRequestAs,
  207. } from "~/newstore/collections"
  208. import {
  209. useReadonlyStream,
  210. useStreamSubscriber,
  211. } from "~/helpers/utils/composables"
  212. export default defineComponent({
  213. components: {
  214. CollectionsMyCollection,
  215. CollectionsTeamsCollection,
  216. },
  217. props: {
  218. doc: Boolean,
  219. selected: { type: Array, default: () => [] },
  220. saveRequest: Boolean,
  221. picked: { type: Object, default: () => {} },
  222. },
  223. setup() {
  224. const { subscribeToStream } = useStreamSubscriber()
  225. return {
  226. subscribeTo: subscribeToStream,
  227. collections: useReadonlyStream(restCollections$, []),
  228. currentUser: useReadonlyStream(currentUser$, null),
  229. }
  230. },
  231. data() {
  232. return {
  233. showModalAdd: false,
  234. showModalEdit: false,
  235. showModalImportExport: false,
  236. showModalAddFolder: false,
  237. showModalEditFolder: false,
  238. showModalEditRequest: false,
  239. editingCollection: undefined,
  240. editingCollectionIndex: undefined,
  241. editingFolder: undefined,
  242. editingFolderName: undefined,
  243. editingFolderIndex: undefined,
  244. editingFolderPath: undefined,
  245. editingRequest: undefined,
  246. editingRequestIndex: undefined,
  247. filterText: "",
  248. collectionsType: {
  249. type: "my-collections",
  250. selectedTeam: undefined,
  251. },
  252. teamCollectionAdapter: new TeamCollectionAdapter(null),
  253. teamCollectionsNew: [],
  254. }
  255. },
  256. computed: {
  257. showTeamCollections() {
  258. if (this.currentUser == null) {
  259. return false
  260. }
  261. return true
  262. },
  263. filteredCollections() {
  264. const collections =
  265. this.collectionsType.type === "my-collections"
  266. ? this.collections
  267. : this.teamCollectionsNew
  268. if (!this.filterText) {
  269. return collections
  270. }
  271. if (this.collectionsType.type === "team-collections") {
  272. return []
  273. }
  274. const filterText = this.filterText.toLowerCase()
  275. const filteredCollections = []
  276. for (const collection of collections) {
  277. const filteredRequests = []
  278. const filteredFolders = []
  279. for (const request of collection.requests) {
  280. if (request.name.toLowerCase().includes(filterText))
  281. filteredRequests.push(request)
  282. }
  283. for (const folder of this.collectionsType.type === "team-collections"
  284. ? collection.children
  285. : collection.folders) {
  286. const filteredFolderRequests = []
  287. for (const request of folder.requests) {
  288. if (request.name.toLowerCase().includes(filterText))
  289. filteredFolderRequests.push(request)
  290. }
  291. if (filteredFolderRequests.length > 0) {
  292. const filteredFolder = Object.assign({}, folder)
  293. filteredFolder.requests = filteredFolderRequests
  294. filteredFolders.push(filteredFolder)
  295. }
  296. }
  297. if (
  298. filteredRequests.length + filteredFolders.length > 0 ||
  299. collection.name.toLowerCase().includes(filterText)
  300. ) {
  301. const filteredCollection = Object.assign({}, collection)
  302. filteredCollection.requests = filteredRequests
  303. filteredCollection.folders = filteredFolders
  304. filteredCollections.push(filteredCollection)
  305. }
  306. }
  307. return filteredCollections
  308. },
  309. },
  310. watch: {
  311. "collectionsType.type": function emitstuff() {
  312. this.$emit("update-collection", this.$data.collectionsType.type)
  313. },
  314. "collectionsType.selectedTeam"(value) {
  315. if (value?.id) this.teamCollectionAdapter.changeTeamID(value.id)
  316. },
  317. },
  318. mounted() {
  319. this.subscribeTo(this.teamCollectionAdapter.collections$, (colls) => {
  320. this.teamCollectionsNew = cloneDeep(colls)
  321. })
  322. },
  323. methods: {
  324. updateTeamCollections() {
  325. // TODO: Remove this at some point
  326. },
  327. updateSelectedTeam(newSelectedTeam) {
  328. this.collectionsType.selectedTeam = newSelectedTeam
  329. this.$emit("update-coll-type", this.collectionsType)
  330. },
  331. updateCollectionType(newCollectionType) {
  332. this.collectionsType.type = newCollectionType
  333. this.$emit("update-coll-type", this.collectionsType)
  334. },
  335. // Intented to be called by the CollectionAdd modal submit event
  336. addNewRootCollection(name) {
  337. if (this.collectionsType.type === "my-collections") {
  338. addRESTCollection({
  339. name,
  340. folders: [],
  341. requests: [],
  342. })
  343. } else if (
  344. this.collectionsType.type === "team-collections" &&
  345. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  346. ) {
  347. teamUtils
  348. .createNewRootCollection(
  349. this.$apollo,
  350. name,
  351. this.collectionsType.selectedTeam.id
  352. )
  353. .then(() => {
  354. this.$toast.success(this.$t("collection.created"))
  355. })
  356. .catch((e) => {
  357. this.$toast.error(this.$t("error.something_went_wrong"))
  358. console.error(e)
  359. })
  360. }
  361. this.displayModalAdd(false)
  362. },
  363. // Intented to be called by CollectionEdit modal submit event
  364. updateEditingCollection(newName) {
  365. if (!newName) {
  366. this.$toast.error(this.$t("collection.invalid_name"))
  367. return
  368. }
  369. if (this.collectionsType.type === "my-collections") {
  370. const collectionUpdated = {
  371. ...this.editingCollection,
  372. name: newName,
  373. }
  374. editRESTCollection(this.editingCollectionIndex, collectionUpdated)
  375. } else if (
  376. this.collectionsType.type === "team-collections" &&
  377. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  378. ) {
  379. teamUtils
  380. .renameCollection(this.$apollo, newName, this.editingCollection.id)
  381. .then(() => {
  382. this.$toast.success(this.$t("collection.renamed"))
  383. })
  384. .catch((e) => {
  385. this.$toast.error(this.$t("error.something_went_wrong"))
  386. console.error(e)
  387. })
  388. }
  389. this.displayModalEdit(false)
  390. },
  391. // Intended to be called by CollectionEditFolder modal submit event
  392. updateEditingFolder(name) {
  393. if (this.collectionsType.type === "my-collections") {
  394. editRESTFolder(this.editingFolderPath, { ...this.editingFolder, name })
  395. } else if (
  396. this.collectionsType.type === "team-collections" &&
  397. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  398. ) {
  399. teamUtils
  400. .renameCollection(this.$apollo, name, this.editingFolder.id)
  401. .then(() => {
  402. this.$toast.success(this.$t("folder.renamed"))
  403. })
  404. .catch((e) => {
  405. this.$toast.error(this.$t("error.something_went_wrong"))
  406. console.error(e)
  407. })
  408. }
  409. this.displayModalEditFolder(false)
  410. },
  411. // Intented to by called by CollectionsEditRequest modal submit event
  412. updateEditingRequest(requestUpdateData) {
  413. const requestUpdated = {
  414. ...this.editingRequest,
  415. name: requestUpdateData.name || this.editingRequest.name,
  416. }
  417. if (this.collectionsType.type === "my-collections") {
  418. editRESTRequest(
  419. this.editingFolderPath,
  420. this.editingRequestIndex,
  421. requestUpdated
  422. )
  423. } else if (
  424. this.collectionsType.type === "team-collections" &&
  425. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  426. ) {
  427. const requestName = requestUpdateData.name || this.editingRequest.name
  428. teamUtils
  429. .updateRequest(
  430. this.$apollo,
  431. requestUpdated,
  432. requestName,
  433. this.editingRequestIndex
  434. )
  435. .then(() => {
  436. this.$toast.success(this.$t("request.renamed"))
  437. this.$emit("update-team-collections")
  438. })
  439. .catch((e) => {
  440. this.$toast.error(this.$t("error.something_went_wrong"))
  441. console.error(e)
  442. })
  443. }
  444. this.displayModalEditRequest(false)
  445. },
  446. displayModalAdd(shouldDisplay) {
  447. this.showModalAdd = shouldDisplay
  448. },
  449. displayModalEdit(shouldDisplay) {
  450. this.showModalEdit = shouldDisplay
  451. if (!shouldDisplay) this.resetSelectedData()
  452. },
  453. displayModalImportExport(shouldDisplay) {
  454. this.showModalImportExport = shouldDisplay
  455. },
  456. displayModalAddFolder(shouldDisplay) {
  457. this.showModalAddFolder = shouldDisplay
  458. if (!shouldDisplay) this.resetSelectedData()
  459. },
  460. displayModalEditFolder(shouldDisplay) {
  461. this.showModalEditFolder = shouldDisplay
  462. if (!shouldDisplay) this.resetSelectedData()
  463. },
  464. displayModalEditRequest(shouldDisplay) {
  465. this.showModalEditRequest = shouldDisplay
  466. if (!shouldDisplay) this.resetSelectedData()
  467. },
  468. editCollection(collection, collectionIndex) {
  469. this.$data.editingCollection = collection
  470. this.$data.editingCollectionIndex = collectionIndex
  471. this.displayModalEdit(true)
  472. },
  473. onAddFolder({ name, folder, path }) {
  474. if (this.collectionsType.type === "my-collections") {
  475. addRESTFolder(name, path)
  476. } else if (this.collectionsType.type === "team-collections") {
  477. if (this.collectionsType.selectedTeam.myRole !== "VIEWER") {
  478. this.$apollo
  479. .mutate({
  480. mutation: gql`
  481. mutation CreateChildCollection(
  482. $childTitle: String!
  483. $collectionID: ID!
  484. ) {
  485. createChildCollection(
  486. childTitle: $childTitle
  487. collectionID: $collectionID
  488. ) {
  489. id
  490. }
  491. }
  492. `,
  493. // Parameters
  494. variables: {
  495. childTitle: name,
  496. collectionID: folder.id,
  497. },
  498. })
  499. .then(() => {
  500. this.$toast.success(this.$t("folder.created"))
  501. this.$emit("update-team-collections")
  502. })
  503. .catch((e) => {
  504. this.$toast.error(this.$t("error.something_went_wrong"))
  505. console.error(e)
  506. })
  507. }
  508. }
  509. this.displayModalAddFolder(false)
  510. },
  511. addFolder(payload) {
  512. const { folder, path } = payload
  513. this.$data.editingFolder = folder
  514. this.$data.editingFolderPath = path
  515. this.displayModalAddFolder(true)
  516. },
  517. editFolder(payload) {
  518. const { collectionIndex, folder, folderIndex, folderPath } = payload
  519. this.$data.editingCollectionIndex = collectionIndex
  520. this.$data.editingFolder = folder
  521. this.$data.editingFolderIndex = folderIndex
  522. this.$data.editingFolderPath = folderPath
  523. this.$data.collectionsType = this.collectionsType
  524. this.displayModalEditFolder(true)
  525. },
  526. editRequest(payload) {
  527. const {
  528. collectionIndex,
  529. folderIndex,
  530. folderName,
  531. request,
  532. requestIndex,
  533. folderPath,
  534. } = payload
  535. this.$data.editingCollectionIndex = collectionIndex
  536. this.$data.editingFolderIndex = folderIndex
  537. this.$data.editingFolderName = folderName
  538. this.$data.editingRequest = request
  539. this.$data.editingRequestIndex = requestIndex
  540. this.editingFolderPath = folderPath
  541. this.$emit("select-request", requestIndex)
  542. this.displayModalEditRequest(true)
  543. },
  544. resetSelectedData() {
  545. this.$data.editingCollection = undefined
  546. this.$data.editingCollectionIndex = undefined
  547. this.$data.editingFolder = undefined
  548. this.$data.editingFolderIndex = undefined
  549. this.$data.editingRequest = undefined
  550. this.$data.editingRequestIndex = undefined
  551. },
  552. expandCollection(collectionID) {
  553. this.teamCollectionAdapter.expandCollection(collectionID)
  554. },
  555. removeCollection({ collectionsType, collectionIndex, collectionID }) {
  556. if (collectionsType.type === "my-collections") {
  557. // Cancel pick if picked collection is deleted
  558. if (
  559. this.picked &&
  560. this.picked.pickedType === "my-collection" &&
  561. this.picked.collectionIndex === collectionIndex
  562. ) {
  563. this.$emit("select", { picked: null })
  564. }
  565. removeRESTCollection(collectionIndex)
  566. this.$toast.success(this.$t("state.deleted"))
  567. } else if (collectionsType.type === "team-collections") {
  568. // Cancel pick if picked collection is deleted
  569. if (
  570. this.picked &&
  571. this.picked.pickedType === "teams-collection" &&
  572. this.picked.collectionID === collectionID
  573. ) {
  574. this.$emit("select", { picked: null })
  575. }
  576. if (collectionsType.selectedTeam.myRole !== "VIEWER") {
  577. this.$apollo
  578. .mutate({
  579. // Query
  580. mutation: gql`
  581. mutation ($collectionID: ID!) {
  582. deleteCollection(collectionID: $collectionID)
  583. }
  584. `,
  585. // Parameters
  586. variables: {
  587. collectionID,
  588. },
  589. })
  590. .then(() => {
  591. this.$toast.success(this.$t("state.deleted"))
  592. })
  593. .catch((e) => {
  594. this.$toast.error(this.$t("error.something_went_wrong"))
  595. console.error(e)
  596. })
  597. }
  598. }
  599. },
  600. removeRequest({ requestIndex, folderPath }) {
  601. if (this.collectionsType.type === "my-collections") {
  602. // Cancel pick if the picked item is being deleted
  603. if (
  604. this.picked &&
  605. this.picked.pickedType === "my-request" &&
  606. this.picked.folderPath === folderPath &&
  607. this.picked.requestIndex === requestIndex
  608. ) {
  609. this.$emit("select", { picked: null })
  610. }
  611. removeRESTRequest(folderPath, requestIndex)
  612. this.$toast.success(this.$t("state.deleted"))
  613. } else if (this.collectionsType.type === "team-collections") {
  614. // Cancel pick if the picked item is being deleted
  615. if (
  616. this.picked &&
  617. this.picked.pickedType === "teams-request" &&
  618. this.picked.requestID === requestIndex
  619. ) {
  620. this.$emit("select", { picked: null })
  621. }
  622. teamUtils
  623. .deleteRequest(this.$apollo, requestIndex)
  624. .then(() => {
  625. this.$toast.success(this.$t("state.deleted"))
  626. })
  627. .catch((e) => {
  628. this.$toast.error(this.$t("error.something_went_wrong"))
  629. console.error(e)
  630. })
  631. }
  632. },
  633. duplicateRequest({ folderPath, request }) {
  634. saveRESTRequestAs(folderPath, {
  635. ...cloneDeep(request),
  636. name: `${request.name} - ${this.$t("action.duplicate")}`,
  637. })
  638. },
  639. },
  640. })
  641. </script>