From dc1da8fc91c7a356a135ca5a1a7b292fc7073938 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 12 Jul 2022 20:32:32 +0800 Subject: [PATCH] feat: add settings support to the plugin Signed-off-by: Ryan Wang --- src/modules/system/plugins/PluginDetail.vue | 149 +++++++++++++++++--- src/types/extension.d.ts | 23 +++ 2 files changed, 154 insertions(+), 18 deletions(-) diff --git a/src/modules/system/plugins/PluginDetail.vue b/src/modules/system/plugins/PluginDetail.vue index 1688c677..c734e771 100644 --- a/src/modules/system/plugins/PluginDetail.vue +++ b/src/modules/system/plugins/PluginDetail.vue @@ -11,12 +11,30 @@ import { } from "@halo-dev/components"; import { useRoute } from "vue-router"; import { computed, onMounted, ref } from "vue"; -import type { Plugin } from "@/types/extension"; +import type { + ConfigMap, + Plugin, + Setting, + SettingSpec, +} from "@/types/extension"; import { axiosInstance } from "@halo-dev/admin-shared"; import cloneDeep from "lodash.clonedeep"; -const pluginActiveId = ref("detail"); +const pageTabs = ref([{ id: "detail", label: "详情" }]); +const activeTabId = ref(pageTabs.value[0].id); const plugin = ref({} as Plugin); +const settings = ref({} as Setting); + +const configmapFormData = ref>>({}); +const configmap = ref({ + data: {}, + apiVersion: "v1alpha1", + kind: "ConfigMap", + metadata: { + name: "", + }, +}); +const saving = ref(false); const { params } = useRoute(); const dialog = useDialog(); @@ -27,11 +45,90 @@ const handleFetchPlugin = async () => { `/apis/plugin.halo.run/v1alpha1/plugins/${params.pluginName}` ); plugin.value = response.data; + + await handleFetchSettings(); + await handleFetchConfigMap(); } catch (e) { console.error(e); } }; +const handleFetchSettings = async () => { + try { + const response = await axiosInstance.get( + `/api/v1alpha1/settings/${plugin.value.spec.settingName}` + ); + settings.value = response.data; + + const { spec } = settings.value; + + if (spec) { + pageTabs.value = [ + ...pageTabs.value, + ...spec.map((item: SettingSpec) => { + return { + id: item.group, + label: item.label, + }; + }), + ]; + + spec.forEach((item: SettingSpec) => { + configmapFormData.value[item.group] = {}; + }); + } + } catch (e) { + console.error(e); + } +}; + +const handleFetchConfigMap = async () => { + try { + const response = await axiosInstance.get( + `/api/v1alpha1/configmaps/${plugin.value.spec.configmapName}` + ); + configmap.value = response.data; + + const { data } = configmap.value; + + Object.keys(data).forEach((key) => { + configmapFormData.value[key] = JSON.parse(data[key]); + }); + } catch (e) { + console.error(e); + } +}; + +const handleSaveConfigMap = async () => { + try { + saving.value = true; + + if (!configmap.value.metadata.name && plugin.value.spec.configmapName) { + configmap.value.metadata.name = plugin.value.spec.configmapName; + } + + settings.value.spec.forEach((item: SettingSpec) => { + configmap.value.data[item.group] = JSON.stringify( + configmapFormData.value[item.group] + ); + }); + + if (!configmap.value.metadata.creationTimestamp) { + await axiosInstance.post(`/api/v1alpha1/configmaps`, configmap.value); + } else { + await axiosInstance.put( + `/api/v1alpha1/configmaps/${configmap.value.metadata.name}`, + configmap.value + ); + } + } catch (e) { + console.error(e); + } finally { + await handleFetchConfigMap(); + saving.value = false; + } +}; + const isStarted = computed(() => { return plugin.value.status?.phase === "STARTED" && plugin.value.spec.enabled; }); @@ -74,17 +171,14 @@ onMounted(handleFetchPlugin); -
+

@@ -303,18 +397,37 @@ onMounted(handleFetchPlugin);

-
- - - - - -
-
- 保存 +
diff --git a/src/types/extension.d.ts b/src/types/extension.d.ts index 4a63ecf0..51f419d1 100644 --- a/src/types/extension.d.ts +++ b/src/types/extension.d.ts @@ -1,4 +1,5 @@ import type { Plugin as PluginModule } from "@halo-dev/admin-shared"; +import type { FormKitSchemaCondition, FormKitSchemaNode } from "@formkit/core"; export interface License { name?: string; @@ -47,6 +48,8 @@ export interface PluginSpec { pluginClass?: string; enabled?: boolean; module?: PluginModule; + settingName?: string; + configmapName?: string; } export interface PluginStatus { @@ -167,3 +170,23 @@ export interface ReverseProxyRule { path?: string; file?: FileReverseProxyProvider; } + +export interface Setting { + spec: SettingSpec[]; + apiVersion: string; + kind: string; + metadata: Metadata; +} + +export interface SettingSpec { + group: string; + label: string; + formSchema: FormKitSchemaCondition | FormKitSchemaNode[]; +} + +export interface ConfigMap { + data: Record; + apiVersion: string; + kind: string; + metadata: Metadata; +}