diff --git a/src/modules/system/roles/RoleDetail.vue b/src/modules/system/roles/RoleDetail.vue index 8957747c..b4844d87 100644 --- a/src/modules/system/roles/RoleDetail.vue +++ b/src/modules/system/roles/RoleDetail.vue @@ -10,27 +10,92 @@ import { VTag, } from "@halo-dev/components"; import { useRoute, useRouter } from "vue-router"; -import { onMounted, ref } from "vue"; +import { computed, onMounted, ref } from "vue"; import { axiosInstance } from "@halo-dev/admin-shared"; import type { Role, User } from "@/types/extension"; +interface RoleTemplateGroup { + name: string | null | undefined; + roles: Role[]; +} + +interface FormState { + role: Role; + selectedRoleTemplates: string[]; + saving: boolean; +} + const route = useRoute(); const users = ref([]); -const role = ref(); +const roles = ref([]); const roleActiveId = ref("detail"); +const formState = ref({ + role: { + apiVersion: "v1alpha1", + kind: "Role", + metadata: { + name: "", + labels: {}, + annotations: { + "plugin.halo.run/dependencies": "", + "plugin.halo.run/display-name": "", + }, + }, + }, + selectedRoleTemplates: [], + saving: false, +}); + +const roleTemplates = computed(() => { + return roles.value.filter( + (role) => role.metadata.labels?.["plugin.halo.run/role-template"] === "true" + ); +}); + +const roleTemplateGroups = computed(() => { + const groups: RoleTemplateGroup[] = []; + roleTemplates.value.forEach((role) => { + const group = groups.find( + (group) => + group.name === role.metadata.annotations?.["plugin.halo.run/module"] + ); + if (group) { + group.roles.push(role); + } else { + groups.push({ + name: role.metadata.annotations?.["plugin.halo.run/module"], + roles: [role], + }); + } + }); + return groups; +}); const handleFetchRole = async () => { try { - const response = await axiosInstance.get( + const response = await axiosInstance.get( `/api/v1alpha1/roles/${route.params.name}` ); - role.value = response.data; + formState.value.role = response.data; + formState.value.selectedRoleTemplates = JSON.parse( + response.data.metadata.annotations?.["plugin.halo.run/dependencies"] || + "[]" + ); } catch (error) { console.error(error); } }; +const handleFetchRoles = async () => { + try { + const { data } = await axiosInstance.get("/api/v1alpha1/roles"); + roles.value = data; + } catch (e) { + console.error(e); + } +}; + const handleFetchUsers = async () => { try { const { data } = await axiosInstance.get("/api/v1alpha1/users"); @@ -40,6 +105,26 @@ const handleFetchUsers = async () => { } }; +const handleUpdateRole = async () => { + try { + formState.value.saving = true; + if (formState.value.role.metadata.annotations) { + formState.value.role.metadata.annotations[ + "plugin.halo.run/dependencies" + ] = JSON.stringify(formState.value.selectedRoleTemplates); + } + await axiosInstance.put( + `/api/v1alpha1/roles/${route.params.name}`, + formState.value.role + ); + } catch (e) { + console.error(e); + } finally { + formState.value.saving = false; + await handleFetchRole(); + } +}; + const router = useRouter(); const handleRouteToUser = (name: string) => { @@ -48,11 +133,17 @@ const handleRouteToUser = (name: string) => { onMounted(() => { handleFetchRole(); + handleFetchRoles(); handleFetchUsers(); });