mirror of https://github.com/halo-dev/halo-admin
refactor: theme settings page form
Signed-off-by: Ryan Wang <i@ryanc.cc>pull/594/head
parent
7bde20a515
commit
9f3ccb6af1
|
@ -0,0 +1,126 @@
|
||||||
|
// core libs
|
||||||
|
import { ref } from "vue";
|
||||||
|
import { apiClient } from "@halo-dev/admin-shared";
|
||||||
|
|
||||||
|
// libs
|
||||||
|
import cloneDeep from "lodash.clonedeep";
|
||||||
|
|
||||||
|
// types
|
||||||
|
import type { Ref } from "vue";
|
||||||
|
import type {
|
||||||
|
FormKitSetting,
|
||||||
|
FormKitSettingSpec,
|
||||||
|
} from "@halo-dev/admin-shared";
|
||||||
|
import type { ConfigMap } from "@halo-dev/api-client";
|
||||||
|
|
||||||
|
const initialConfigMap: ConfigMap = {
|
||||||
|
apiVersion: "v1alpha1",
|
||||||
|
kind: "ConfigMap",
|
||||||
|
metadata: {
|
||||||
|
name: "",
|
||||||
|
},
|
||||||
|
data: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useSettingForm(
|
||||||
|
settingName: Ref<string | undefined>,
|
||||||
|
configMapName: Ref<string | undefined>
|
||||||
|
) {
|
||||||
|
const settings = ref<FormKitSetting | undefined>();
|
||||||
|
const configMap = ref<ConfigMap>(cloneDeep(initialConfigMap));
|
||||||
|
const configMapFormData = ref<
|
||||||
|
Record<string, Record<string, string>> | undefined
|
||||||
|
>();
|
||||||
|
const saving = ref(false);
|
||||||
|
|
||||||
|
const handleFetchSettings = async () => {
|
||||||
|
if (!settingName.value) {
|
||||||
|
settings.value = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await apiClient.extension.setting.getv1alpha1Setting(
|
||||||
|
settingName.value
|
||||||
|
);
|
||||||
|
settings.value = response.data as FormKitSetting;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFetchConfigMap = async () => {
|
||||||
|
if (!configMapName.value) {
|
||||||
|
configMap.value = cloneDeep(initialConfigMap);
|
||||||
|
configMapFormData.value = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await apiClient.extension.configMap.getv1alpha1ConfigMap(
|
||||||
|
configMapName.value
|
||||||
|
);
|
||||||
|
configMap.value = response.data;
|
||||||
|
|
||||||
|
const { data } = configMap.value;
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
configMapFormData.value = Object.keys(data).reduce((acc, key) => {
|
||||||
|
acc[key] = JSON.parse(data[key]);
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
} finally {
|
||||||
|
if (!configMapFormData.value) {
|
||||||
|
configMapFormData.value = settings.value?.spec.reduce((acc, item) => {
|
||||||
|
acc[item.group] = {};
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveConfigMap = async () => {
|
||||||
|
try {
|
||||||
|
saving.value = true;
|
||||||
|
|
||||||
|
if (!configMap.value.metadata.name && configMapName.value) {
|
||||||
|
configMap.value.metadata.name = configMapName.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
settings?.value?.spec.forEach((item: FormKitSettingSpec) => {
|
||||||
|
// @ts-ignore
|
||||||
|
configMap.value.data[item.group] = JSON.stringify(
|
||||||
|
configMapFormData?.value?.[item.group]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!configMap.value.metadata.creationTimestamp) {
|
||||||
|
await apiClient.extension.configMap.createv1alpha1ConfigMap(
|
||||||
|
configMap.value
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await apiClient.extension.configMap.updatev1alpha1ConfigMap(
|
||||||
|
configMap.value.metadata.name,
|
||||||
|
configMap.value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
} finally {
|
||||||
|
await handleFetchSettings();
|
||||||
|
await handleFetchConfigMap();
|
||||||
|
saving.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
settings,
|
||||||
|
configMap,
|
||||||
|
configMapFormData,
|
||||||
|
saving,
|
||||||
|
handleFetchSettings,
|
||||||
|
handleFetchConfigMap,
|
||||||
|
handleSaveConfigMap,
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,8 +1,13 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { VAlert, VSpace, VTag } from "@halo-dev/components";
|
// core libs
|
||||||
import type { ComputedRef, Ref } from "vue";
|
|
||||||
import { computed, inject, ref } from "vue";
|
import { computed, inject, ref } from "vue";
|
||||||
import { RouterLink } from "vue-router";
|
import { RouterLink } from "vue-router";
|
||||||
|
|
||||||
|
// components
|
||||||
|
import { VAlert, VSpace, VTag } from "@halo-dev/components";
|
||||||
|
|
||||||
|
// types
|
||||||
|
import type { ComputedRef, Ref } from "vue";
|
||||||
import type { Theme } from "@halo-dev/api-client";
|
import type { Theme } from "@halo-dev/api-client";
|
||||||
|
|
||||||
const selectedTheme = inject<Ref<Theme>>(
|
const selectedTheme = inject<Ref<Theme>>(
|
||||||
|
|
|
@ -1,24 +1,30 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { Ref } from "vue";
|
// core libs
|
||||||
import { computed, inject, ref } from "vue";
|
import { computed, inject, onMounted, ref } from "vue";
|
||||||
import { VButton } from "@halo-dev/components";
|
|
||||||
import { apiClient } from "@halo-dev/admin-shared";
|
// components
|
||||||
import type { ConfigMap, Theme } from "@halo-dev/api-client";
|
import { VButton } from "@halo-dev/components";
|
||||||
import type {
|
|
||||||
FormKitSetting,
|
// types
|
||||||
FormKitSettingSpec,
|
import type { Ref } from "vue";
|
||||||
} from "@halo-dev/admin-shared/src";
|
import type { Theme } from "@halo-dev/api-client";
|
||||||
|
|
||||||
|
// hooks
|
||||||
|
import { useSettingForm } from "@/composables/use-setting-form";
|
||||||
|
|
||||||
const settings = inject<Ref<FormKitSetting | undefined>>("settings");
|
|
||||||
const configmapFormData =
|
|
||||||
inject<Ref<Record<string, Record<string, string>> | undefined>>(
|
|
||||||
"configmapFormData"
|
|
||||||
);
|
|
||||||
const configmap = inject<Ref<ConfigMap>>("configmap", {} as Ref<ConfigMap>);
|
|
||||||
const selectedTheme = inject<Ref<Theme>>("selectedTheme", ref({} as Theme));
|
const selectedTheme = inject<Ref<Theme>>("selectedTheme", ref({} as Theme));
|
||||||
const group = inject<Ref<string | undefined>>("activeTab");
|
const group = inject<Ref<string | undefined>>("activeTab");
|
||||||
|
|
||||||
const saving = ref(false);
|
const settingName = computed(() => selectedTheme.value.spec?.settingName);
|
||||||
|
const configMapName = computed(() => selectedTheme.value.spec?.configMapName);
|
||||||
|
const {
|
||||||
|
settings,
|
||||||
|
configMapFormData,
|
||||||
|
saving,
|
||||||
|
handleFetchConfigMap,
|
||||||
|
handleFetchSettings,
|
||||||
|
handleSaveConfigMap,
|
||||||
|
} = useSettingForm(settingName, configMapName);
|
||||||
|
|
||||||
const formSchema = computed(() => {
|
const formSchema = computed(() => {
|
||||||
if (!settings?.value?.spec) {
|
if (!settings?.value?.spec) {
|
||||||
|
@ -28,53 +34,18 @@ const formSchema = computed(() => {
|
||||||
?.formSchema;
|
?.formSchema;
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleFetchSettings = inject<() => void>("handleFetchSettings");
|
onMounted(() => {
|
||||||
const handleFetchConfigMap = inject<() => void>("handleFetchConfigMap");
|
handleFetchSettings();
|
||||||
|
handleFetchConfigMap();
|
||||||
const handleSaveConfigMap = async () => {
|
});
|
||||||
try {
|
|
||||||
saving.value = true;
|
|
||||||
|
|
||||||
if (
|
|
||||||
!configmap.value.metadata.name &&
|
|
||||||
selectedTheme.value.spec.configMapName
|
|
||||||
) {
|
|
||||||
configmap.value.metadata.name = selectedTheme.value.spec.configMapName;
|
|
||||||
}
|
|
||||||
|
|
||||||
settings?.value?.spec.forEach((item: FormKitSettingSpec) => {
|
|
||||||
// @ts-ignore
|
|
||||||
configmap.value.data[item.group] = JSON.stringify(
|
|
||||||
configmapFormData?.value?.[item.group]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!configmap.value.metadata.creationTimestamp) {
|
|
||||||
await apiClient.extension.configMap.createv1alpha1ConfigMap(
|
|
||||||
configmap.value
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
await apiClient.extension.configMap.updatev1alpha1ConfigMap(
|
|
||||||
configmap.value.metadata.name,
|
|
||||||
configmap.value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
} finally {
|
|
||||||
handleFetchSettings?.();
|
|
||||||
handleFetchConfigMap?.();
|
|
||||||
saving.value = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<div class="bg-white p-4 sm:px-6">
|
<div class="bg-white p-4 sm:px-6">
|
||||||
<div class="w-1/3">
|
<div class="w-1/3">
|
||||||
<FormKit
|
<FormKit
|
||||||
v-if="group && formSchema && configmapFormData"
|
v-if="group && formSchema && configMapFormData"
|
||||||
:id="group"
|
:id="group"
|
||||||
v-model="configmapFormData[group]"
|
v-model="configMapFormData[group]"
|
||||||
:actions="false"
|
:actions="false"
|
||||||
:preserve="true"
|
:preserve="true"
|
||||||
type="form"
|
type="form"
|
||||||
|
|
|
@ -1,4 +1,16 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
// core libs
|
||||||
|
import { computed, provide, ref, watch, watchEffect } from "vue";
|
||||||
|
import { useRoute, useRouter } from "vue-router";
|
||||||
|
|
||||||
|
// libs
|
||||||
|
import cloneDeep from "lodash.clonedeep";
|
||||||
|
|
||||||
|
// hooks
|
||||||
|
import { useThemeLifeCycle } from "../composables/use-theme";
|
||||||
|
import { useSettingForm } from "@/composables/use-setting-form";
|
||||||
|
|
||||||
|
// components
|
||||||
import {
|
import {
|
||||||
IconExchange,
|
IconExchange,
|
||||||
IconEye,
|
IconEye,
|
||||||
|
@ -9,24 +21,21 @@ import {
|
||||||
VSpace,
|
VSpace,
|
||||||
VTabbar,
|
VTabbar,
|
||||||
} from "@halo-dev/components";
|
} from "@halo-dev/components";
|
||||||
import type {
|
|
||||||
FormKitSetting,
|
|
||||||
FormKitSettingSpec,
|
|
||||||
} from "@halo-dev/admin-shared";
|
|
||||||
import { apiClient, BasicLayout } from "@halo-dev/admin-shared";
|
|
||||||
import ThemeListModal from "../components/ThemeListModal.vue";
|
import ThemeListModal from "../components/ThemeListModal.vue";
|
||||||
|
import { BasicLayout } from "@halo-dev/admin-shared";
|
||||||
|
|
||||||
|
// types
|
||||||
|
import type { FormKitSettingSpec } from "@halo-dev/admin-shared";
|
||||||
import type { ComputedRef, Ref } from "vue";
|
import type { ComputedRef, Ref } from "vue";
|
||||||
import { provide, ref, watch, watchEffect } from "vue";
|
import type { Theme } from "@halo-dev/api-client";
|
||||||
import type { RouteLocationRaw } from "vue-router";
|
|
||||||
import { useRoute, useRouter } from "vue-router";
|
|
||||||
import type { ConfigMap, Theme } from "@halo-dev/api-client";
|
|
||||||
import { useThemeLifeCycle } from "../composables/use-theme";
|
|
||||||
import cloneDeep from "lodash.clonedeep";
|
|
||||||
|
|
||||||
interface ThemeTab {
|
interface ThemeTab {
|
||||||
id: string;
|
id: string;
|
||||||
label: string;
|
label: string;
|
||||||
route: RouteLocationRaw;
|
route: {
|
||||||
|
name: string;
|
||||||
|
params?: Record<string, string>;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialTabs: ThemeTab[] = [
|
const initialTabs: ThemeTab[] = [
|
||||||
|
@ -39,36 +48,24 @@ const initialTabs: ThemeTab[] = [
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const initialConfigMap: ConfigMap = {
|
|
||||||
data: {},
|
|
||||||
apiVersion: "v1alpha1",
|
|
||||||
kind: "ConfigMap",
|
|
||||||
metadata: {
|
|
||||||
name: "",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const tabs = ref<ThemeTab[]>(cloneDeep(initialTabs));
|
const tabs = ref<ThemeTab[]>(cloneDeep(initialTabs));
|
||||||
const selectedTheme = ref<Theme>({} as Theme);
|
const selectedTheme = ref<Theme>({} as Theme);
|
||||||
const settings = ref<FormKitSetting | undefined>();
|
|
||||||
const configmapFormData = ref<
|
|
||||||
Record<string, Record<string, string>> | undefined
|
|
||||||
>();
|
|
||||||
const configmap = ref<ConfigMap>(cloneDeep(initialConfigMap));
|
|
||||||
const themesModal = ref(false);
|
const themesModal = ref(false);
|
||||||
const activeTab = ref("detail");
|
const activeTab = ref("");
|
||||||
|
|
||||||
const { isActivated, activatedTheme, handleActiveTheme } =
|
const { isActivated, activatedTheme, handleActiveTheme } =
|
||||||
useThemeLifeCycle(selectedTheme);
|
useThemeLifeCycle(selectedTheme);
|
||||||
|
|
||||||
|
const settingName = computed(() => selectedTheme.value.spec?.settingName);
|
||||||
|
const configMapName = computed(() => selectedTheme.value.spec?.configMapName);
|
||||||
|
|
||||||
|
const { settings, handleFetchSettings } = useSettingForm(
|
||||||
|
settingName,
|
||||||
|
configMapName
|
||||||
|
);
|
||||||
|
|
||||||
provide<Ref<Theme>>("activatedTheme", activatedTheme);
|
provide<Ref<Theme>>("activatedTheme", activatedTheme);
|
||||||
provide<Ref<Theme>>("selectedTheme", selectedTheme);
|
provide<Ref<Theme>>("selectedTheme", selectedTheme);
|
||||||
provide<Ref<FormKitSetting | undefined>>("settings", settings);
|
|
||||||
provide<Ref<Record<string, Record<string, string>> | undefined>>(
|
|
||||||
"configmapFormData",
|
|
||||||
configmapFormData
|
|
||||||
);
|
|
||||||
provide<Ref<ConfigMap>>("configmap", configmap);
|
|
||||||
provide<ComputedRef<boolean>>("isActivated", isActivated);
|
provide<ComputedRef<boolean>>("isActivated", isActivated);
|
||||||
provide<Ref<string | undefined>>("activeTab", activeTab);
|
provide<Ref<string | undefined>>("activeTab", activeTab);
|
||||||
|
|
||||||
|
@ -82,23 +79,16 @@ const handleTabChange = (id: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleFetchSettings = async () => {
|
watchEffect(async () => {
|
||||||
tabs.value = cloneDeep(initialTabs);
|
if (selectedTheme.value) {
|
||||||
if (!selectedTheme.value?.spec?.settingName) {
|
// reset tabs
|
||||||
return;
|
tabs.value = cloneDeep(initialTabs);
|
||||||
}
|
await handleFetchSettings();
|
||||||
try {
|
|
||||||
const response = await apiClient.extension.setting.getv1alpha1Setting(
|
|
||||||
selectedTheme.value.spec.settingName as string
|
|
||||||
);
|
|
||||||
settings.value = response.data as FormKitSetting;
|
|
||||||
|
|
||||||
const { spec } = settings.value;
|
if (settings.value && settings.value.spec) {
|
||||||
|
|
||||||
if (spec) {
|
|
||||||
tabs.value = [
|
tabs.value = [
|
||||||
...tabs.value,
|
...tabs.value,
|
||||||
...spec.map((item: FormKitSettingSpec) => {
|
...settings.value.spec.map((item: FormKitSettingSpec) => {
|
||||||
return {
|
return {
|
||||||
id: item.group,
|
id: item.group,
|
||||||
label: item.label || "",
|
label: item.label || "",
|
||||||
|
@ -111,51 +101,8 @@ const handleFetchSettings = async () => {
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
] as ThemeTab[];
|
] as ThemeTab[];
|
||||||
|
|
||||||
onTabChange(route.name as string);
|
onTabChange(route.name as string);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFetchConfigMap = async () => {
|
|
||||||
if (!selectedTheme.value.spec?.configMapName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const response = await apiClient.extension.configMap.getv1alpha1ConfigMap(
|
|
||||||
selectedTheme.value.spec?.configMapName as string
|
|
||||||
);
|
|
||||||
configmap.value = response.data;
|
|
||||||
|
|
||||||
const { data } = configmap.value;
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
configmapFormData.value = Object.keys(data).reduce((acc, key) => {
|
|
||||||
acc[key] = JSON.parse(data[key]);
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
} finally {
|
|
||||||
if (!configmapFormData.value) {
|
|
||||||
configmapFormData.value = settings.value?.spec.reduce((acc, item) => {
|
|
||||||
acc[item.group] = {};
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
provide<() => void>("handleFetchSettings", handleFetchSettings);
|
|
||||||
provide<() => void>("handleFetchConfigMap", handleFetchConfigMap);
|
|
||||||
|
|
||||||
watchEffect(() => {
|
|
||||||
if (selectedTheme.value) {
|
|
||||||
handleFetchSettings();
|
|
||||||
handleFetchConfigMap();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -163,10 +110,8 @@ const onTabChange = (routeName: string) => {
|
||||||
if (routeName === "ThemeSetting") {
|
if (routeName === "ThemeSetting") {
|
||||||
const tab = tabs.value.find((tab) => {
|
const tab = tabs.value.find((tab) => {
|
||||||
return (
|
return (
|
||||||
// @ts-ignore
|
|
||||||
tab.route.name === routeName &&
|
tab.route.name === routeName &&
|
||||||
// @ts-ignore
|
tab.route.params?.group === route.params.group
|
||||||
tab.route.params.group === route.params.group
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
if (tab) {
|
if (tab) {
|
||||||
|
|
Loading…
Reference in New Issue