123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <template>
- <div :class="{ 'rounded border border-divider': savingMode }">
- <div
- class="sticky top-0 z-10 flex flex-col border-b divide-dividerLight divide-y border-dividerLight"
- :class="{ 'bg-primary': !savingMode }"
- >
- <input
- v-if="showCollActions"
- v-model="filterText"
- type="search"
- autocomplete="off"
- :placeholder="$t('action.search')"
- class="flex px-4 py-2 bg-transparent"
- />
- <div class="flex justify-between flex-1">
- <ButtonSecondary
- svg="plus"
- :label="$t('action.new')"
- class="!rounded-none"
- @click.native="displayModalAdd(true)"
- />
- <div class="flex">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- to="https://docs.hoppscotch.io/features/collections"
- blank
- :title="$t('app.wiki')"
- svg="help-circle"
- />
- <ButtonSecondary
- v-if="showCollActions"
- v-tippy="{ theme: 'tooltip' }"
- :title="$t('modal.import_export')"
- svg="archive"
- @click.native="displayModalImportExport(true)"
- />
- </div>
- </div>
- </div>
- <div class="flex-col">
- <CollectionsGraphqlCollection
- v-for="(collection, index) in filteredCollections"
- :key="`collection-${index}`"
- :picked="picked"
- :name="collection.name"
- :collection-index="index"
- :collection="collection"
- :doc="doc"
- :is-filtered="filterText.length > 0"
- :saving-mode="savingMode"
- @edit-collection="editCollection(collection, index)"
- @add-request="addRequest($event)"
- @add-folder="addFolder($event)"
- @edit-folder="editFolder($event)"
- @edit-request="editRequest($event)"
- @duplicate-request="duplicateRequest($event)"
- @select-collection="$emit('use-collection', collection)"
- @select="$emit('select', $event)"
- />
- </div>
- <div
- v-if="collections.length === 0"
- class="flex flex-col items-center justify-center p-4 text-secondaryLight"
- >
- <img
- :src="`/images/states/${$colorMode.value}/pack.svg`"
- loading="lazy"
- class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
- :alt="$t('empty.collections')"
- />
- <span class="pb-4 text-center">
- {{ $t("empty.collections") }}
- </span>
- <ButtonSecondary
- :label="$t('add.new')"
- filled
- @click.native="displayModalAdd(true)"
- />
- </div>
- <div
- v-if="!(filteredCollections.length !== 0 || collections.length === 0)"
- class="flex flex-col items-center justify-center p-4 text-secondaryLight"
- >
- <i class="pb-2 opacity-75 material-icons">manage_search</i>
- <span class="my-2 text-center">
- {{ $t("state.nothing_found") }} "{{ filterText }}"
- </span>
- </div>
- <CollectionsGraphqlAdd
- :show="showModalAdd"
- @hide-modal="displayModalAdd(false)"
- />
- <CollectionsGraphqlEdit
- :show="showModalEdit"
- :editing-collection="editingCollection"
- :editing-collection-index="editingCollectionIndex"
- :editing-collection-name="editingCollection ? editingCollection.name : ''"
- @hide-modal="displayModalEdit(false)"
- />
- <CollectionsGraphqlAddRequest
- :show="showModalAddRequest"
- :folder-path="editingFolderPath"
- @add-request="onAddRequest($event)"
- @hide-modal="displayModalAddRequest(false)"
- />
- <CollectionsGraphqlAddFolder
- :show="showModalAddFolder"
- :folder-path="editingFolderPath"
- @add-folder="onAddFolder($event)"
- @hide-modal="displayModalAddFolder(false)"
- />
- <CollectionsGraphqlEditFolder
- :show="showModalEditFolder"
- :collection-index="editingCollectionIndex"
- :folder="editingFolder"
- :folder-index="editingFolderIndex"
- :folder-path="editingFolderPath"
- :editing-folder-name="editingFolder ? editingFolder.name : ''"
- @hide-modal="displayModalEditFolder(false)"
- />
- <CollectionsGraphqlEditRequest
- :show="showModalEditRequest"
- :folder-path="editingFolderPath"
- :request="editingRequest"
- :request-index="editingRequestIndex"
- :editing-request-name="editingRequest ? editingRequest.name : ''"
- @hide-modal="displayModalEditRequest(false)"
- />
- <CollectionsGraphqlImportExport
- :show="showModalImportExport"
- @hide-modal="displayModalImportExport(false)"
- />
- </div>
- </template>
- <script>
- import { defineComponent } from "@nuxtjs/composition-api"
- import cloneDeep from "lodash/cloneDeep"
- import clone from "lodash/clone"
- import { useReadonlyStream } from "~/helpers/utils/composables"
- import {
- graphqlCollections$,
- addGraphqlFolder,
- saveGraphqlRequestAs,
- } from "~/newstore/collections"
- import { getGQLSession, setGQLSession } from "~/newstore/GQLSession"
- export default defineComponent({
- props: {
- // Whether to activate the ability to pick items (activates 'select' events)
- savingMode: { type: Boolean, default: false },
- doc: { type: Boolean, default: false },
- picked: { type: Object, default: null },
- // Whether to show the 'New' and 'Import/Export' actions
- showCollActions: { type: Boolean, default: true },
- },
- setup() {
- return {
- collections: useReadonlyStream(graphqlCollections$, []),
- }
- },
- data() {
- return {
- showModalAdd: false,
- showModalEdit: false,
- showModalImportExport: false,
- showModalAddRequest: false,
- showModalAddFolder: false,
- showModalEditFolder: false,
- showModalEditRequest: false,
- editingCollection: undefined,
- editingCollectionIndex: undefined,
- editingFolder: undefined,
- editingFolderName: undefined,
- editingFolderIndex: undefined,
- editingFolderPath: undefined,
- editingRequest: undefined,
- editingRequestIndex: undefined,
- filterText: "",
- }
- },
- computed: {
- filteredCollections() {
- const collections = clone(this.collections)
- if (!this.filterText) return collections
- const filterText = this.filterText.toLowerCase()
- const filteredCollections = []
- for (const collection of collections) {
- const filteredRequests = []
- const filteredFolders = []
- for (const request of collection.requests) {
- if (request.name.toLowerCase().includes(filterText))
- filteredRequests.push(request)
- }
- for (const folder of collection.folders) {
- const filteredFolderRequests = []
- for (const request of folder.requests) {
- if (request.name.toLowerCase().includes(filterText))
- filteredFolderRequests.push(request)
- }
- if (filteredFolderRequests.length > 0) {
- const filteredFolder = Object.assign({}, folder)
- filteredFolder.requests = filteredFolderRequests
- filteredFolders.push(filteredFolder)
- }
- }
- if (filteredRequests.length + filteredFolders.length > 0) {
- const filteredCollection = Object.assign({}, collection)
- filteredCollection.requests = filteredRequests
- filteredCollection.folders = filteredFolders
- filteredCollections.push(filteredCollection)
- }
- }
- return filteredCollections
- },
- },
- methods: {
- displayModalAdd(shouldDisplay) {
- this.showModalAdd = shouldDisplay
- },
- displayModalEdit(shouldDisplay) {
- this.showModalEdit = shouldDisplay
- if (!shouldDisplay) this.resetSelectedData()
- },
- displayModalImportExport(shouldDisplay) {
- this.showModalImportExport = shouldDisplay
- },
- displayModalAddRequest(shouldDisplay) {
- this.showModalAddRequest = shouldDisplay
- if (!shouldDisplay) this.resetSelectedData()
- },
- displayModalAddFolder(shouldDisplay) {
- this.showModalAddFolder = shouldDisplay
- if (!shouldDisplay) this.resetSelectedData()
- },
- displayModalEditFolder(shouldDisplay) {
- this.showModalEditFolder = shouldDisplay
- if (!shouldDisplay) this.resetSelectedData()
- },
- displayModalEditRequest(shouldDisplay) {
- this.showModalEditRequest = shouldDisplay
- if (!shouldDisplay) this.resetSelectedData()
- },
- editCollection(collection, collectionIndex) {
- this.$data.editingCollection = collection
- this.$data.editingCollectionIndex = collectionIndex
- this.displayModalEdit(true)
- },
- onAddRequest({ name, path }) {
- const newRequest = {
- ...getGQLSession().request,
- name,
- }
- saveGraphqlRequestAs(path, newRequest)
- setGQLSession({
- request: newRequest,
- schema: "",
- response: "",
- })
- this.displayModalAddRequest(false)
- },
- addRequest(payload) {
- const { path } = payload
- this.$data.editingFolderPath = path
- this.displayModalAddRequest(true)
- },
- onAddFolder({ name, path }) {
- addGraphqlFolder(name, path)
- this.displayModalAddFolder(false)
- },
- addFolder(payload) {
- const { path } = payload
- this.$data.editingFolderPath = path
- this.displayModalAddFolder(true)
- },
- editFolder(payload) {
- const { folder, folderPath } = payload
- this.editingFolder = folder
- this.editingFolderPath = folderPath
- this.displayModalEditFolder(true)
- },
- editRequest(payload) {
- const {
- collectionIndex,
- folderIndex,
- folderName,
- request,
- requestIndex,
- folderPath,
- } = payload
- this.$data.editingFolderPath = folderPath
- this.$data.editingCollectionIndex = collectionIndex
- this.$data.editingFolderIndex = folderIndex
- this.$data.editingFolderName = folderName
- this.$data.editingRequest = request
- this.$data.editingRequestIndex = requestIndex
- this.displayModalEditRequest(true)
- },
- resetSelectedData() {
- this.$data.editingCollection = undefined
- this.$data.editingCollectionIndex = undefined
- this.$data.editingFolder = undefined
- this.$data.editingFolderIndex = undefined
- this.$data.editingRequest = undefined
- this.$data.editingRequestIndex = undefined
- },
- duplicateRequest({ folderPath, request }) {
- saveGraphqlRequestAs(folderPath, {
- ...cloneDeep(request),
- name: `${request.name} - ${this.$t("action.duplicate")}`,
- })
- },
- },
- })
- </script>
|