| 1 | <template> |
| 2 | <n-button quaternary class="w-full! justify-start!" @click="showModal = true"> |
| 3 | <template #icon> |
| 4 | <Icon :name="RoleIcon" :size="14" /> |
| 5 | </template> |
| 6 | Assign Role |
| 7 | </n-button> |
| 8 | |
| 9 | <n-modal |
| 10 | v-model:show="showModal" |
| 11 | display-directive="show" |
| 12 | preset="card" |
| 13 | :style="{ maxWidth: 'min(500px, 90vw)', minHeight: 'min(200px, 50vh)' }" |
| 14 | title="Assign Role" |
| 15 | :bordered="false" |
| 16 | content-class="flex flex-col" |
| 17 | segmented |
| 18 | > |
| 19 | <div class="flex flex-col gap-4"> |
| 20 | <div> |
| 21 | <strong>User:</strong> |
| 22 | {{ user?.username }} |
| 23 | </div> |
| 24 | |
| 25 | <n-form ref="formRef" :model="formModel" :rules> |
| 26 | <n-form-item path="role" label="Select Role"> |
| 27 | <n-select |
| 28 | v-model:value="formModel.role" |
| 29 | :options="roleOptions" |
| 30 | placeholder="Choose a role" |
| 31 | :loading |
| 32 | /> |
| 33 | </n-form-item> |
| 34 | </n-form> |
| 35 | |
| 36 | <div class="flex justify-end gap-3"> |
| 37 | <n-button @click="showModal = false">Cancel</n-button> |
| 38 | <n-button type="primary" :loading :disabled="!formModel.role" @click="handleAssignRole"> |
| 39 | Assign Role |
| 40 | </n-button> |
| 41 | </div> |
| 42 | </div> |
| 43 | </n-modal> |
| 44 | </template> |
| 45 | |
| 46 | <script setup lang="ts"> |
| 47 | import type { FormInst } from "naive-ui" |
| 48 | import type { User } from "@/types/user.d" |
| 49 | import { NButton, NForm, NFormItem, NModal, NSelect, useMessage } from "naive-ui" |
| 50 | import { ref } from "vue" |
| 51 | import Api from "@/api" |
| 52 | import Icon from "@/components/common/Icon.vue" |
| 53 | |
| 54 | const props = defineProps<{ |
| 55 | user?: User |
| 56 | }>() |
| 57 | |
| 58 | const emit = defineEmits<{ |
| 59 | success: [] |
| 60 | }>() |
| 61 | |
| 62 | const RoleIcon = "carbon:user-role" |
| 63 | const message = useMessage() |
| 64 | const showModal = ref(false) |
| 65 | const loading = ref(false) |
| 66 | const formRef = ref<FormInst>() |
| 67 | |
| 68 | const formModel = ref({ |
| 69 | role: null as string | null |
| 70 | }) |
| 71 | |
| 72 | const roleOptions = [ |
| 73 | { label: "Admin", value: "admin" }, |
| 74 | { label: "Analyst", value: "analyst" }, |
| 75 | { label: "Scheduler", value: "scheduler" }, |
| 76 | { label: "Customer User", value: "customer_user" } |
| 77 | ] |
| 78 | |
| 79 | const rules = { |
| 80 | role: { |
| 81 | required: true, |
| 82 | message: "Please select a role", |
| 83 | trigger: ["blur", "change"] |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function handleAssignRole() { |
| 88 | if (!props.user || !formModel.value.role) return |
| 89 | |
| 90 | formRef.value?.validate(async errors => { |
| 91 | if (!errors && props.user?.id && formModel.value.role) { |
| 92 | loading.value = true |
| 93 | |
| 94 | try { |
| 95 | const res = await Api.auth.assignRole(props.user.id, formModel.value.role) |
| 96 | if (res.data.success) { |
| 97 | message.success(res.data.message || "Role assigned successfully") |
| 98 | showModal.value = false |
| 99 | formModel.value.role = null |
| 100 | emit("success") |
| 101 | } else { |
| 102 | message.error(res.data.message || "Failed to assign role") |
| 103 | } |
| 104 | } catch (err: any) { |
| 105 | message.error(err.response?.data?.message || "Failed to assign role") |
| 106 | } finally { |
| 107 | loading.value = false |
| 108 | } |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | </script> |