|
| 1 | +<script setup lang="ts"> |
| 2 | +import "vue-multiselect/dist/vue-multiselect.min.css"; |
| 3 | +
|
| 4 | +import { faSave } from "@fortawesome/free-solid-svg-icons"; |
| 5 | +import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; |
| 6 | +import { BAlert, BButton, BFormCheckbox } from "bootstrap-vue"; |
| 7 | +import { ref } from "vue"; |
| 8 | +import Multiselect from "vue-multiselect"; |
| 9 | +import { useRouter } from "vue-router/composables"; |
| 10 | +
|
| 11 | +import { GalaxyApi } from "@/api"; |
| 12 | +import { errorMessageAsString } from "@/utils/simple-error"; |
| 13 | +
|
| 14 | +import FormInput from "@/components/Form/Elements/FormInput.vue"; |
| 15 | +import FormCard from "@/components/Form/FormCard.vue"; |
| 16 | +import FormElementLabel from "@/components/Form/FormElementLabel.vue"; |
| 17 | +import LoadingSpan from "@/components/LoadingSpan.vue"; |
| 18 | +
|
| 19 | +interface UserOption { |
| 20 | + id: string; |
| 21 | + email: string; |
| 22 | +} |
| 23 | +
|
| 24 | +interface RoleOption { |
| 25 | + id: string; |
| 26 | + name: string; |
| 27 | +} |
| 28 | +
|
| 29 | +const props = defineProps<{ |
| 30 | + groupId?: string; |
| 31 | +}>(); |
| 32 | +
|
| 33 | +const isEditMode = !!props.groupId; |
| 34 | +
|
| 35 | +const errorMessage = ref(""); |
| 36 | +const loading = ref(false); |
| 37 | +const groupName = ref(""); |
| 38 | +const selectedUsers = ref<UserOption[]>([]); |
| 39 | +const selectedRoles = ref<RoleOption[]>([]); |
| 40 | +const userOptions = ref<UserOption[]>([]); |
| 41 | +const roleOptions = ref<RoleOption[]>([]); |
| 42 | +const userSearch = ref(""); |
| 43 | +const roleSearch = ref(""); |
| 44 | +const autoCreateRole = ref(false); |
| 45 | +
|
| 46 | +const router = useRouter(); |
| 47 | +
|
| 48 | +async function onUserSearch(searchValue: string) { |
| 49 | + userSearch.value = searchValue; |
| 50 | + if (searchValue.length < 3) { |
| 51 | + userOptions.value = [...selectedUsers.value]; |
| 52 | + return; |
| 53 | + } |
| 54 | + const { data, error } = await GalaxyApi().GET("/api/users", { |
| 55 | + params: { query: { f_email: searchValue, limit: 50 } }, |
| 56 | + }); |
| 57 | + if (error) { |
| 58 | + errorMessage.value = errorMessageAsString(error); |
| 59 | + return; |
| 60 | + } |
| 61 | + const selectedIds = new Set(selectedUsers.value.map((u) => u.id)); |
| 62 | + const filtered = data.filter((u) => u.email && !selectedIds.has(u.id)).map((u) => ({ id: u.id, email: u.email! })); |
| 63 | + userOptions.value = [...selectedUsers.value, ...filtered]; |
| 64 | +} |
| 65 | +
|
| 66 | +async function onRoleSearch(searchValue: string) { |
| 67 | + roleSearch.value = searchValue; |
| 68 | + if (searchValue.length < 3) { |
| 69 | + roleOptions.value = [...selectedRoles.value]; |
| 70 | + return; |
| 71 | + } |
| 72 | + const { data, error } = await GalaxyApi().GET("/api/roles", { |
| 73 | + params: { query: { search: searchValue, limit: 50 } }, |
| 74 | + }); |
| 75 | + if (error) { |
| 76 | + errorMessage.value = errorMessageAsString(error); |
| 77 | + return; |
| 78 | + } |
| 79 | + const selectedIds = new Set(selectedRoles.value.map((r) => r.id)); |
| 80 | + const filtered = data.filter((r) => !selectedIds.has(r.id)).map((r) => ({ id: r.id, name: r.name })); |
| 81 | + roleOptions.value = [...selectedRoles.value, ...filtered]; |
| 82 | +} |
| 83 | +
|
| 84 | +async function loadGroupData() { |
| 85 | + if (!props.groupId) { |
| 86 | + return; |
| 87 | + } |
| 88 | + loading.value = true; |
| 89 | + try { |
| 90 | + const { data: group, error: groupError } = await GalaxyApi().GET("/api/groups/{group_id}", { |
| 91 | + params: { path: { group_id: props.groupId } }, |
| 92 | + }); |
| 93 | + if (groupError) { |
| 94 | + errorMessage.value = errorMessageAsString(groupError); |
| 95 | + loading.value = false; |
| 96 | + return; |
| 97 | + } |
| 98 | + groupName.value = group.name; |
| 99 | +
|
| 100 | + const { data: users, error: usersError } = await GalaxyApi().GET("/api/groups/{group_id}/users", { |
| 101 | + params: { path: { group_id: props.groupId } }, |
| 102 | + }); |
| 103 | + if (usersError) { |
| 104 | + errorMessage.value = errorMessageAsString(usersError); |
| 105 | + loading.value = false; |
| 106 | + return; |
| 107 | + } |
| 108 | + selectedUsers.value = users.map((u) => ({ |
| 109 | + id: u.id, |
| 110 | + email: u.email, |
| 111 | + })); |
| 112 | + userOptions.value = [...selectedUsers.value]; |
| 113 | +
|
| 114 | + const { data: roles, error: rolesError } = await GalaxyApi().GET("/api/groups/{group_id}/roles", { |
| 115 | + params: { path: { group_id: props.groupId } }, |
| 116 | + }); |
| 117 | + if (rolesError) { |
| 118 | + errorMessage.value = errorMessageAsString(rolesError); |
| 119 | + loading.value = false; |
| 120 | + return; |
| 121 | + } |
| 122 | + selectedRoles.value = roles.map((r) => ({ |
| 123 | + id: r.id, |
| 124 | + name: r.name, |
| 125 | + })); |
| 126 | + roleOptions.value = [...selectedRoles.value]; |
| 127 | + } catch (e) { |
| 128 | + errorMessage.value = errorMessageAsString(e); |
| 129 | + } |
| 130 | + loading.value = false; |
| 131 | +} |
| 132 | +
|
| 133 | +async function onSubmit() { |
| 134 | + const userIds = selectedUsers.value.map((u) => u.id); |
| 135 | + const roleIds = selectedRoles.value.map((r) => r.id); |
| 136 | +
|
| 137 | + if (isEditMode) { |
| 138 | + const { error } = await GalaxyApi().PUT("/api/groups/{group_id}", { |
| 139 | + params: { path: { group_id: props.groupId! } }, |
| 140 | + body: { |
| 141 | + user_ids: userIds, |
| 142 | + role_ids: roleIds, |
| 143 | + }, |
| 144 | + }); |
| 145 | + if (error) { |
| 146 | + errorMessage.value = errorMessageAsString(error); |
| 147 | + return; |
| 148 | + } |
| 149 | + } else { |
| 150 | + if (!groupName.value) { |
| 151 | + errorMessage.value = "Please enter a group name."; |
| 152 | + return; |
| 153 | + } |
| 154 | + const { error } = await GalaxyApi().POST("/api/groups", { |
| 155 | + body: { |
| 156 | + name: groupName.value, |
| 157 | + user_ids: userIds, |
| 158 | + role_ids: roleIds, |
| 159 | + auto_create_role: autoCreateRole.value, |
| 160 | + }, |
| 161 | + }); |
| 162 | + if (error) { |
| 163 | + errorMessage.value = errorMessageAsString(error); |
| 164 | + return; |
| 165 | + } |
| 166 | + } |
| 167 | + router.push("/admin/groups"); |
| 168 | +} |
| 169 | +
|
| 170 | +loadGroupData(); |
| 171 | +</script> |
| 172 | + |
| 173 | +<template> |
| 174 | + <div> |
| 175 | + <LoadingSpan v-if="loading" /> |
| 176 | + <div v-else> |
| 177 | + <BAlert v-if="errorMessage" variant="danger" show>{{ errorMessage }}</BAlert> |
| 178 | + <FormCard :title="isEditMode ? `Group '${groupName}'` : 'Create a new Group'" icon="fa-users"> |
| 179 | + <template v-slot:body> |
| 180 | + <FormElementLabel title="Name" :required="!isEditMode" :condition="!!groupName"> |
| 181 | + <FormInput v-if="!isEditMode" id="admin-group-name-input" v-model="groupName" /> |
| 182 | + <span v-else>{{ groupName }}</span> |
| 183 | + </FormElementLabel> |
| 184 | + |
| 185 | + <FormElementLabel title="Users"> |
| 186 | + <Multiselect |
| 187 | + id="admin-group-users-select" |
| 188 | + v-model="selectedUsers" |
| 189 | + :options="userOptions" |
| 190 | + :clear-on-select="true" |
| 191 | + :multiple="true" |
| 192 | + :internal-search="false" |
| 193 | + :max-height="300" |
| 194 | + label="email" |
| 195 | + track-by="id" |
| 196 | + placeholder="Search users by email..." |
| 197 | + @search-change="onUserSearch"> |
| 198 | + <template slot="noResult"> |
| 199 | + <div v-if="userSearch.length < 3">Enter at least 3 characters to search</div> |
| 200 | + <div v-else>No users found</div> |
| 201 | + </template> |
| 202 | + <template slot="noOptions"> |
| 203 | + <div>Enter at least 3 characters to search</div> |
| 204 | + </template> |
| 205 | + </Multiselect> |
| 206 | + </FormElementLabel> |
| 207 | + |
| 208 | + <FormElementLabel title="Roles"> |
| 209 | + <Multiselect |
| 210 | + id="admin-group-roles-select" |
| 211 | + v-model="selectedRoles" |
| 212 | + :options="roleOptions" |
| 213 | + :clear-on-select="true" |
| 214 | + :multiple="true" |
| 215 | + :internal-search="false" |
| 216 | + :max-height="300" |
| 217 | + label="name" |
| 218 | + track-by="id" |
| 219 | + placeholder="Search roles by name..." |
| 220 | + @search-change="onRoleSearch"> |
| 221 | + <template slot="noResult"> |
| 222 | + <div v-if="roleSearch.length < 3">Enter at least 3 characters to search</div> |
| 223 | + <div v-else>No roles found</div> |
| 224 | + </template> |
| 225 | + <template slot="noOptions"> |
| 226 | + <div>Enter at least 3 characters to search</div> |
| 227 | + </template> |
| 228 | + </Multiselect> |
| 229 | + </FormElementLabel> |
| 230 | + |
| 231 | + <FormElementLabel v-if="!isEditMode" title="Auto-create role"> |
| 232 | + <BFormCheckbox v-model="autoCreateRole"> |
| 233 | + Create a new role with the same name as this group |
| 234 | + </BFormCheckbox> |
| 235 | + </FormElementLabel> |
| 236 | + </template> |
| 237 | + </FormCard> |
| 238 | + <BButton id="admin-group-submit" class="my-2" variant="primary" @click="onSubmit"> |
| 239 | + <FontAwesomeIcon :icon="faSave" class="mr-1" /> |
| 240 | + <span v-localize>{{ isEditMode ? "Save" : "Create" }}</span> |
| 241 | + </BButton> |
| 242 | + </div> |
| 243 | + </div> |
| 244 | +</template> |
0 commit comments