index.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. <template>
  2. <div :class="{ 'rounded border border-divider': saveRequest }">
  3. <div
  4. class="sticky z-10 flex flex-col border-b rounded-t divide-y divide-dividerLight bg-primary border-dividerLight"
  5. :style="saveRequest ? 'top: calc(-1 * var(--font-size-body))' : 'top: 0'"
  6. >
  7. <div v-if="!saveRequest" class="flex flex-col">
  8. <input
  9. v-model="filterText"
  10. type="search"
  11. autocomplete="off"
  12. :placeholder="$t('action.search')"
  13. class="py-2 pl-4 pr-2 bg-transparent"
  14. :disabled="collectionsType.type == 'team-collections'"
  15. />
  16. </div>
  17. <CollectionsChooseType
  18. :collections-type="collectionsType"
  19. :show="showTeamCollections"
  20. :doc="doc"
  21. @update-collection-type="updateCollectionType"
  22. @update-selected-team="updateSelectedTeam"
  23. />
  24. <div class="flex justify-between flex-1">
  25. <ButtonSecondary
  26. v-if="
  27. collectionsType.type == 'team-collections' &&
  28. (collectionsType.selectedTeam == undefined ||
  29. collectionsType.selectedTeam.myRole == 'VIEWER')
  30. "
  31. v-tippy="{ theme: 'tooltip' }"
  32. disabled
  33. class="!rounded-none"
  34. svg="plus"
  35. :title="$t('team.no_access')"
  36. :label="$t('action.new')"
  37. />
  38. <ButtonSecondary
  39. v-else
  40. svg="plus"
  41. :label="$t('action.new')"
  42. class="!rounded-none"
  43. @click.native="displayModalAdd(true)"
  44. />
  45. <span class="flex">
  46. <ButtonSecondary
  47. v-tippy="{ theme: 'tooltip' }"
  48. to="https://docs.hoppscotch.io/features/collections"
  49. blank
  50. :title="$t('app.wiki')"
  51. svg="help-circle"
  52. />
  53. <ButtonSecondary
  54. v-if="!saveRequest"
  55. v-tippy="{ theme: 'tooltip' }"
  56. :disabled="
  57. collectionsType.type == 'team-collections' &&
  58. collectionsType.selectedTeam == undefined
  59. "
  60. svg="archive"
  61. :title="$t('modal.import_export')"
  62. @click.native="displayModalImportExport(true)"
  63. />
  64. </span>
  65. </div>
  66. </div>
  67. <div class="flex flex-col flex-1">
  68. <component
  69. :is="
  70. collectionsType.type == 'my-collections'
  71. ? 'CollectionsMyCollection'
  72. : 'CollectionsTeamsCollection'
  73. "
  74. v-for="(collection, index) in filteredCollections"
  75. :key="`collection-${index}`"
  76. :collection-index="index"
  77. :collection="collection"
  78. :doc="doc"
  79. :is-filtered="filterText.length > 0"
  80. :selected="selected.some((coll) => coll == collection)"
  81. :save-request="saveRequest"
  82. :collections-type="collectionsType"
  83. :picked="picked"
  84. :loading-collection-i-ds="loadingCollectionIDs"
  85. @edit-collection="editCollection(collection, index)"
  86. @add-request="addRequest($event)"
  87. @add-folder="addFolder($event)"
  88. @edit-folder="editFolder($event)"
  89. @edit-request="editRequest($event)"
  90. @duplicate-request="duplicateRequest($event)"
  91. @update-team-collections="updateTeamCollections"
  92. @select-collection="$emit('use-collection', collection)"
  93. @unselect-collection="$emit('remove-collection', collection)"
  94. @select="$emit('select', $event)"
  95. @expand-collection="expandCollection"
  96. @remove-collection="removeCollection"
  97. @remove-request="removeRequest"
  98. @remove-folder="removeFolder"
  99. />
  100. </div>
  101. <div
  102. v-if="loadingCollectionIDs.includes('root')"
  103. class="flex flex-col items-center justify-center p-4"
  104. >
  105. <SmartSpinner class="my-4" />
  106. <span class="text-secondaryLight">{{ $t("state.loading") }}</span>
  107. </div>
  108. <div
  109. v-else-if="filteredCollections.length === 0 && filterText.length === 0"
  110. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  111. >
  112. <img
  113. :src="`/images/states/${$colorMode.value}/pack.svg`"
  114. loading="lazy"
  115. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  116. :alt="$t('empty.collections')"
  117. />
  118. <span class="pb-4 text-center">
  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 items-center justify-center p-4 text-secondaryLight"
  144. >
  145. <i class="pb-2 opacity-75 material-icons">manage_search</i>
  146. <span class="my-2 text-center">
  147. {{ $t("state.nothing_found") }} "{{ filterText }}"
  148. </span>
  149. </div>
  150. <CollectionsAdd
  151. :show="showModalAdd"
  152. :loading-state="modalLoadingState"
  153. @submit="addNewRootCollection"
  154. @hide-modal="displayModalAdd(false)"
  155. />
  156. <CollectionsEdit
  157. :show="showModalEdit"
  158. :editing-collection-name="
  159. editingCollection
  160. ? editingCollection.name || editingCollection.title
  161. : ''
  162. "
  163. :loading-state="modalLoadingState"
  164. @hide-modal="displayModalEdit(false)"
  165. @submit="updateEditingCollection"
  166. />
  167. <CollectionsAddRequest
  168. :show="showModalAddRequest"
  169. :folder="editingFolder"
  170. :folder-path="editingFolderPath"
  171. :loading-state="modalLoadingState"
  172. @add-request="onAddRequest($event)"
  173. @hide-modal="displayModalAddRequest(false)"
  174. />
  175. <CollectionsAddFolder
  176. :show="showModalAddFolder"
  177. :folder="editingFolder"
  178. :folder-path="editingFolderPath"
  179. :loading-state="modalLoadingState"
  180. @add-folder="onAddFolder($event)"
  181. @hide-modal="displayModalAddFolder(false)"
  182. />
  183. <CollectionsEditFolder
  184. :show="showModalEditFolder"
  185. :editing-folder-name="
  186. editingFolder ? editingFolder.name || editingFolder.title : ''
  187. "
  188. :loading-state="modalLoadingState"
  189. @submit="updateEditingFolder"
  190. @hide-modal="displayModalEditFolder(false)"
  191. />
  192. <CollectionsEditRequest
  193. :show="showModalEditRequest"
  194. :editing-request-name="editingRequest ? editingRequest.name : ''"
  195. :loading-state="modalLoadingState"
  196. @submit="updateEditingRequest"
  197. @hide-modal="displayModalEditRequest(false)"
  198. />
  199. <CollectionsImportExport
  200. :show="showModalImportExport"
  201. :collections-type="collectionsType"
  202. @hide-modal="displayModalImportExport(false)"
  203. @update-team-collections="updateTeamCollections"
  204. />
  205. <SmartConfirmModal
  206. :show="showConfirmModal"
  207. :title="confirmModalTitle"
  208. :loading-state="modalLoadingState"
  209. @hide-modal="showConfirmModal = false"
  210. @resolve="resolveConfirmModal"
  211. />
  212. </div>
  213. </template>
  214. <script>
  215. import cloneDeep from "lodash/cloneDeep"
  216. import { defineComponent } from "@nuxtjs/composition-api"
  217. import { makeCollection } from "@hoppscotch/data"
  218. import * as E from "fp-ts/Either"
  219. import CollectionsMyCollection from "./my/Collection.vue"
  220. import CollectionsTeamsCollection from "./teams/Collection.vue"
  221. import { currentUser$ } from "~/helpers/fb/auth"
  222. import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter"
  223. import {
  224. restCollections$,
  225. addRESTCollection,
  226. editRESTCollection,
  227. addRESTFolder,
  228. removeRESTCollection,
  229. removeRESTFolder,
  230. editRESTFolder,
  231. removeRESTRequest,
  232. editRESTRequest,
  233. saveRESTRequestAs,
  234. } from "~/newstore/collections"
  235. import {
  236. setRESTRequest,
  237. getRESTRequest,
  238. getRESTSaveContext,
  239. } from "~/newstore/RESTSession"
  240. import {
  241. useReadonlyStream,
  242. useStreamSubscriber,
  243. } from "~/helpers/utils/composables"
  244. import { runMutation } from "~/helpers/backend/GQLClient"
  245. import {
  246. CreateChildCollectionDocument,
  247. CreateNewRootCollectionDocument,
  248. CreateRequestInCollectionDocument,
  249. DeleteCollectionDocument,
  250. DeleteRequestDocument,
  251. RenameCollectionDocument,
  252. UpdateRequestDocument,
  253. } from "~/helpers/backend/graphql"
  254. export default defineComponent({
  255. components: {
  256. CollectionsMyCollection,
  257. CollectionsTeamsCollection,
  258. },
  259. props: {
  260. doc: Boolean,
  261. selected: { type: Array, default: () => [] },
  262. saveRequest: Boolean,
  263. picked: { type: Object, default: () => {} },
  264. },
  265. setup() {
  266. const { subscribeToStream } = useStreamSubscriber()
  267. return {
  268. subscribeTo: subscribeToStream,
  269. collections: useReadonlyStream(restCollections$, []),
  270. currentUser: useReadonlyStream(currentUser$, null),
  271. }
  272. },
  273. data() {
  274. return {
  275. showModalAdd: false,
  276. showModalEdit: false,
  277. showModalImportExport: false,
  278. showModalAddRequest: false,
  279. showModalAddFolder: false,
  280. showModalEditFolder: false,
  281. showModalEditRequest: false,
  282. showConfirmModal: false,
  283. modalLoadingState: false,
  284. editingCollection: undefined,
  285. editingCollectionIndex: undefined,
  286. editingCollectionID: undefined,
  287. editingFolder: undefined,
  288. editingFolderName: undefined,
  289. editingFolderIndex: undefined,
  290. editingFolderPath: undefined,
  291. editingRequest: undefined,
  292. editingRequestIndex: undefined,
  293. confirmModalTitle: undefined,
  294. filterText: "",
  295. collectionsType: {
  296. type: "my-collections",
  297. selectedTeam: undefined,
  298. },
  299. teamCollectionAdapter: new TeamCollectionAdapter(null),
  300. teamCollectionsNew: [],
  301. loadingCollectionIDs: [],
  302. }
  303. },
  304. computed: {
  305. showTeamCollections() {
  306. if (this.currentUser == null) {
  307. return false
  308. }
  309. return true
  310. },
  311. filteredCollections() {
  312. const collections =
  313. this.collectionsType.type === "my-collections"
  314. ? this.collections
  315. : this.teamCollectionsNew
  316. if (!this.filterText) {
  317. return collections
  318. }
  319. if (this.collectionsType.type === "team-collections") {
  320. return []
  321. }
  322. const filterText = this.filterText.toLowerCase()
  323. const filteredCollections = []
  324. for (const collection of collections) {
  325. const filteredRequests = []
  326. const filteredFolders = []
  327. for (const request of collection.requests) {
  328. if (request.name.toLowerCase().includes(filterText))
  329. filteredRequests.push(request)
  330. }
  331. for (const folder of this.collectionsType.type === "team-collections"
  332. ? collection.children
  333. : collection.folders) {
  334. const filteredFolderRequests = []
  335. for (const request of folder.requests) {
  336. if (request.name.toLowerCase().includes(filterText))
  337. filteredFolderRequests.push(request)
  338. }
  339. if (filteredFolderRequests.length > 0) {
  340. const filteredFolder = Object.assign({}, folder)
  341. filteredFolder.requests = filteredFolderRequests
  342. filteredFolders.push(filteredFolder)
  343. }
  344. }
  345. if (
  346. filteredRequests.length + filteredFolders.length > 0 ||
  347. collection.name.toLowerCase().includes(filterText)
  348. ) {
  349. const filteredCollection = Object.assign({}, collection)
  350. filteredCollection.requests = filteredRequests
  351. filteredCollection.folders = filteredFolders
  352. filteredCollections.push(filteredCollection)
  353. }
  354. }
  355. return filteredCollections
  356. },
  357. },
  358. watch: {
  359. "collectionsType.type": function emitstuff() {
  360. this.$emit("update-collection", this.$data.collectionsType.type)
  361. },
  362. "collectionsType.selectedTeam"(value) {
  363. if (value?.id) this.teamCollectionAdapter.changeTeamID(value.id)
  364. },
  365. currentUser(newValue) {
  366. if (!newValue) this.updateCollectionType("my-collections")
  367. },
  368. },
  369. mounted() {
  370. this.subscribeTo(this.teamCollectionAdapter.collections$, (colls) => {
  371. this.teamCollectionsNew = cloneDeep(colls)
  372. })
  373. this.subscribeTo(
  374. this.teamCollectionAdapter.loadingCollections$,
  375. (collectionsIDs) => {
  376. this.loadingCollectionIDs = collectionsIDs
  377. }
  378. )
  379. },
  380. methods: {
  381. updateTeamCollections() {
  382. // TODO: Remove this at some point
  383. },
  384. updateSelectedTeam(newSelectedTeam) {
  385. this.collectionsType.selectedTeam = newSelectedTeam
  386. this.$emit("update-coll-type", this.collectionsType)
  387. },
  388. updateCollectionType(newCollectionType) {
  389. this.collectionsType.type = newCollectionType
  390. this.$emit("update-coll-type", this.collectionsType)
  391. },
  392. // Intented to be called by the CollectionAdd modal submit event
  393. addNewRootCollection(name) {
  394. if (this.collectionsType.type === "my-collections") {
  395. addRESTCollection(
  396. makeCollection({
  397. name,
  398. folders: [],
  399. requests: [],
  400. })
  401. )
  402. this.displayModalAdd(false)
  403. } else if (
  404. this.collectionsType.type === "team-collections" &&
  405. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  406. ) {
  407. this.modalLoadingState = true
  408. runMutation(CreateNewRootCollectionDocument, {
  409. title: name,
  410. teamID: this.collectionsType.selectedTeam.id,
  411. })().then((result) => {
  412. this.modalLoadingState = false
  413. if (E.isLeft(result)) {
  414. if (result.left.error === "team_coll/short_title")
  415. this.$toast.error(this.$t("collection.name_length_insufficient"))
  416. else this.$toast.error(this.$t("error.something_went_wrong"))
  417. console.error(result.left.error)
  418. } else {
  419. this.$toast.success(this.$t("collection.created"))
  420. this.displayModalAdd(false)
  421. }
  422. })
  423. }
  424. },
  425. // Intented to be called by CollectionEdit modal submit event
  426. updateEditingCollection(newName) {
  427. if (!newName) {
  428. this.$toast.error(this.$t("collection.invalid_name"))
  429. return
  430. }
  431. if (this.collectionsType.type === "my-collections") {
  432. const collectionUpdated = {
  433. ...this.editingCollection,
  434. name: newName,
  435. }
  436. editRESTCollection(this.editingCollectionIndex, collectionUpdated)
  437. this.displayModalEdit(false)
  438. } else if (
  439. this.collectionsType.type === "team-collections" &&
  440. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  441. ) {
  442. this.modalLoadingState = true
  443. runMutation(RenameCollectionDocument, {
  444. collectionID: this.editingCollection.id,
  445. newTitle: newName,
  446. })().then((result) => {
  447. this.modalLoadingState = false
  448. if (E.isLeft(result)) {
  449. this.$toast.error(this.$t("error.something_went_wrong"))
  450. console.error(result.left.error)
  451. } else {
  452. this.$toast.success(this.$t("collection.renamed"))
  453. this.displayModalEdit(false)
  454. }
  455. })
  456. }
  457. },
  458. // Intended to be called by CollectionEditFolder modal submit event
  459. updateEditingFolder(name) {
  460. if (this.collectionsType.type === "my-collections") {
  461. editRESTFolder(this.editingFolderPath, { ...this.editingFolder, name })
  462. this.displayModalEditFolder(false)
  463. } else if (
  464. this.collectionsType.type === "team-collections" &&
  465. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  466. ) {
  467. this.modalLoadingState = true
  468. runMutation(RenameCollectionDocument, {
  469. collectionID: this.editingFolder.id,
  470. newTitle: name,
  471. })().then((result) => {
  472. this.modalLoadingState = false
  473. if (E.isLeft(result)) {
  474. if (result.left.error === "team_coll/short_title")
  475. this.$toast.error(this.$t("folder.name_length_insufficient"))
  476. else this.$toast.error(this.$t("error.something_went_wrong"))
  477. console.error(result.left.error)
  478. } else {
  479. this.$toast.success(this.$t("folder.renamed"))
  480. this.displayModalEditFolder(false)
  481. }
  482. })
  483. }
  484. },
  485. // Intented to by called by CollectionsEditRequest modal submit event
  486. updateEditingRequest(requestUpdateData) {
  487. const saveCtx = getRESTSaveContext()
  488. const requestUpdated = {
  489. ...this.editingRequest,
  490. name: requestUpdateData.name || this.editingRequest.name,
  491. }
  492. if (this.collectionsType.type === "my-collections") {
  493. // Update REST Session with the updated state
  494. if (
  495. saveCtx &&
  496. saveCtx.originLocation === "user-collection" &&
  497. saveCtx.requestIndex === this.editingRequestIndex &&
  498. saveCtx.folderPath === this.editingFolderPath
  499. ) {
  500. setRESTRequest({
  501. ...getRESTRequest(),
  502. name: requestUpdateData.name,
  503. })
  504. }
  505. editRESTRequest(
  506. this.editingFolderPath,
  507. this.editingRequestIndex,
  508. requestUpdated
  509. )
  510. this.displayModalEditRequest(false)
  511. } else if (
  512. this.collectionsType.type === "team-collections" &&
  513. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  514. ) {
  515. this.modalLoadingState = true
  516. const requestName = requestUpdateData.name || this.editingRequest.name
  517. // Update REST Session with the updated state
  518. if (
  519. saveCtx &&
  520. saveCtx.originLocation === "team-collection" &&
  521. saveCtx.requestID === this.editingRequestIndex
  522. ) {
  523. setRESTRequest({
  524. ...getRESTRequest(),
  525. name: requestUpdateData.name,
  526. })
  527. }
  528. runMutation(UpdateRequestDocument, {
  529. data: {
  530. request: JSON.stringify(requestUpdated),
  531. title: requestName,
  532. },
  533. requestID: this.editingRequestIndex,
  534. })().then((result) => {
  535. this.modalLoadingState = false
  536. if (E.isLeft(result)) {
  537. this.$toast.error(this.$t("error.something_went_wrong"))
  538. console.error(result.left.error)
  539. } else {
  540. this.$toast.success(this.$t("request.renamed"))
  541. this.$emit("update-team-collections")
  542. this.displayModalEditRequest(false)
  543. }
  544. })
  545. }
  546. },
  547. displayModalAdd(shouldDisplay) {
  548. this.showModalAdd = shouldDisplay
  549. },
  550. displayModalEdit(shouldDisplay) {
  551. this.showModalEdit = shouldDisplay
  552. if (!shouldDisplay) this.resetSelectedData()
  553. },
  554. displayModalImportExport(shouldDisplay) {
  555. this.showModalImportExport = shouldDisplay
  556. },
  557. displayModalAddRequest(shouldDisplay) {
  558. this.showModalAddRequest = shouldDisplay
  559. if (!shouldDisplay) this.resetSelectedData()
  560. },
  561. displayModalAddFolder(shouldDisplay) {
  562. this.showModalAddFolder = shouldDisplay
  563. if (!shouldDisplay) this.resetSelectedData()
  564. },
  565. displayModalEditFolder(shouldDisplay) {
  566. this.showModalEditFolder = shouldDisplay
  567. if (!shouldDisplay) this.resetSelectedData()
  568. },
  569. displayModalEditRequest(shouldDisplay) {
  570. this.showModalEditRequest = shouldDisplay
  571. if (!shouldDisplay) this.resetSelectedData()
  572. },
  573. displayConfirmModal(shouldDisplay) {
  574. this.showConfirmModal = shouldDisplay
  575. if (!shouldDisplay) this.resetSelectedData()
  576. },
  577. editCollection(collection, collectionIndex) {
  578. this.$data.editingCollection = collection
  579. this.$data.editingCollectionIndex = collectionIndex
  580. this.displayModalEdit(true)
  581. },
  582. onAddFolder({ name, folder, path }) {
  583. if (this.collectionsType.type === "my-collections") {
  584. addRESTFolder(name, path)
  585. this.displayModalAddFolder(false)
  586. } else if (
  587. this.collectionsType.type === "team-collections" &&
  588. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  589. ) {
  590. this.modalLoadingState = true
  591. runMutation(CreateChildCollectionDocument, {
  592. childTitle: name,
  593. collectionID: folder.id,
  594. })().then((result) => {
  595. this.modalLoadingState = false
  596. if (E.isLeft(result)) {
  597. if (result.left.error === "team_coll/short_title")
  598. this.$toast.error(this.$t("folder.name_length_insufficient"))
  599. else this.$toast.error(this.$t("error.something_went_wrong"))
  600. console.error(result.left.error)
  601. } else {
  602. this.$toast.success(this.$t("folder.created"))
  603. this.displayModalAddFolder(false)
  604. this.$emit("update-team-collections")
  605. }
  606. })
  607. }
  608. },
  609. addFolder(payload) {
  610. const { folder, path } = payload
  611. this.$data.editingFolder = folder
  612. this.$data.editingFolderPath = path
  613. this.displayModalAddFolder(true)
  614. },
  615. editFolder(payload) {
  616. const { collectionIndex, folder, folderIndex, folderPath } = payload
  617. this.$data.editingCollectionIndex = collectionIndex
  618. this.$data.editingFolder = folder
  619. this.$data.editingFolderIndex = folderIndex
  620. this.$data.editingFolderPath = folderPath
  621. this.$data.collectionsType = this.collectionsType
  622. this.displayModalEditFolder(true)
  623. },
  624. editRequest(payload) {
  625. const {
  626. collectionIndex,
  627. folderIndex,
  628. folderName,
  629. request,
  630. requestIndex,
  631. folderPath,
  632. } = payload
  633. this.$data.editingCollectionIndex = collectionIndex
  634. this.$data.editingFolderIndex = folderIndex
  635. this.$data.editingFolderName = folderName
  636. this.$data.editingRequest = request
  637. this.$data.editingRequestIndex = requestIndex
  638. this.editingFolderPath = folderPath
  639. this.$emit("select-request", requestIndex)
  640. this.displayModalEditRequest(true)
  641. },
  642. resetSelectedData() {
  643. this.$data.editingCollection = undefined
  644. this.$data.editingCollectionIndex = undefined
  645. this.$data.editingCollectionID = undefined
  646. this.$data.editingFolder = undefined
  647. this.$data.editingFolderPath = undefined
  648. this.$data.editingFolderIndex = undefined
  649. this.$data.editingRequest = undefined
  650. this.$data.editingRequestIndex = undefined
  651. this.$data.confirmModalTitle = undefined
  652. },
  653. expandCollection(collectionID) {
  654. this.teamCollectionAdapter.expandCollection(collectionID)
  655. },
  656. removeCollection({ collectionIndex, collectionID }) {
  657. this.$data.editingCollectionIndex = collectionIndex
  658. this.$data.editingCollectionID = collectionID
  659. this.confirmModalTitle = `${this.$t("confirm.remove_collection")}`
  660. this.displayConfirmModal(true)
  661. },
  662. onRemoveCollection() {
  663. const collectionIndex = this.$data.editingCollectionIndex
  664. const collectionID = this.$data.editingCollectionID
  665. if (this.collectionsType.type === "my-collections") {
  666. // Cancel pick if picked collection is deleted
  667. if (
  668. this.picked &&
  669. this.picked.pickedType === "my-collection" &&
  670. this.picked.collectionIndex === collectionIndex
  671. ) {
  672. this.$emit("select", { picked: null })
  673. }
  674. removeRESTCollection(collectionIndex)
  675. this.$toast.success(this.$t("state.deleted"))
  676. this.displayConfirmModal(false)
  677. } else if (this.collectionsType.type === "team-collections") {
  678. this.modalLoadingState = true
  679. // Cancel pick if picked collection is deleted
  680. if (
  681. this.picked &&
  682. this.picked.pickedType === "teams-collection" &&
  683. this.picked.collectionID === collectionID
  684. ) {
  685. this.$emit("select", { picked: null })
  686. }
  687. if (this.collectionsType.selectedTeam.myRole !== "VIEWER") {
  688. runMutation(DeleteCollectionDocument, {
  689. collectionID,
  690. })().then((result) => {
  691. this.modalLoadingState = false
  692. if (E.isLeft(result)) {
  693. this.$toast.error(this.$t("error.something_went_wrong"))
  694. console.error(result.left.error)
  695. } else {
  696. this.$toast.success(this.$t("state.deleted"))
  697. this.displayConfirmModal(false)
  698. }
  699. })
  700. }
  701. }
  702. },
  703. removeFolder({ collectionID, folder, folderPath }) {
  704. this.$data.editingCollectionID = collectionID
  705. this.$data.editingFolder = folder
  706. this.$data.editingFolderPath = folderPath
  707. this.confirmModalTitle = `${this.$t("confirm.remove_folder")}`
  708. this.displayConfirmModal(true)
  709. },
  710. onRemoveFolder() {
  711. const folder = this.$data.editingFolder
  712. const folderPath = this.$data.editingFolderPath
  713. if (this.collectionsType.type === "my-collections") {
  714. // Cancel pick if picked folder was deleted
  715. if (
  716. this.picked &&
  717. this.picked.pickedType === "my-folder" &&
  718. this.picked.folderPath === folderPath
  719. ) {
  720. this.$emit("select", { picked: null })
  721. }
  722. removeRESTFolder(folderPath)
  723. this.$toast.success(this.$t("state.deleted"))
  724. this.displayConfirmModal(false)
  725. } else if (this.collectionsType.type === "team-collections") {
  726. this.modalLoadingState = true
  727. // Cancel pick if picked collection folder was deleted
  728. if (
  729. this.picked &&
  730. this.picked.pickedType === "teams-folder" &&
  731. this.picked.folderID === folder.id
  732. ) {
  733. this.$emit("select", { picked: null })
  734. }
  735. if (this.collectionsType.selectedTeam.myRole !== "VIEWER") {
  736. runMutation(DeleteCollectionDocument, {
  737. collectionID: folder.id,
  738. })().then((result) => {
  739. this.modalLoadingState = false
  740. if (E.isLeft(result)) {
  741. this.$toast.error(`${this.$t("error.something_went_wrong")}`)
  742. console.error(result.left.error)
  743. } else {
  744. this.$toast.success(`${this.$t("state.deleted")}`)
  745. this.displayConfirmModal(false)
  746. this.updateTeamCollections()
  747. }
  748. })
  749. }
  750. }
  751. },
  752. removeRequest({ requestIndex, folderPath }) {
  753. this.$data.editingRequestIndex = requestIndex
  754. this.$data.editingFolderPath = folderPath
  755. this.confirmModalTitle = `${this.$t("confirm.remove_request")}`
  756. this.displayConfirmModal(true)
  757. },
  758. onRemoveRequest() {
  759. const requestIndex = this.$data.editingRequestIndex
  760. const folderPath = this.$data.editingFolderPath
  761. if (this.collectionsType.type === "my-collections") {
  762. // Cancel pick if the picked item is being deleted
  763. if (
  764. this.picked &&
  765. this.picked.pickedType === "my-request" &&
  766. this.picked.folderPath === folderPath &&
  767. this.picked.requestIndex === requestIndex
  768. ) {
  769. this.$emit("select", { picked: null })
  770. }
  771. removeRESTRequest(folderPath, requestIndex)
  772. this.$toast.success(this.$t("state.deleted"))
  773. this.displayConfirmModal(false)
  774. } else if (this.collectionsType.type === "team-collections") {
  775. this.modalLoadingState = true
  776. // Cancel pick if the picked item is being deleted
  777. if (
  778. this.picked &&
  779. this.picked.pickedType === "teams-request" &&
  780. this.picked.requestID === requestIndex
  781. ) {
  782. this.$emit("select", { picked: null })
  783. }
  784. runMutation(DeleteRequestDocument, {
  785. requestID: requestIndex,
  786. })().then((result) => {
  787. this.modalLoadingState = false
  788. if (E.isLeft(result)) {
  789. this.$toast.error(this.$t("error.something_went_wrong"))
  790. console.error(result.left.error)
  791. } else {
  792. this.$toast.success(this.$t("state.deleted"))
  793. this.displayConfirmModal(false)
  794. }
  795. })
  796. }
  797. },
  798. addRequest(payload) {
  799. // TODO: check if the request being worked on
  800. // is being overwritten (selected or not)
  801. const { folder, path } = payload
  802. this.$data.editingFolder = folder
  803. this.$data.editingFolderPath = path
  804. this.displayModalAddRequest(true)
  805. },
  806. onAddRequest({ name, folder, path }) {
  807. const newRequest = {
  808. ...cloneDeep(getRESTRequest()),
  809. name,
  810. }
  811. if (this.collectionsType.type === "my-collections") {
  812. const insertionIndex = saveRESTRequestAs(path, newRequest)
  813. // point to it
  814. setRESTRequest(newRequest, {
  815. originLocation: "user-collection",
  816. folderPath: path,
  817. requestIndex: insertionIndex,
  818. })
  819. this.displayModalAddRequest(false)
  820. } else if (
  821. this.collectionsType.type === "team-collections" &&
  822. this.collectionsType.selectedTeam.myRole !== "VIEWER"
  823. ) {
  824. this.modalLoadingState = true
  825. runMutation(CreateRequestInCollectionDocument, {
  826. collectionID: folder.id,
  827. data: {
  828. request: JSON.stringify(newRequest),
  829. teamID: this.collectionsType.selectedTeam.id,
  830. title: name,
  831. },
  832. })().then((result) => {
  833. this.modalLoadingState = false
  834. if (E.isLeft(result)) {
  835. this.$toast.error(this.$t("error.something_went_wrong"))
  836. console.error(result.left.error)
  837. } else {
  838. const { createRequestInCollection } = result.right
  839. // point to it
  840. setRESTRequest(newRequest, {
  841. originLocation: "team-collection",
  842. requestID: createRequestInCollection.id,
  843. collectionID: createRequestInCollection.collection.id,
  844. teamID: createRequestInCollection.collection.team.id,
  845. })
  846. this.displayModalAddRequest(false)
  847. }
  848. })
  849. }
  850. },
  851. duplicateRequest({ folderPath, request, collectionID }) {
  852. if (this.collectionsType.type === "team-collections") {
  853. const newReq = {
  854. ...cloneDeep(request),
  855. name: `${request.name} - ${this.$t("action.duplicate")}`,
  856. }
  857. // Error handling ?
  858. runMutation(CreateRequestInCollectionDocument, {
  859. collectionID,
  860. data: {
  861. request: JSON.stringify(newReq),
  862. teamID: this.collectionsType.selectedTeam.id,
  863. title: `${request.name} - ${this.$t("action.duplicate")}`,
  864. },
  865. })()
  866. } else if (this.collectionsType.type === "my-collections") {
  867. saveRESTRequestAs(folderPath, {
  868. ...cloneDeep(request),
  869. name: `${request.name} - ${this.$t("action.duplicate")}`,
  870. })
  871. }
  872. },
  873. resolveConfirmModal(title) {
  874. if (title === `${this.$t("confirm.remove_collection")}`)
  875. this.onRemoveCollection()
  876. else if (title === `${this.$t("confirm.remove_request")}`)
  877. this.onRemoveRequest()
  878. else if (title === `${this.$t("confirm.remove_folder")}`)
  879. this.onRemoveFolder()
  880. else {
  881. console.error(
  882. `Confirm modal title ${title} is not handled by the component`
  883. )
  884. this.$toast.error(this.$t("error.something_went_wrong"))
  885. this.displayConfirmModal(false)
  886. }
  887. },
  888. },
  889. })
  890. // request inside folder is not being deleted, you dumb fuck
  891. </script>