diff --git a/src/store/modules/dashboard.ts b/src/store/modules/dashboard.ts index 5612fc60..4961279a 100644 --- a/src/store/modules/dashboard.ts +++ b/src/store/modules/dashboard.ts @@ -33,6 +33,7 @@ import { useSelectorStore } from "@/store/modules/selectors"; import { NewControl } from "../data"; import { Duration } from "@/types/app"; import { AxiosResponse } from "axios"; +import { ElMessage } from "element-plus"; interface DashboardState { showConfig: boolean; layout: LayoutConfig[]; @@ -44,6 +45,7 @@ interface DashboardState { selectorStore: any; showTopology: boolean; currentTabItems: LayoutConfig[]; + dashboards: { name: string; layer: string; entity: string }[]; } export const dashboardStore = defineStore({ @@ -59,6 +61,7 @@ export const dashboardStore = defineStore({ selectorStore: useSelectorStore(), showTopology: false, currentTabItems: [], + dashboards: [], }), actions: { setLayout(data: LayoutConfig[]) { @@ -281,20 +284,12 @@ export const dashboardStore = defineStore({ const res: AxiosResponse = await query(param); return res.data; }, - async getAllTemplates() { + async fetchTemplates() { const res: AxiosResponse = await graphql.query("getTemplates").params({}); if (res.data.errors) { return res.data; } - return res.data; - }, - async fetchTemplates() { - const res = await this.getAllTemplates(); - - if (res.errors) { - return res; - } const data = [ ServiceLayout, AllLayout, @@ -322,7 +317,20 @@ export const dashboardStore = defineStore({ sessionStorage.setItem(key, JSON.stringify(t)); } sessionStorage.setItem("dashboards", JSON.stringify(list)); - return res; + return res.data; + }, + async setDashboards() { + if (!sessionStorage.getItem("dashboards")) { + const res = await this.fetchTemplates(); + if (res.errors) { + this.dashboards = []; + ElMessage.error(res.errors); + return; + } + } + this.dashboards = JSON.parse( + sessionStorage.getItem("dashboards") || "[]" + ); }, }, }); diff --git a/src/views/Service.vue b/src/views/Service.vue index 99e12df1..a82c0969 100644 --- a/src/views/Service.vue +++ b/src/views/Service.vue @@ -81,28 +81,14 @@ const routeNames = [ "ControlPanel", "DataPanel", ]; -const dashboards = ref< - { name: string; layer: string; entity: string; isRoot: boolean }[] ->([]); const layer = ref("GENERAL"); const searchText = ref(""); const services = ref([]); const groups = ref({}); getServices(); -setList(); +dashboardStore.setDashboards(); -async function setList() { - if (!sessionStorage.getItem("dashboards")) { - const res = await dashboardStore.fetchTemplates(); - if (res.errors) { - dashboards.value = []; - ElMessage.error(res.errors); - return; - } - } - dashboards.value = JSON.parse(sessionStorage.getItem("dashboards") || "[]"); -} async function getServices() { setLayer(String(route.name)); const res = await selectorStore.fetchServices(layer.value); @@ -160,7 +146,7 @@ function searchServices() { function visitLayout(row: { id: string }) { const l = - dashboards.value.filter( + dashboardStore.dashboards.filter( (d: { name: string; isRoot: boolean; layer: string; entity: string }) => d.layer === layer.value && d.entity === EntityType[0].value && d.isRoot )[0] || {}; diff --git a/src/views/dashboard/Edit.vue b/src/views/dashboard/Edit.vue index 69ac3066..f9f2afd6 100644 --- a/src/views/dashboard/Edit.vue +++ b/src/views/dashboard/Edit.vue @@ -41,14 +41,12 @@ limitations under the License. --> import { useI18n } from "vue-i18n"; import { useRoute } from "vue-router"; import GridLayout from "./panel/Layout.vue"; -// import { LayoutConfig } from "@/types/dashboard"; import Tool from "./panel/Tool.vue"; import Widget from "./configuration/Widget.vue"; import TopologyConfig from "./configuration/Topology.vue"; import Topology from "./related/topology/Index.vue"; import { useDashboardStore } from "@/store/modules/dashboard"; import { useAppStoreWithOut } from "@/store/modules/app"; -import { ElMessage } from "element-plus"; const dashboardStore = useDashboardStore(); const appStore = useAppStoreWithOut(); @@ -60,14 +58,7 @@ appStore.setPageTitle("Dashboard Name"); setTemplate(); async function setTemplate() { - if (!sessionStorage.getItem("dashboards")) { - const res = await dashboardStore.fetchTemplates(); - if (res.errors) { - dashboardStore.setLayout([]); - ElMessage.error(res.errors); - return; - } - } + await dashboardStore.setDashboards(); const c: { configuration: string; id: string } = JSON.parse( sessionStorage.getItem(layoutKey) || "{}" ); diff --git a/src/views/dashboard/List.vue b/src/views/dashboard/List.vue index 8cf874f6..cd9d943b 100644 --- a/src/views/dashboard/List.vue +++ b/src/views/dashboard/List.vue @@ -49,12 +49,6 @@ limitations under the License. --> {{ t("view") }} - (""); setList(); async function setList() { - if (!sessionStorage.getItem("dashboards")) { - const res = await dashboardStore.fetchTemplates(); - if (res.errors) { - dashboards.value = []; - ElMessage.error(res.errors); - return; - } - } - dashboards.value = JSON.parse(sessionStorage.getItem("dashboards") || "[]"); + await dashboardStore.setDashboards(); + dashboards.value = dashboardStore.dashboards; } const handleEdit = (row: { name: string; layer: string; entity: string }) => { router.push( diff --git a/src/views/dashboard/New.vue b/src/views/dashboard/New.vue index e4081226..3ddb5820 100644 --- a/src/views/dashboard/New.vue +++ b/src/views/dashboard/New.vue @@ -51,15 +51,17 @@ limitations under the License. -->