index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 cloneDeep from "lodash/cloneDeep"
  134. import clone from "lodash/clone"
  135. import { useReadonlyStream } from "~/helpers/utils/composables"
  136. import {
  137. graphqlCollections$,
  138. addGraphqlFolder,
  139. saveGraphqlRequestAs,
  140. } from "~/newstore/collections"
  141. export default defineComponent({
  142. props: {
  143. // Whether to activate the ability to pick items (activates 'select' events)
  144. savingMode: { type: Boolean, default: false },
  145. doc: { type: Boolean, default: false },
  146. picked: { type: Object, default: null },
  147. // Whether to show the 'New' and 'Import/Export' actions
  148. showCollActions: { type: Boolean, default: true },
  149. },
  150. setup() {
  151. return {
  152. collections: useReadonlyStream(graphqlCollections$, []),
  153. }
  154. },
  155. data() {
  156. return {
  157. showModalAdd: false,
  158. showModalEdit: false,
  159. showModalImportExport: false,
  160. showModalAddFolder: false,
  161. showModalEditFolder: false,
  162. showModalEditRequest: false,
  163. editingCollection: undefined,
  164. editingCollectionIndex: undefined,
  165. editingFolder: undefined,
  166. editingFolderName: undefined,
  167. editingFolderIndex: undefined,
  168. editingFolderPath: undefined,
  169. editingRequest: undefined,
  170. editingRequestIndex: undefined,
  171. filterText: "",
  172. }
  173. },
  174. computed: {
  175. filteredCollections() {
  176. const collections = clone(this.collections)
  177. if (!this.filterText) return collections
  178. const filterText = this.filterText.toLowerCase()
  179. const filteredCollections = []
  180. for (const collection of collections) {
  181. const filteredRequests = []
  182. const filteredFolders = []
  183. for (const request of collection.requests) {
  184. if (request.name.toLowerCase().includes(filterText))
  185. filteredRequests.push(request)
  186. }
  187. for (const folder of collection.folders) {
  188. const filteredFolderRequests = []
  189. for (const request of folder.requests) {
  190. if (request.name.toLowerCase().includes(filterText))
  191. filteredFolderRequests.push(request)
  192. }
  193. if (filteredFolderRequests.length > 0) {
  194. const filteredFolder = Object.assign({}, folder)
  195. filteredFolder.requests = filteredFolderRequests
  196. filteredFolders.push(filteredFolder)
  197. }
  198. }
  199. if (filteredRequests.length + filteredFolders.length > 0) {
  200. const filteredCollection = Object.assign({}, collection)
  201. filteredCollection.requests = filteredRequests
  202. filteredCollection.folders = filteredFolders
  203. filteredCollections.push(filteredCollection)
  204. }
  205. }
  206. return filteredCollections
  207. },
  208. },
  209. methods: {
  210. displayModalAdd(shouldDisplay) {
  211. this.showModalAdd = shouldDisplay
  212. },
  213. displayModalEdit(shouldDisplay) {
  214. this.showModalEdit = shouldDisplay
  215. if (!shouldDisplay) this.resetSelectedData()
  216. },
  217. displayModalImportExport(shouldDisplay) {
  218. this.showModalImportExport = shouldDisplay
  219. },
  220. displayModalAddFolder(shouldDisplay) {
  221. this.showModalAddFolder = shouldDisplay
  222. if (!shouldDisplay) this.resetSelectedData()
  223. },
  224. displayModalEditFolder(shouldDisplay) {
  225. this.showModalEditFolder = shouldDisplay
  226. if (!shouldDisplay) this.resetSelectedData()
  227. },
  228. displayModalEditRequest(shouldDisplay) {
  229. this.showModalEditRequest = shouldDisplay
  230. if (!shouldDisplay) this.resetSelectedData()
  231. },
  232. editCollection(collection, collectionIndex) {
  233. this.$data.editingCollection = collection
  234. this.$data.editingCollectionIndex = collectionIndex
  235. this.displayModalEdit(true)
  236. },
  237. onAddFolder({ name, path }) {
  238. addGraphqlFolder(name, path)
  239. this.displayModalAddFolder(false)
  240. },
  241. addFolder(payload) {
  242. const { path } = payload
  243. this.$data.editingFolderPath = path
  244. this.displayModalAddFolder(true)
  245. },
  246. editFolder(payload) {
  247. const { folder, folderPath } = payload
  248. this.editingFolder = folder
  249. this.editingFolderPath = folderPath
  250. this.displayModalEditFolder(true)
  251. },
  252. editRequest(payload) {
  253. const {
  254. collectionIndex,
  255. folderIndex,
  256. folderName,
  257. request,
  258. requestIndex,
  259. folderPath,
  260. } = payload
  261. this.$data.editingFolderPath = folderPath
  262. this.$data.editingCollectionIndex = collectionIndex
  263. this.$data.editingFolderIndex = folderIndex
  264. this.$data.editingFolderName = folderName
  265. this.$data.editingRequest = request
  266. this.$data.editingRequestIndex = requestIndex
  267. this.displayModalEditRequest(true)
  268. },
  269. resetSelectedData() {
  270. this.$data.editingCollection = undefined
  271. this.$data.editingCollectionIndex = undefined
  272. this.$data.editingFolder = undefined
  273. this.$data.editingFolderIndex = undefined
  274. this.$data.editingRequest = undefined
  275. this.$data.editingRequestIndex = undefined
  276. },
  277. duplicateRequest({ folderPath, request }) {
  278. saveGraphqlRequestAs(folderPath, {
  279. ...cloneDeep(request),
  280. name: `${request.name} - ${this.$t("action.duplicate")}`,
  281. })
  282. },
  283. },
  284. })
  285. </script>