index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <AppSection
  3. label="collections"
  4. :class="{ 'rounded border border-divider': savingMode }"
  5. >
  6. <div
  7. class="divide-dividerLight divide-y border-b border-dividerLight flex flex-col top-0 z-10 sticky"
  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="bg-transparent flex w-full py-2 px-4"
  17. />
  18. <div class="flex flex-1 justify-between">
  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="flex flex-col text-secondaryLight p-4 items-center justify-center"
  66. >
  67. <img
  68. :src="`/images/states/${$colorMode.value}/pack.svg`"
  69. loading="lazy"
  70. class="flex-col object-contain object-center h-16 my-4 w-16 inline-flex"
  71. :alt="$t('empty.collections')"
  72. />
  73. <span class="text-center pb-4">
  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="flex flex-col text-secondaryLight p-4 items-center justify-center"
  85. >
  86. <i class="opacity-75 pb-2 material-icons">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. :editing-collection-name="editingCollection ? editingCollection.name : ''"
  100. @hide-modal="displayModalEdit(false)"
  101. />
  102. <CollectionsGraphqlAddFolder
  103. :show="showModalAddFolder"
  104. :folder-path="editingFolderPath"
  105. @add-folder="onAddFolder($event)"
  106. @hide-modal="displayModalAddFolder(false)"
  107. />
  108. <CollectionsGraphqlEditFolder
  109. :show="showModalEditFolder"
  110. :collection-index="editingCollectionIndex"
  111. :folder="editingFolder"
  112. :folder-index="editingFolderIndex"
  113. :folder-path="editingFolderPath"
  114. :editing-folder-name="editingFolder ? editingFolder.name : ''"
  115. @hide-modal="displayModalEditFolder(false)"
  116. />
  117. <CollectionsGraphqlEditRequest
  118. :show="showModalEditRequest"
  119. :folder-path="editingFolderPath"
  120. :request="editingRequest"
  121. :request-index="editingRequestIndex"
  122. :editing-request-name="editingRequest ? editingRequest.name : ''"
  123. @hide-modal="displayModalEditRequest(false)"
  124. />
  125. <CollectionsGraphqlImportExport
  126. :show="showModalImportExport"
  127. @hide-modal="displayModalImportExport(false)"
  128. />
  129. </AppSection>
  130. </template>
  131. <script>
  132. import { defineComponent } from "@nuxtjs/composition-api"
  133. import clone from "lodash/clone"
  134. import { useReadonlyStream } from "~/helpers/utils/composables"
  135. import {
  136. graphqlCollections$,
  137. addGraphqlFolder,
  138. saveGraphqlRequestAs,
  139. } from "~/newstore/collections"
  140. export default defineComponent({
  141. props: {
  142. // Whether to activate the ability to pick items (activates 'select' events)
  143. savingMode: { type: Boolean, default: false },
  144. doc: { type: Boolean, default: false },
  145. picked: { type: Object, default: null },
  146. // Whether to show the 'New' and 'Import/Export' actions
  147. showCollActions: { type: Boolean, default: true },
  148. },
  149. setup() {
  150. return {
  151. collections: useReadonlyStream(graphqlCollections$, []),
  152. }
  153. },
  154. data() {
  155. return {
  156. showModalAdd: false,
  157. showModalEdit: false,
  158. showModalImportExport: false,
  159. showModalAddFolder: false,
  160. showModalEditFolder: false,
  161. showModalEditRequest: false,
  162. editingCollection: undefined,
  163. editingCollectionIndex: undefined,
  164. editingFolder: undefined,
  165. editingFolderName: undefined,
  166. editingFolderIndex: undefined,
  167. editingFolderPath: undefined,
  168. editingRequest: undefined,
  169. editingRequestIndex: undefined,
  170. filterText: "",
  171. }
  172. },
  173. computed: {
  174. filteredCollections() {
  175. const collections = clone(this.collections)
  176. if (!this.filterText) return collections
  177. const filterText = this.filterText.toLowerCase()
  178. const filteredCollections = []
  179. for (const collection of collections) {
  180. const filteredRequests = []
  181. const filteredFolders = []
  182. for (const request of collection.requests) {
  183. if (request.name.toLowerCase().includes(filterText))
  184. filteredRequests.push(request)
  185. }
  186. for (const folder of collection.folders) {
  187. const filteredFolderRequests = []
  188. for (const request of folder.requests) {
  189. if (request.name.toLowerCase().includes(filterText))
  190. filteredFolderRequests.push(request)
  191. }
  192. if (filteredFolderRequests.length > 0) {
  193. const filteredFolder = Object.assign({}, folder)
  194. filteredFolder.requests = filteredFolderRequests
  195. filteredFolders.push(filteredFolder)
  196. }
  197. }
  198. if (filteredRequests.length + filteredFolders.length > 0) {
  199. const filteredCollection = Object.assign({}, collection)
  200. filteredCollection.requests = filteredRequests
  201. filteredCollection.folders = filteredFolders
  202. filteredCollections.push(filteredCollection)
  203. }
  204. }
  205. return filteredCollections
  206. },
  207. },
  208. methods: {
  209. displayModalAdd(shouldDisplay) {
  210. this.showModalAdd = shouldDisplay
  211. },
  212. displayModalEdit(shouldDisplay) {
  213. this.showModalEdit = shouldDisplay
  214. if (!shouldDisplay) this.resetSelectedData()
  215. },
  216. displayModalImportExport(shouldDisplay) {
  217. this.showModalImportExport = shouldDisplay
  218. },
  219. displayModalAddFolder(shouldDisplay) {
  220. this.showModalAddFolder = shouldDisplay
  221. if (!shouldDisplay) this.resetSelectedData()
  222. },
  223. displayModalEditFolder(shouldDisplay) {
  224. this.showModalEditFolder = shouldDisplay
  225. if (!shouldDisplay) this.resetSelectedData()
  226. },
  227. displayModalEditRequest(shouldDisplay) {
  228. this.showModalEditRequest = shouldDisplay
  229. if (!shouldDisplay) this.resetSelectedData()
  230. },
  231. editCollection(collection, collectionIndex) {
  232. this.$data.editingCollection = collection
  233. this.$data.editingCollectionIndex = collectionIndex
  234. this.displayModalEdit(true)
  235. },
  236. onAddFolder({ name, path }) {
  237. addGraphqlFolder(name, path)
  238. this.displayModalAddFolder(false)
  239. },
  240. addFolder(payload) {
  241. const { path } = payload
  242. this.$data.editingFolderPath = path
  243. this.displayModalAddFolder(true)
  244. },
  245. editFolder(payload) {
  246. const { folder, folderPath } = payload
  247. this.editingFolder = folder
  248. this.editingFolderPath = folderPath
  249. this.displayModalEditFolder(true)
  250. },
  251. editRequest(payload) {
  252. const {
  253. collectionIndex,
  254. folderIndex,
  255. folderName,
  256. request,
  257. requestIndex,
  258. folderPath,
  259. } = payload
  260. this.$data.editingFolderPath = folderPath
  261. this.$data.editingCollectionIndex = collectionIndex
  262. this.$data.editingFolderIndex = folderIndex
  263. this.$data.editingFolderName = folderName
  264. this.$data.editingRequest = request
  265. this.$data.editingRequestIndex = requestIndex
  266. this.displayModalEditRequest(true)
  267. },
  268. resetSelectedData() {
  269. this.$data.editingCollection = undefined
  270. this.$data.editingCollectionIndex = undefined
  271. this.$data.editingFolder = undefined
  272. this.$data.editingFolderIndex = undefined
  273. this.$data.editingRequest = undefined
  274. this.$data.editingRequestIndex = undefined
  275. },
  276. duplicateRequest({ folderPath, request }) {
  277. saveGraphqlRequestAs(folderPath, {
  278. ...cloneDeep(request),
  279. name: `${request.name} - ${this.$t("action.duplicate")}`,
  280. })
  281. },
  282. },
  283. })
  284. </script>