ff-admin/src/views/admin/user/RoleEditPermission.vue

88 lines
2.5 KiB
Vue
Raw Normal View History

2024-09-02 15:57:03 +02:00
<template>
<MainTemplate>
<template #headerInsert>
2024-09-17 16:44:02 +02:00
<RouterLink to="@/" class="text-primary">zurück zur Liste (abbrechen)</RouterLink>
2024-09-02 15:57:03 +02:00
</template>
<template #topBar>
<div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
<h1 class="font-bold text-xl h-8">Rolle {{ role?.role }} - Berechtigungen bearbeiten</h1>
</div>
</template>
<template #main>
2024-09-15 13:52:54 +02:00
<Spinner v-if="loading == 'loading'" class="mx-auto" />
<p v-else-if="loading == 'failed'">laden fehlgeschlagen</p>
2024-09-02 15:57:03 +02:00
<Permission
2024-09-15 13:52:54 +02:00
v-else-if="role != null"
:permissions="role.permissions"
:status="status"
@savePermissions="triggerUpdate"
2024-09-02 15:57:03 +02:00
/>
</template>
</MainTemplate>
</template>
<script setup lang="ts">
import { defineComponent } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
2024-09-17 16:44:02 +02:00
import { useRoleStore } from "@/stores/admin/role";
2024-09-02 15:57:03 +02:00
import Permission from "@/components/admin/Permission.vue";
import Spinner from "@/components/Spinner.vue";
import type { PermissionObject } from "@/types/permissionTypes";
2024-09-15 13:52:54 +02:00
import type { RoleViewModel } from "@/viewmodels/admin/role.models";
2024-09-02 15:57:03 +02:00
</script>
<script lang="ts">
export default defineComponent({
props: {
id: String,
},
2024-09-15 13:52:54 +02:00
data() {
return {
loading: "loading" as "loading" | "fetched" | "failed",
status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
role: null as null | RoleViewModel,
timeout: null as any,
};
2024-09-02 15:57:03 +02:00
},
mounted() {
2024-09-15 13:52:54 +02:00
this.fetchItem();
},
beforeUnmount() {
try {
clearTimeout(this.timeout);
} catch (error) {}
2024-09-02 15:57:03 +02:00
},
methods: {
2024-09-15 13:52:54 +02:00
...mapActions(useRoleStore, ["fetchRoleById", "updateActiveRolePermissions"]),
fetchItem() {
this.fetchRoleById(parseInt(this.id ?? ""))
.then((result) => {
this.role = result.data;
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
triggerUpdate(e: PermissionObject) {
if (this.role == null) return;
this.status = "loading";
this.updateActiveRolePermissions(this.role.id, e)
.then(() => {
this.fetchItem();
this.status = { status: "success" };
})
.catch((err) => {
this.status = { status: "failed" };
})
.finally(() => {
this.timeout = setTimeout(() => {
this.status = null;
}, 2000);
});
2024-09-02 15:57:03 +02:00
},
},
});
</script>