From a2113a2c6becce30c7c833d64623881067c66f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20K=C3=B6nig?= Date: Sun, 31 Dec 2023 17:09:58 +0100 Subject: [PATCH] fix #180 --- src/components/rest/RestQuery.vue | 7 +++++-- src/components/rest/RestQueryFormTabs.vue | 3 ++- src/composables/components/rest/RestQuery.ts | 20 ++++++------------- .../components/rest/RestQueryTabs.ts | 17 ++++++++++++++++ 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/components/rest/RestQuery.vue b/src/components/rest/RestQuery.vue index 1c19a994..8bdebb7f 100644 --- a/src/components/rest/RestQuery.vue +++ b/src/components/rest/RestQuery.vue @@ -55,7 +55,7 @@ - + @@ -66,8 +66,11 @@ import RestQueryHistoryList from './RestQueryHistoryList.vue' import RestQuerySavedQueriesList from './RestQuerySavedQueriesList.vue' import RestQueryFormTabs from './RestQueryFormTabs.vue' + import { Ref, ref } from 'vue' const t = useTranslation() + const tabs: Ref = ref(null) + const { clusterMinor, history, @@ -80,5 +83,5 @@ toggleSavedQueries, useRequest, useRequestInNewTab - } = useRestQuery() + } = useRestQuery(tabs) diff --git a/src/components/rest/RestQueryFormTabs.vue b/src/components/rest/RestQueryFormTabs.vue index 4eb0dd0a..e09d54e5 100644 --- a/src/components/rest/RestQueryFormTabs.vue +++ b/src/components/rest/RestQueryFormTabs.vue @@ -39,5 +39,6 @@ const emit = defineEmits(['reloadHistory', 'reloadSavedQueries']) - const { tabs, activeTabName, addTab, updateTab, removeTab } = useRestQueryTabs() + const { tabs, activeTabName, addTab, updateTab, removeTab, setTabContent } = useRestQueryTabs() + defineExpose({ setTabContent, addTab }) \ No newline at end of file diff --git a/src/composables/components/rest/RestQuery.ts b/src/composables/components/rest/RestQuery.ts index 90d36487..7198de32 100644 --- a/src/composables/components/rest/RestQuery.ts +++ b/src/composables/components/rest/RestQuery.ts @@ -1,10 +1,10 @@ import { useConnectionStore } from '../../../store/connection.ts' import { computed, ref, Ref, toRaw } from 'vue' import { useIdbStore } from '../../../db/Idb.ts' -import RestQueryFormTabs from '../../../components/rest/RestQueryFormTabs.vue' import { IdbRestQueryHistory, IdbRestQuerySavedQuery, IdbRestQueryTabRequest } from '../../../db/types.ts' +import RestQueryFormTabs from '../../../components/rest/RestQueryFormTabs.vue' -export const useRestQuery = () => { +export const useRestQuery = (formTabs: Ref) => { const { activeCluster } = useConnectionStore() const clusterMinor = computed(() => (activeCluster?.version?.split('.')?.slice(0, 2)?.join('.'))) @@ -18,7 +18,6 @@ export const useRestQuery = () => { const reloadSavedQueries = async () => (savedQueries.value = await restQuerySavedQueries.getAll()) reloadSavedQueries() - const tabs: Ref | null> = ref(null) const historyOpen = ref(false) const savedQueriesOpen = ref(false) const toggleHistory = () => { @@ -31,23 +30,16 @@ export const useRestQuery = () => { } const useRequest = async (request: IdbRestQueryTabRequest) => { - if (!request || !tabs.value) return - - const activeTab = restQueryTabs.all.value[tabs.value.activeTabIndex()] - if (!activeTab) return + if (!request || !formTabs.value) return - const { id, label, name } = activeTab - const obj = Object.assign({}, { id, label, name }, { - request: toRaw(request), - response: { status: '', ok: false, bodyText: '' } - }) + const obj = formTabs.value.setTabContent(toRaw(request)) await restQueryTabs.update(obj) } const useRequestInNewTab = async (request: IdbRestQueryTabRequest) => { - if (!tabs.value) return + if (!formTabs.value) return - await tabs.value.addTab() + await formTabs.value.addTab() await useRequest(request) } diff --git a/src/composables/components/rest/RestQueryTabs.ts b/src/composables/components/rest/RestQueryTabs.ts index f2206f6d..5c5e5ab7 100644 --- a/src/composables/components/rest/RestQueryTabs.ts +++ b/src/composables/components/rest/RestQueryTabs.ts @@ -45,7 +45,24 @@ export const useRestQueryTabs = () => { } loadTabs() + const activeTabIndex = () => (tabs.value.findIndex(t => t.name === activeTabName.value) || 0) + + const setTabContent = ({ method, path, body }: { method: string, path: string, body: string }) => { + const idx = activeTabIndex() + tabs.value[idx].request.method = method + tabs.value[idx].request.path = path + tabs.value[idx].request.body = body + tabs.value[idx].response.status = '' + tabs.value[idx].response.ok = false + tabs.value[idx].response.bodyText = '' + + const obj = toRaw(tabs.value[idx]) + restQueryTabs.update(obj) + return obj + } + return { + setTabContent, tabs, activeTabName, addTab,