index.vue 9.2 KB

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