All files / src/components MemberFormSidebar.vue

54.16% Statements 13/24
50% Branches 12/24
50% Functions 4/8
56.52% Lines 13/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106  73x                                   1x 1x 1x 2x                                   2x                   65x 65x   65x                         65x                                                   65x 4x 67x                      
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :dirty="dirty"
    storage-key="member-form-sidebar"
    :title="title || t('member.editRoles')"
    panel-width="600px"
    :loading="loading"
    :submit-label="submitLabel || 'Save'"
    :submit-icon="submitIcon || 'fa-solid fa-save'"
    submit-button-class="btn-success"
    @close="handleClose"
    @submit="handleSubmit"
  >
    <form @submit.prevent="handleSubmit">
      <p v-if="member" class="mb-3">
        <strong>{{ member.name || member.userId }}</strong>
      </p>
 
      <div class="mb-3">
        <label class="form-label fw-bold">{{ t('member.roles') }}</label>
        <div class="roles-checkboxes">
          <div v-for="role in availableRoles" :key="role.roleId" class="form-check">
            <input
              :id="`member-role-${role.roleId}`"
              type="checkbox"
              class="form-check-input"
              :checked="selectedRoles.includes(role.roleId)"
              @change="toggleRole(role.roleId, ($event.target as HTMLInputElement).checked)"
            />
            <label class="form-check-label" :for="`member-role-${role.roleId}`">
              {{ getTranslation(role.name) }}
            </label>
          </div>
        </div>
      </div>
 
      <div v-if="error" class="alert alert-danger">
        {{ error }}
      </div>
    </form>
  </BaseFormSidebar>
</template>
 
<script setup lang="ts">
import { useUnsavedChanges } from '@/composables/useUnsavedChanges'
import BaseFormSidebar from '@/components/BaseFormSidebar.vue'
import { useLanguage } from '@/composables/useLanguage'
import { useUILanguage } from '@/composables/useUILanguage'
 
const { getTranslation } = useLanguage()
const { t } = useUILanguage()
 
const props = defineProps<{
  isOpen: boolean
  member: any
  availableRoles: any[]
  selectedRoles: string[]
  loading: boolean
  error: string
  /** Reused to approve a membership request (MembershipRequestsPanel) */
  title?: string
  submitLabel?: string
  submitIcon?: string
}>()
 
const emit = defineEmits<{
  'update:selectedRoles': [value: string[]]
  close: []
  submit: []
}>()
 
function toggleRole(roleId: string, checked: boolean) {
  const roles = [...props.selectedRoles]
  if (checked && !roles.includes(roleId)) {
    roles.push(roleId)
  } else if (!checked) {
    const index = roles.indexOf(roleId)
    if (index > -1) roles.splice(index, 1)
  }
  emit('update:selectedRoles', roles)
}
 
function handleClose() {
  emit('close')
}
 
function handleSubmit() {
  emit('submit')
}
 
// Closing, or leaving the page, asks before dropping the changes (BaseFormSidebar)
const { dirty } = useUnsavedChanges(
  () => props.selectedRoles,
  () => (props.isOpen ? props.member?.userId || 'member' : null)
)
</script>
 
<style scoped>
.roles-checkboxes {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
</style>