fix: failed to change plugin status (#4506)

#### What type of PR is this?

/area console
/kind bug
/milestone 2.9.x

#### What this PR does / why we need it:

修复可能因为 version 锁的原因导致无法正常切换插件状态,现在改为在调用切换状态接口前,获取一次最新的插件信息,并提供了 retry 的配置,即失败之后重试 3 次。

#### Special notes for your reviewer:

测试能否正常切换插件状态即可。

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/4518/head^2
Ryan Wang 2023-08-29 23:56:10 -05:00 committed by GitHub
parent a819296945
commit 22571367fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 13 deletions

View File

@ -1,10 +1,11 @@
import type { ComputedRef, Ref } from "vue";
import { computed, ref } from "vue";
import { computed } from "vue";
import type { Plugin } from "@halo-dev/api-client";
import cloneDeep from "lodash.clonedeep";
import { apiClient } from "@/utils/api-client";
import { Dialog, Toast } from "@halo-dev/components";
import { useI18n } from "vue-i18n";
import { useMutation } from "@tanstack/vue-query";
interface usePluginLifeCycleReturn {
isStarted: ComputedRef<boolean | undefined>;
@ -37,26 +38,28 @@ export function usePluginLifeCycle(
}
};
const changingStatus = ref(false);
const { isLoading: changingStatus, mutate: changeStatus } = useMutation({
mutationKey: ["change-plugin-status"],
mutationFn: async () => {
if (!plugin?.value) return;
const changeStatus = async () => {
if (!plugin?.value) return;
const { data: pluginToUpdate } =
await apiClient.extension.plugin.getpluginHaloRunV1alpha1Plugin({
name: plugin.value.metadata.name,
});
try {
changingStatus.value = true;
const pluginToUpdate = cloneDeep(plugin.value);
pluginToUpdate.spec.enabled = !pluginToUpdate.spec.enabled;
await apiClient.extension.plugin.updatepluginHaloRunV1alpha1Plugin({
name: pluginToUpdate.metadata.name,
plugin: pluginToUpdate,
});
},
retry: 3,
retryDelay: 1000,
onSuccess() {
window.location.reload();
} catch (e) {
console.error(e);
changingStatus.value = false;
}
};
},
});
const uninstall = (deleteExtensions?: boolean) => {
if (!plugin?.value) return;