Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
refactor: use tanstack query to refactor plugins fetching (#876)
Browse files Browse the repository at this point in the history
#### 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 halo-dev/halo#3360

#### Special notes for your reviewer:

测试方式:

1. 需要 `pnpm install`
2. 插件管理页面,安装若干插件。
3. 测试分页、条件筛选等逻辑是否正常。

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

```release-note
None
```
  • Loading branch information
ruibaby authored Feb 22, 2023
1 parent c03ea64 commit 816feb9
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 120 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@halo-dev/components": "workspace:*",
"@halo-dev/console-shared": "workspace:*",
"@halo-dev/richtext-editor": "0.0.0-alpha.19",
"@tanstack/vue-query": "^4.24.10",
"@tiptap/extension-character-count": "^2.0.0-beta.202",
"@uppy/core": "^3.0.4",
"@uppy/dashboard": "^3.2.0",
Expand Down
49 changes: 47 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import { useSystemStatesStore } from "./stores/system-states";
import { useUserStore } from "./stores/user";
import { useSystemConfigMapStore } from "./stores/system-configmap";
import i18n from "./locales";
import { VueQueryPlugin } from "@tanstack/vue-query";

const app = createApp(App);

setupComponents(app);

app.use(createPinia());
app.use(i18n);
app.use(VueQueryPlugin);

function registerModule(pluginModule: PluginModule, core: boolean) {
if (pluginModule.components) {
Expand Down
27 changes: 10 additions & 17 deletions src/modules/system/plugins/PluginDetail.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script lang="ts" setup>
import { VSwitch, VTag } from "@halo-dev/components";
import type { Ref } from "vue";
import { computed, inject, ref, watchEffect } from "vue";
import { computed, inject } from "vue";
import { apiClient } from "@/utils/api-client";
import type { Plugin, Role } from "@halo-dev/api-client";
import { pluginLabels } from "@/constants/labels";
import { rbacAnnotations } from "@/constants/annotations";
import { usePluginLifeCycle } from "./composables/use-plugin";
import { formatDatetime } from "@/utils/date";
import { useQuery } from "@tanstack/vue-query";
const plugin = inject<Ref<Plugin | undefined>>("plugin");
const { changeStatus, isStarted } = usePluginLifeCycle(plugin);
Expand All @@ -17,24 +18,22 @@ interface RoleTemplateGroup {
roles: Role[];
}
const pluginRoleTemplates = ref<Role[]>([]);
const handleFetchRoles = async () => {
try {
const { data: pluginRoleTemplates } = useQuery({
queryKey: ["plugin-roles", plugin?.value?.metadata.name],
queryFn: async () => {
const { data } = await apiClient.extension.role.listv1alpha1Role({
page: 0,
size: 0,
labelSelector: [`${pluginLabels.NAME}=${plugin?.value?.metadata.name}`],
});
pluginRoleTemplates.value = data.items;
} catch (e) {
console.error(e);
}
};
return data.items;
},
});
const pluginRoleTemplateGroups = computed<RoleTemplateGroup[]>(() => {
const groups: RoleTemplateGroup[] = [];
pluginRoleTemplates.value.forEach((role) => {
pluginRoleTemplates.value?.forEach((role) => {
const group = groups.find(
(group) =>
group.module === role.metadata.annotations?.[rbacAnnotations.MODULE]
Expand All @@ -50,12 +49,6 @@ const pluginRoleTemplateGroups = computed<RoleTemplateGroup[]>(() => {
});
return groups;
});
watchEffect(() => {
if (plugin?.value) {
handleFetchRoles();
}
});
</script>

<template>
Expand Down
Loading

1 comment on commit 816feb9

@vercel
Copy link

@vercel vercel bot commented on 816feb9 Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

halo-admin-ui.vercel.app
ui-git-main-halo-dev.vercel.app
ui-halo-dev.vercel.app

Please sign in to comment.