index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <AppSection
  3. label="collections"
  4. :class="{ 'rounded border border-divider': savingMode }"
  5. >
  6. <div
  7. class="divide-dividerLight border-dividerLight sticky top-0 z-10 flex flex-col border-b divide-y"
  8. :class="{ 'bg-primary': !savingMode }"
  9. >
  10. <input
  11. v-if="showCollActions"
  12. v-model="filterText"
  13. type="search"
  14. autocomplete="off"
  15. :placeholder="$t('action.search')"
  16. class="flex w-full px-4 py-2 bg-transparent"
  17. />
  18. <div class="flex justify-between flex-1">
  19. <ButtonSecondary
  20. svg="plus"
  21. :label="$t('action.new')"
  22. class="!rounded-none"
  23. @click.native="displayModalAdd(true)"
  24. />
  25. <div class="flex">
  26. <ButtonSecondary
  27. v-tippy="{ theme: 'tooltip' }"
  28. to="https://docs.hoppscotch.io/features/collections"
  29. blank
  30. :title="$t('app.wiki')"
  31. svg="help-circle"
  32. />
  33. <ButtonSecondary
  34. v-if="showCollActions"
  35. v-tippy="{ theme: 'tooltip' }"
  36. :title="$t('modal.import_export')"
  37. svg="archive"
  38. @click.native="displayModalImportExport(true)"
  39. />
  40. </div>
  41. </div>
  42. </div>
  43. <div class="flex-col">
  44. <CollectionsGraphqlCollection
  45. v-for="(collection, index) in filteredCollections"
  46. :key="`collection-${index}`"
  47. :picked="picked"
  48. :name="collection.name"
  49. :collection-index="index"
  50. :collection="collection"
  51. :doc="doc"
  52. :is-filtered="filterText.length > 0"
  53. :saving-mode="savingMode"
  54. @edit-collection="editCollection(collection, index)"
  55. @add-folder="addFolder($event)"
  56. @edit-folder="editFolder($event)"
  57. @edit-request="editRequest($event)"
  58. @duplicate-request="duplicateRequest($event)"
  59. @select-collection="$emit('use-collection', collection)"
  60. @select="$emit('select', $event)"
  61. />
  62. </div>
  63. <div
  64. v-if="collections.length === 0"
  65. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  66. >
  67. <img
  68. :src="`/images/states/${$colorMode.value}/pack.svg`"
  69. loading="lazy"
  70. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  71. :alt="$t('empty.collections')"
  72. />
  73. <span class="pb-4 text-center">
  74. {{ $t("empty.collections") }}
  75. </span>
  76. <ButtonSecondary
  77. :label="$t('add.new')"
  78. filled
  79. @click.native="displayModalAdd(true)"
  80. />
  81. </div>
  82. <div
  83. v-if="!(filteredCollections.length !== 0 || collections.length === 0)"
  84. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  85. >
  86. <i class="material-icons pb-2 opacity-75">manage_search</i>
  87. <span class="text-center">
  88. {{ $t("state.nothing_found") }} "{{ filterText }}"
  89. </span>
  90. </div>
  91. <CollectionsGraphqlAdd
  92. :show="showModalAdd"
  93. @hide-modal="displayModalAdd(false)"
  94. />
  95. <CollectionsGraphqlEdit
  96. :show="showModalEdit"
  97. :editing-collection="editingCollection"
  98. :editing-collection-index="editingCollectionIndex"
  99. @hide-modal="displayModalEdit(false)"
  100. />
  101. <CollectionsGraphqlAddFolder
  102. :show="showModalAddFolder"
  103. :folder-path="editingFolderPath"
  104. @add-folder="onAddFolder($event)"
  105. @hide-modal="displayModalAddFolder(false)"
  106. />
  107. <CollectionsGraphqlEditFolder
  108. :show="showModalEditFolder"
  109. :collection-index="editingCollectionIndex"
  110. :folder="editingFolder"
  111. :folder-index="editingFolderIndex"
  112. :folder-path="editingFolderPath"
  113. @hide-modal="displayModalEditFolder(false)"
  114. />
  115. <CollectionsGraphqlEditRequest
  116. :show="showModalEditRequest"
  117. :folder-path="editingFolderPath"
  118. :request="editingRequest"
  119. :request-index="editingRequestIndex"
  120. @hide-modal="displayModalEditRequest(false)"
  121. />
  122. <CollectionsGraphqlImportExport
  123. :show="showModalImportExport"
  124. @hide-modal="displayModalImportExport(false)"
  125. />
  126. </AppSection>
  127. </template>
  128. <script>
  129. import { defineComponent } from "@nuxtjs/composition-api"
  130. import clone from "lodash/clone"
  131. import { useReadonlyStream } from "~/helpers/utils/composables"
  132. import {
  133. graphqlCollections$,
  134. addGraphqlFolder,
  135. saveGraphqlRequestAs,
  136. } from "~/newstore/collections"
  137. export default defineComponent({
  138. props: {
  139. // Whether to activate the ability to pick items (activates 'select' events)
  140. savingMode: { type: Boolean, default: false },
  141. doc: { type: Boolean, default: false },
  142. picked: { type: Object, default: null },
  143. // Whether to show the 'New' and 'Import/Export' actions
  144. showCollActions: { type: Boolean, default: true },
  145. },
  146. setup() {
  147. return {
  148. collections: useReadonlyStream(graphqlCollections$, []),
  149. }
  150. },
  151. data() {
  152. return {
  153. showModalAdd: false,
  154. showModalEdit: false,
  155. showModalImportExport: false,
  156. showModalAddFolder: false,
  157. showModalEditFolder: false,
  158. showModalEditRequest: false,
  159. editingCollection: undefined,
  160. editingCollectionIndex: undefined,
  161. editingFolder: undefined,
  162. editingFolderName: undefined,
  163. editingFolderIndex: undefined,
  164. editingFolderPath: undefined,
  165. editingRequest: undefined,
  166. editingRequestIndex: undefined,
  167. filterText: "",
  168. }
  169. },
  170. computed: {
  171. filteredCollections() {
  172. const collections = clone(this.collections)
  173. if (!this.filterText) return collections
  174. const filterText = this.filterText.toLowerCase()
  175. const filteredCollections = []
  176. for (const collection of collections) {
  177. const filteredRequests = []
  178. const filteredFolders = []
  179. for (const request of collection.requests) {
  180. if (request.name.toLowerCase().includes(filterText))
  181. filteredRequests.push(request)
  182. }
  183. for (const folder of collection.folders) {
  184. const filteredFolderRequests = []
  185. for (const request of folder.requests) {
  186. if (request.name.toLowerCase().includes(filterText))
  187. filteredFolderRequests.push(request)
  188. }
  189. if (filteredFolderRequests.length > 0) {
  190. const filteredFolder = Object.assign({}, folder)
  191. filteredFolder.requests = filteredFolderRequests
  192. filteredFolders.push(filteredFolder)
  193. }
  194. }
  195. if (filteredRequests.length + filteredFolders.length > 0) {
  196. const filteredCollection = Object.assign({}, collection)
  197. filteredCollection.requests = filteredRequests
  198. filteredCollection.folders = filteredFolders
  199. filteredCollections.push(filteredCollection)
  200. }
  201. }
  202. return filteredCollections
  203. },
  204. },
  205. methods: {
  206. displayModalAdd(shouldDisplay) {
  207. this.showModalAdd = shouldDisplay
  208. },
  209. displayModalEdit(shouldDisplay) {
  210. this.showModalEdit = shouldDisplay
  211. if (!shouldDisplay) this.resetSelectedData()
  212. },
  213. displayModalImportExport(shouldDisplay) {
  214. this.showModalImportExport = shouldDisplay
  215. },
  216. displayModalAddFolder(shouldDisplay) {
  217. this.showModalAddFolder = shouldDisplay
  218. if (!shouldDisplay) this.resetSelectedData()
  219. },
  220. displayModalEditFolder(shouldDisplay) {
  221. this.showModalEditFolder = shouldDisplay
  222. if (!shouldDisplay) this.resetSelectedData()
  223. },
  224. displayModalEditRequest(shouldDisplay) {
  225. this.showModalEditRequest = shouldDisplay
  226. if (!shouldDisplay) this.resetSelectedData()
  227. },
  228. editCollection(collection, collectionIndex) {
  229. this.$data.editingCollection = collection
  230. this.$data.editingCollectionIndex = collectionIndex
  231. this.displayModalEdit(true)
  232. },
  233. onAddFolder({ name, path }) {
  234. addGraphqlFolder(name, path)
  235. this.displayModalAddFolder(false)
  236. },
  237. addFolder(payload) {
  238. const { path } = payload
  239. this.$data.editingFolderPath = path
  240. this.displayModalAddFolder(true)
  241. },
  242. editFolder(payload) {
  243. const { folder, folderPath } = payload
  244. this.editingFolder = folder
  245. this.editingFolderPath = folderPath
  246. this.displayModalEditFolder(true)
  247. },
  248. editRequest(payload) {
  249. const {
  250. collectionIndex,
  251. folderIndex,
  252. folderName,
  253. request,
  254. requestIndex,
  255. folderPath,
  256. } = payload
  257. this.$data.editingFolderPath = folderPath
  258. this.$data.editingCollectionIndex = collectionIndex
  259. this.$data.editingFolderIndex = folderIndex
  260. this.$data.editingFolderName = folderName
  261. this.$data.editingRequest = request
  262. this.$data.editingRequestIndex = requestIndex
  263. this.displayModalEditRequest(true)
  264. },
  265. resetSelectedData() {
  266. this.$data.editingCollection = undefined
  267. this.$data.editingCollectionIndex = undefined
  268. this.$data.editingFolder = undefined
  269. this.$data.editingFolderIndex = undefined
  270. this.$data.editingRequest = undefined
  271. this.$data.editingRequestIndex = undefined
  272. },
  273. duplicateRequest({ folderPath, request }) {
  274. saveGraphqlRequestAs(folderPath, {
  275. ...cloneDeep(request),
  276. name: `${request.name} - ${this.$t("action.duplicate")}`,
  277. })
  278. },
  279. },
  280. })
  281. </script>