mirror of https://github.com/halo-dev/halo
refactor: use tanstack query to refactor system setting form fetching (#3606)
#### What type of PR is this? /kind improvement #### What this PR does / why we need it: 使用 [TanStack Query](https://github.com/TanStack/query) 重构系统设置表单的逻辑,移除无意义的重复请求。 #### Which issue(s) this PR fixes: Ref https://github.com/halo-dev/halo/issues/3360 #### Special notes for your reviewer: 测试方式: 1. 测试系统设置的表单加载是否正常,以及保存之后重载配置是否正常。 #### Does this PR introduce a user-facing change? ```release-note None ```pull/3626/head
parent
1d9c7343fc
commit
a77756abad
|
@ -1,58 +1,86 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
// core libs
|
// core libs
|
||||||
import { computed, ref } from "vue";
|
import { computed, ref, type Ref, inject } from "vue";
|
||||||
|
|
||||||
// components
|
// components
|
||||||
import { VButton } from "@halo-dev/components";
|
import { Toast, VButton } from "@halo-dev/components";
|
||||||
|
|
||||||
// hooks
|
// hooks
|
||||||
import { useSettingForm } from "@/composables/use-setting-form";
|
import { useSettingFormConvert } from "@/composables/use-setting-form";
|
||||||
import { useRouteParams } from "@vueuse/router";
|
import { useRouteParams } from "@vueuse/router";
|
||||||
import type { FormKitSchemaCondition, FormKitSchemaNode } from "@formkit/core";
|
|
||||||
import { useSystemConfigMapStore } from "@/stores/system-configmap";
|
import { useSystemConfigMapStore } from "@/stores/system-configmap";
|
||||||
|
import type { ConfigMap, Setting } from "@halo-dev/api-client";
|
||||||
|
import { useQuery, useQueryClient } from "@tanstack/vue-query";
|
||||||
|
import { apiClient } from "@/utils/api-client";
|
||||||
|
import { useI18n } from "vue-i18n";
|
||||||
|
|
||||||
|
const SYSTEM_CONFIGMAP_NAME = "system";
|
||||||
|
|
||||||
|
const { t } = useI18n();
|
||||||
|
const systemConfigMapStore = useSystemConfigMapStore();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const saving = ref(false);
|
||||||
const group = useRouteParams<string>("group");
|
const group = useRouteParams<string>("group");
|
||||||
|
const setting = inject<Ref<Setting | undefined>>("setting", ref());
|
||||||
|
|
||||||
const {
|
const { data: configMap, suspense } = useQuery<ConfigMap>({
|
||||||
setting,
|
queryKey: ["system-configMap"],
|
||||||
configMapFormData,
|
queryFn: async () => {
|
||||||
saving,
|
const { data } = await apiClient.extension.configMap.getv1alpha1ConfigMap({
|
||||||
handleFetchConfigMap,
|
name: SYSTEM_CONFIGMAP_NAME,
|
||||||
handleFetchSettings,
|
});
|
||||||
handleSaveConfigMap,
|
return data;
|
||||||
} = useSettingForm(ref("system"), ref("system"));
|
},
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
const formSchema = computed(() => {
|
enabled: computed(() => !!setting.value),
|
||||||
if (!setting.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return setting.value.spec.forms.find((item) => item.group === group?.value)
|
|
||||||
?.formSchema as (FormKitSchemaCondition | FormKitSchemaNode)[];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const systemConfigMapStore = useSystemConfigMapStore();
|
const { configMapFormData, formSchema, convertToSave } = useSettingFormConvert(
|
||||||
|
setting,
|
||||||
|
configMap,
|
||||||
|
group
|
||||||
|
);
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSaveConfigMap = async () => {
|
||||||
await handleSaveConfigMap();
|
saving.value = true;
|
||||||
await systemConfigMapStore.fetchSystemConfigMap();
|
|
||||||
|
const configMapToUpdate = convertToSave();
|
||||||
|
|
||||||
|
if (!configMapToUpdate) {
|
||||||
|
saving.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await apiClient.extension.configMap.updatev1alpha1ConfigMap({
|
||||||
|
name: SYSTEM_CONFIGMAP_NAME,
|
||||||
|
configMap: configMapToUpdate,
|
||||||
|
});
|
||||||
|
|
||||||
|
Toast.success(t("core.common.toast.save_success"));
|
||||||
|
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["system-configMap"] });
|
||||||
|
|
||||||
|
systemConfigMapStore.configMap = data;
|
||||||
|
|
||||||
|
saving.value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
await handleFetchSettings();
|
await suspense();
|
||||||
await handleFetchConfigMap();
|
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<Transition mode="out-in" name="fade">
|
<Transition mode="out-in" name="fade">
|
||||||
<div class="bg-white p-4">
|
<div class="bg-white p-4">
|
||||||
<div>
|
<div>
|
||||||
<FormKit
|
<FormKit
|
||||||
v-if="group && formSchema && configMapFormData"
|
v-if="group && formSchema && configMapFormData?.[group]"
|
||||||
:id="group"
|
:id="group"
|
||||||
v-model="configMapFormData[group]"
|
v-model="configMapFormData[group]"
|
||||||
:name="group"
|
:name="group"
|
||||||
:actions="false"
|
:actions="false"
|
||||||
:preserve="true"
|
:preserve="true"
|
||||||
type="form"
|
type="form"
|
||||||
@submit="handleSave"
|
@submit="handleSaveConfigMap"
|
||||||
>
|
>
|
||||||
<FormKitSchema
|
<FormKitSchema
|
||||||
:schema="formSchema"
|
:schema="formSchema"
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
// core libs
|
// core libs
|
||||||
import { nextTick, onMounted } from "vue";
|
import { nextTick, ref, watch, type Ref, provide } from "vue";
|
||||||
import { ref, watch } from "vue";
|
|
||||||
import { useRoute, useRouter } from "vue-router";
|
import { useRoute, useRouter } from "vue-router";
|
||||||
|
|
||||||
// types
|
// types
|
||||||
import BasicLayout from "@/layouts/BasicLayout.vue";
|
import BasicLayout from "@/layouts/BasicLayout.vue";
|
||||||
import { useSettingForm } from "@/composables/use-setting-form";
|
|
||||||
|
|
||||||
// components
|
// components
|
||||||
import {
|
import {
|
||||||
|
@ -16,7 +14,12 @@ import {
|
||||||
IconSettings,
|
IconSettings,
|
||||||
VLoading,
|
VLoading,
|
||||||
} from "@halo-dev/components";
|
} from "@halo-dev/components";
|
||||||
import type { SettingForm } from "@halo-dev/api-client";
|
import type { Setting, SettingForm } from "@halo-dev/api-client";
|
||||||
|
import { useQuery } from "@tanstack/vue-query";
|
||||||
|
import { apiClient } from "@/utils/api-client";
|
||||||
|
import { useI18n } from "vue-i18n";
|
||||||
|
|
||||||
|
const { t } = useI18n();
|
||||||
|
|
||||||
interface SettingTab {
|
interface SettingTab {
|
||||||
id: string;
|
id: string;
|
||||||
|
@ -27,17 +30,52 @@ interface SettingTab {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const tabs = ref<SettingTab[]>([] as SettingTab[]);
|
const tabs = ref<SettingTab[]>([
|
||||||
const activeTab = ref("");
|
{
|
||||||
|
id: "loading",
|
||||||
const { setting, handleFetchSettings } = useSettingForm(
|
label: t("core.common.status.loading"),
|
||||||
ref("system"),
|
route: { name: "SystemSetting" },
|
||||||
ref("system")
|
},
|
||||||
);
|
]);
|
||||||
|
const activeTab = ref(tabs.value[0].id);
|
||||||
|
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
const { data: setting } = useQuery({
|
||||||
|
queryKey: ["system-setting"],
|
||||||
|
queryFn: async () => {
|
||||||
|
const { data } = await apiClient.extension.setting.getv1alpha1Setting({
|
||||||
|
name: "system",
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
async onSuccess(data) {
|
||||||
|
if (data) {
|
||||||
|
const { forms } = data.spec;
|
||||||
|
tabs.value = forms.map((item: SettingForm) => {
|
||||||
|
return {
|
||||||
|
id: item.group,
|
||||||
|
label: item.label || "",
|
||||||
|
route: {
|
||||||
|
name: "SystemSetting",
|
||||||
|
params: {
|
||||||
|
group: item.group,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
handleTriggerTabChange();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
provide<Ref<Setting | undefined>>("setting", setting);
|
||||||
|
|
||||||
const handleTabChange = (id: string) => {
|
const handleTabChange = (id: string) => {
|
||||||
const tab = tabs.value.find((item) => item.id === id);
|
const tab = tabs.value.find((item) => item.id === id);
|
||||||
if (tab) {
|
if (tab) {
|
||||||
|
@ -46,30 +84,6 @@ const handleTabChange = (id: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
await handleFetchSettings();
|
|
||||||
|
|
||||||
if (setting.value) {
|
|
||||||
const { forms } = setting.value.spec;
|
|
||||||
tabs.value = forms.map((item: SettingForm) => {
|
|
||||||
return {
|
|
||||||
id: item.group,
|
|
||||||
label: item.label || "",
|
|
||||||
route: {
|
|
||||||
name: "SystemSetting",
|
|
||||||
params: {
|
|
||||||
group: item.group,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await nextTick();
|
|
||||||
|
|
||||||
handleTriggerTabChange();
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleTriggerTabChange = () => {
|
const handleTriggerTabChange = () => {
|
||||||
const tab = tabs.value.find((tab) => {
|
const tab = tabs.value.find((tab) => {
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in New Issue