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

127 lines
4.4 KiB
Vue
Raw Normal View History

2024-09-02 15:57:03 +02:00
<template>
<MainTemplate>
<template #headerInsert>
<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">Nutzer {{ user?.username }} - Rollen bearbeiten</h1>
</div>
</template>
<template #main>
<Spinner v-if="loadingSingle == 'loading'" class="mx-auto" />
<p v-else-if="loadingSingle == 'failed'">laden fehlgeschlagen</p>
<div v-else class="flex flex-col grow gap-4">
<div class="flex flex-col gap-2 grow">
<p class="text-xl font-semibold">zugewiesene Rollen</p>
<div class="flex flex-row flex-wrap gap-2 h-1/2 overflow-y-auto">
<div
v-for="role in assignedRoles"
:key="role.id"
class="flex flex-row gap-2 items-center px-2 p-1 border border-gray-300 rounded-md h-fit cursor-pointer"
@click="removeAssigned(role.id)"
>
<p>{{ role.role }}</p>
<XMarkIcon class="w-5 h-5" />
</div>
</div>
<p class="text-xl font-semibold">verfügbare Rollen</p>
<div class="flex flex-row flex-wrap gap-2 h-1/2 overflow-y-auto">
<div
v-for="role in availableRoles"
:key="role.id"
class="flex flex-row gap-2 items-center px-2 p-1 border border-gray-300 rounded-md h-fit cursor-pointer"
@click="addAvailable(role.id)"
>
<p>{{ role.role }}</p>
<PlusIcon class="w-5 h-5" />
</div>
</div>
</div>
<div class="flex flex-row justify-end gap-2">
<button primary-outline class="!w-fit" :disabled="!change_detect" @click="reset">verwerfen</button>
2024-09-02 15:57:03 +02:00
<button primary class="!w-fit" :disabled="updateStatus == 'loading'" @click="triggerRolesUpdate">
speichern
</button>
<Spinner v-if="updateStatus == 'loading'" class="my-auto" />
<SuccessCheckmark v-else-if="updateStatus?.status == 'success'" />
<FailureXMark v-else-if="updateStatus?.status == 'failed'" />
</div>
</div>
</template>
</MainTemplate>
</template>
<script setup lang="ts">
import { defineComponent } from "vue";
import { RouterLink } from "vue-router";
import { mapState, mapActions } from "pinia";
import { useUserStore } from "@/stores/admin/user";
import { useRoleStore } from "@/stores/admin/role";
import type { PermissionObject } from "@/types/permissionTypes";
import MainTemplate from "@/templates/Main.vue";
import Permission from "@/components/admin/Permission.vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { XMarkIcon, PlusIcon } from "@heroicons/vue/24/outline";
</script>
<script lang="ts">
export default defineComponent({
props: {
id: String,
},
watch: {
user() {
this.assigned = this.user?.roles.map((r) => r.id) ?? [];
},
},
data() {
return {
change_detect: false as boolean,
2024-09-02 15:57:03 +02:00
assigned: [] as Array<number>,
};
},
computed: {
...mapState(useUserStore, ["user", "loadingSingle", "updateStatus"]),
...mapState(useRoleStore, ["roles"]),
assignedRoles() {
return this.roles.filter((r) => this.assigned.includes(r.id));
},
availableRoles() {
return this.roles.filter((r) => !this.assigned.includes(r.id));
},
},
mounted() {
this.resetStatus();
this.fetchUserById(parseInt(this.id ?? ""));
this.fetchRoles();
},
methods: {
...mapActions(useUserStore, ["fetchUserById", "updateActiveUserRoles", "resetStatus"]),
...mapActions(useRoleStore, ["fetchRoles"]),
detectChange() {
this.resetStatus();
this.change_detect = true;
},
reset() {
this.change_detect = false;
this.assigned = this.user?.roles.map((r) => r.id) ?? [];
},
2024-09-02 15:57:03 +02:00
addAvailable(id: number) {
this.detectChange();
2024-09-02 15:57:03 +02:00
this.assigned.push(id);
},
removeAssigned(id: number) {
this.detectChange();
2024-09-02 15:57:03 +02:00
this.assigned = this.assigned.filter((roleId) => roleId !== id);
},
triggerRolesUpdate() {
this.change_detect = false;
2024-09-02 15:57:03 +02:00
this.updateActiveUserRoles(this.assigned);
},
},
});
</script>