|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <v-list-item |
| 4 | + :disabled="!hasAuthorization" |
| 5 | + data-test="invitation-edit-btn" |
| 6 | + @click="showDialog = true" |
| 7 | + > |
| 8 | + <div class="d-flex align-center ga-2"> |
| 9 | + <v-icon icon="mdi-pencil" /> |
| 10 | + <v-list-item-title data-test="invitation-edit-title">Edit Role</v-list-item-title> |
| 11 | + </div> |
| 12 | + </v-list-item> |
| 13 | + |
| 14 | + <FormDialog |
| 15 | + v-model="showDialog" |
| 16 | + title="Update invitation role" |
| 17 | + icon="mdi-account-edit" |
| 18 | + confirm-text="Update" |
| 19 | + :confirm-loading="isLoading" |
| 20 | + cancel-text="Cancel" |
| 21 | + confirm-data-test="update-btn" |
| 22 | + cancel-data-test="cancel-btn" |
| 23 | + data-test="invitation-edit-dialog" |
| 24 | + @close="close" |
| 25 | + @confirm="editInvitation" |
| 26 | + @cancel="close" |
| 27 | + > |
| 28 | + <v-card-text class="pa-6"> |
| 29 | + <RoleSelect v-model="newRole" /> |
| 30 | + </v-card-text> |
| 31 | + </FormDialog> |
| 32 | + </div> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script setup lang="ts"> |
| 36 | +import { ref } from "vue"; |
| 37 | +import axios from "axios"; |
| 38 | +import FormDialog from "@/components/Dialogs/FormDialog.vue"; |
| 39 | +import { IInvitation } from "@/interfaces/IInvitation"; |
| 40 | +import handleError from "@/utils/handleError"; |
| 41 | +import useSnackbar from "@/helpers/snackbar"; |
| 42 | +import RoleSelect from "@/components/Team/RoleSelect.vue"; |
| 43 | +import useInvitationsStore from "@/store/modules/invitations"; |
| 44 | +
|
| 45 | +const { invitation, hasAuthorization } = defineProps<{ |
| 46 | + invitation: IInvitation; |
| 47 | + hasAuthorization: boolean; |
| 48 | +}>(); |
| 49 | +
|
| 50 | +const emit = defineEmits(["update"]); |
| 51 | +const invitationsStore = useInvitationsStore(); |
| 52 | +const snackbar = useSnackbar(); |
| 53 | +const showDialog = ref(false); |
| 54 | +const isLoading = ref(false); |
| 55 | +const newRole = ref(invitation.role); |
| 56 | +
|
| 57 | +const close = () => { |
| 58 | + showDialog.value = false; |
| 59 | + newRole.value = invitation.role; |
| 60 | +}; |
| 61 | +
|
| 62 | +const update = () => { |
| 63 | + emit("update"); |
| 64 | + close(); |
| 65 | +}; |
| 66 | +
|
| 67 | +const handleEditInvitationError = (error: unknown) => { |
| 68 | + if (axios.isAxiosError(error)) { |
| 69 | + const status = error.response?.status; |
| 70 | + switch (status) { |
| 71 | + case 400: |
| 72 | + snackbar.showError("Invalid invitation or role."); |
| 73 | + break; |
| 74 | + case 403: |
| 75 | + snackbar.showError("You don't have permission to edit invitations."); |
| 76 | + break; |
| 77 | + case 404: |
| 78 | + snackbar.showError("Invitation not found."); |
| 79 | + break; |
| 80 | + default: |
| 81 | + snackbar.showError("Failed to update invitation role."); |
| 82 | + } |
| 83 | + } else { |
| 84 | + snackbar.showError("Failed to update invitation role."); |
| 85 | + } |
| 86 | +
|
| 87 | + handleError(error); |
| 88 | +}; |
| 89 | +
|
| 90 | +const editInvitation = async () => { |
| 91 | + isLoading.value = true; |
| 92 | + try { |
| 93 | + await invitationsStore.editInvitation({ |
| 94 | + tenant: invitation.namespace.tenant_id, |
| 95 | + user_id: invitation.user.id, |
| 96 | + role: newRole.value, |
| 97 | + }); |
| 98 | +
|
| 99 | + snackbar.showSuccess("Successfully updated invitation role."); |
| 100 | + update(); |
| 101 | + } catch (error: unknown) { |
| 102 | + handleEditInvitationError(error); |
| 103 | + } finally { |
| 104 | + isLoading.value = false; |
| 105 | + } |
| 106 | +}; |
| 107 | +</script> |
0 commit comments