mirror of https://github.com/halo-dev/halo
refactor: migrate system settings to new configmap API (#7524)
#### What type of PR is this? /area ui /kind improvement /milestone 2.21.x #### What this PR does / why we need it: Migrate system settings to new configmap API #### Which issue(s) this PR fixes: Fixes #7515 #### Special notes for your reviewer: Need to test whether the system setting is working properly #### Does this PR introduce a user-facing change? ```release-note None ```pull/7527/head
parent
677caca403
commit
224d78079d
|
@ -1,102 +1,108 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
// core libs
|
import HasPermission from "@/components/permission/HasPermission.vue";
|
||||||
import { computed, inject, ref, toRaw, type Ref } from "vue";
|
|
||||||
|
|
||||||
// components
|
|
||||||
import StickyBlock from "@/components/sticky-block/StickyBlock.vue";
|
import StickyBlock from "@/components/sticky-block/StickyBlock.vue";
|
||||||
import { Toast, VButton } from "@halo-dev/components";
|
|
||||||
|
|
||||||
// hooks
|
|
||||||
import { useGlobalInfoStore } from "@/stores/global-info";
|
import { useGlobalInfoStore } from "@/stores/global-info";
|
||||||
import { useSettingFormConvert } from "@console/composables/use-setting-form";
|
import type { FormKitSchemaCondition, FormKitSchemaNode } from "@formkit/core";
|
||||||
import type { ConfigMap, Setting } from "@halo-dev/api-client";
|
import type { Setting } from "@halo-dev/api-client";
|
||||||
import { coreApiClient } from "@halo-dev/api-client";
|
import { consoleApiClient } from "@halo-dev/api-client";
|
||||||
|
import { Toast, VButton, VLoading } from "@halo-dev/components";
|
||||||
import { useQuery, useQueryClient } from "@tanstack/vue-query";
|
import { useQuery, useQueryClient } from "@tanstack/vue-query";
|
||||||
|
import { computed, inject, ref, toRaw, type Ref } from "vue";
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
|
|
||||||
const SYSTEM_CONFIGMAP_NAME = "system";
|
|
||||||
|
|
||||||
const { t, locale } = useI18n();
|
const { t, locale } = useI18n();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const group = inject<Ref<string>>("activeTab", ref("basic"));
|
const group = inject<Ref<string>>("activeTab", ref("basic"));
|
||||||
const setting = inject<Ref<Setting | undefined>>("setting", ref());
|
const setting = inject<Ref<Setting | undefined>>("setting", ref());
|
||||||
const saving = ref(false);
|
const isSubmitting = ref(false);
|
||||||
|
|
||||||
const { data: configMap } = useQuery<ConfigMap>({
|
const { data: configMapGroupData, isLoading } = useQuery({
|
||||||
queryKey: ["system-configMap"],
|
queryKey: ["core:system:configMap:group-data", group],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const { data } = await coreApiClient.configMap.getConfigMap({
|
const { data } =
|
||||||
name: SYSTEM_CONFIGMAP_NAME,
|
await consoleApiClient.configMap.system.getSystemConfigByGroup({
|
||||||
|
group: group.value,
|
||||||
});
|
});
|
||||||
return data;
|
return data as Record<string, unknown>;
|
||||||
},
|
},
|
||||||
enabled: computed(() => !!setting.value),
|
enabled: computed(() => !!group.value),
|
||||||
});
|
});
|
||||||
|
|
||||||
const { configMapFormData, formSchema, convertToSave } = useSettingFormConvert(
|
const formSchema = computed(() => {
|
||||||
setting,
|
if (!setting.value) {
|
||||||
configMap,
|
|
||||||
group
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleSaveConfigMap = async () => {
|
|
||||||
saving.value = true;
|
|
||||||
|
|
||||||
const configMapToUpdate = convertToSave();
|
|
||||||
|
|
||||||
if (!configMapToUpdate) {
|
|
||||||
saving.value = false;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const { forms } = setting.value.spec;
|
||||||
await coreApiClient.configMap.updateConfigMap({
|
return forms.find((item) => item.group === group?.value)?.formSchema as (
|
||||||
name: SYSTEM_CONFIGMAP_NAME,
|
| FormKitSchemaCondition
|
||||||
configMap: configMapToUpdate,
|
| FormKitSchemaNode
|
||||||
|
)[];
|
||||||
});
|
});
|
||||||
|
|
||||||
Toast.success(t("core.common.toast.save_success"));
|
const handleSaveConfigMap = async (data: Record<string, unknown>) => {
|
||||||
|
try {
|
||||||
|
isSubmitting.value = true;
|
||||||
|
await consoleApiClient.configMap.system.updateSystemConfigByGroup({
|
||||||
|
group: group.value,
|
||||||
|
body: data,
|
||||||
|
});
|
||||||
|
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["core:system:configMap:group-data"],
|
||||||
|
});
|
||||||
|
|
||||||
queryClient.invalidateQueries({ queryKey: ["system-configMap"] });
|
|
||||||
await useGlobalInfoStore().fetchGlobalInfo();
|
await useGlobalInfoStore().fetchGlobalInfo();
|
||||||
|
|
||||||
const language = configMapFormData.value.basic.language;
|
if (group.value === "basic") {
|
||||||
locale.value = language;
|
const language = data.language;
|
||||||
|
locale.value = language as string;
|
||||||
document.cookie = `language=${language}; path=/; SameSite=Lax; Secure`;
|
document.cookie = `language=${language}; path=/; SameSite=Lax; Secure`;
|
||||||
|
}
|
||||||
|
|
||||||
saving.value = false;
|
Toast.success(t("core.common.toast.save_success"));
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
Toast.error(t("core.common.toast.save_failed_and_retry"));
|
||||||
|
} finally {
|
||||||
|
isSubmitting.value = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<div class="p-4">
|
<div class="p-4">
|
||||||
|
<VLoading v-if="isLoading" />
|
||||||
<FormKit
|
<FormKit
|
||||||
v-if="group && formSchema && configMapFormData?.[group]"
|
v-else-if="group && formSchema && configMapGroupData"
|
||||||
:id="group"
|
:id="group"
|
||||||
v-model="configMapFormData[group]"
|
:value="configMapGroupData"
|
||||||
:name="group"
|
:name="group"
|
||||||
:actions="false"
|
|
||||||
:preserve="true"
|
:preserve="true"
|
||||||
type="form"
|
type="form"
|
||||||
@submit="handleSaveConfigMap"
|
@submit="handleSaveConfigMap"
|
||||||
>
|
>
|
||||||
<FormKitSchema
|
<FormKitSchema
|
||||||
:schema="toRaw(formSchema)"
|
:schema="toRaw(formSchema)"
|
||||||
:data="configMapFormData[group]"
|
:data="toRaw(configMapGroupData)"
|
||||||
/>
|
/>
|
||||||
</FormKit>
|
</FormKit>
|
||||||
|
|
||||||
|
<HasPermission
|
||||||
|
v-if="!isLoading"
|
||||||
|
:permissions="['system:configmaps:manage']"
|
||||||
|
>
|
||||||
<StickyBlock
|
<StickyBlock
|
||||||
v-permission="['system:configmaps:manage']"
|
|
||||||
class="-mx-4 -mb-4 rounded-b-base rounded-t-lg bg-white p-4 pt-5"
|
class="-mx-4 -mb-4 rounded-b-base rounded-t-lg bg-white p-4 pt-5"
|
||||||
position="bottom"
|
position="bottom"
|
||||||
>
|
>
|
||||||
<VButton
|
<VButton
|
||||||
:loading="saving"
|
:loading="isSubmitting"
|
||||||
type="secondary"
|
type="secondary"
|
||||||
@click="$formkit.submit(group || '')"
|
@click="$formkit.submit(group || '')"
|
||||||
>
|
>
|
||||||
{{ $t("core.common.buttons.save") }}
|
{{ $t("core.common.buttons.save") }}
|
||||||
</VButton>
|
</VButton>
|
||||||
</StickyBlock>
|
</StickyBlock>
|
||||||
|
</HasPermission>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
Loading…
Reference in New Issue