All files / src/views CoursesView.vue

97.54% Statements 199/204
92.1% Branches 35/38
72.72% Functions 8/11
97.54% Lines 199/204

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250  1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x         1x 1x 1x   1x 1x   1x 1x 1x 1x   1x 7x 7x 4x 4x 3x 4x 1x 1x 7x 7x 7x 7x   1x 10x 10x 10x 7x 7x 10x 1x 1x 1x   1x 1x 1x 1x   1x 2x 2x 1x 1x     2x 1x 1x 2x 1x 1x 2x   1x 1x 1x 1x   1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 2x 2x       1x 1x 1x 1x 1x 1x 1x   1x 21x 1x   1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x  
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { GraduationCap, Trash2, Plus, Users } from 'lucide-vue-next'
import { useCourseStore } from '@/stores/course.store'
import { useToast } from '@/composables/useToast'
import { useRole } from '@/composables/useRole'
import { courseApi } from '@/api/course.api'
import { useI18n } from 'vue-i18n' // <-- i18n importiert
import Card from '@/components/ui/Card.vue'
import BaseButton from '@/components/ui/BaseButton.vue'
import BaseInput from '@/components/ui/BaseInput.vue'
import Modal from '@/components/ui/Modal.vue'
import PageHeader from '@/components/ui/PageHeader.vue'
import EntityListState from '@/components/ui/EntityListState.vue'
 
const courseStore = useCourseStore()
const toast = useToast()
// Permission mirror: course CRUD is staff-only in the UI (the backend still
// enforces course-teacher membership on edit/delete). ``isStaff`` covers
// teacher + admin which matches the previous ``can.editCourse`` /
// ``can.createCourse`` / ``can.deleteCourse`` semantics.
const { isStaff } = useRole()
const router = useRouter()
const { t } = useI18n() // <-- i18n initialisiert
 
const showModal = ref(false)
const formData = ref({ name: '' })
 
const showDeleteModal = ref(false)
const courseToDelete = ref<{ courseId: string; name: string } | null>(null)
const isDeleting = ref(false)
const memberCounts = ref<Record<string, number>>({})
 
const fetchMemberCounts = async () => {
  const entries = await Promise.all(
      courseStore.courses.map(async (c) => {
        try {
          const { data } = await courseApi.listMembers(c.courseId)
          return [c.courseId, data.length] as const
        } catch {
          return [c.courseId, 0] as const
        }
      })
  )
  memberCounts.value = Object.fromEntries(entries)
}
 
onMounted(async () => {
  try {
    await courseStore.fetchCourses()
    if (isStaff.value) {
      await fetchMemberCounts()
    }
  } catch (error) {
    toast.error(t('CoursesView.toasts.loadError'))
  }
})
 
const openCreateModal = () => {
  formData.value = { name: '' }
  showModal.value = true
}
 
const saveCourse = async () => {
  try {
    const created = await courseStore.createCourse({ name: formData.value.name })
    toast.success(t('CoursesView.toasts.createSuccess'))
    showModal.value = false
 
    // Nach dem Erstellen direkt zur Detailseite navigieren
    if (created?.courseId) {
      router.push(`/courses/${created.courseId}`)
    }
  } catch (error: any) {
    toast.error(error.response?.data?.detail || t('CoursesView.toasts.createError'))
  }
}
 
const requestDelete = (course: { courseId: string; name: string }) => {
  courseToDelete.value = course
  showDeleteModal.value = true
}
 
const closeDeleteModal = () => {
  if (isDeleting.value) return
  showDeleteModal.value = false
  courseToDelete.value = null
}
 
const confirmDelete = async () => {
  if (!courseToDelete.value) return
  const { courseId } = courseToDelete.value
  isDeleting.value = true
  try {
    await courseStore.deleteCourse(courseId)
    delete memberCounts.value[courseId]
    toast.success(t('CoursesView.toasts.deleteSuccess'))
    showDeleteModal.value = false
    courseToDelete.value = null
  } catch (error: any) {
    toast.error(error.response?.data?.detail || t('CoursesView.toasts.deleteError'))
  } finally {
    isDeleting.value = false
  }
}
 
const goToDetail = (courseId: string) => {
  router.push({ path: `/courses/${courseId}` })
}
</script>
 
<template>
  <div class="p-6">
    <PageHeader :title="$t('CoursesView.title')" :subtitle="$t('CoursesView.subtitle')">
      <template #actions>
        <BaseButton
            v-if="isStaff"
            @click="openCreateModal"
            class="flex items-center gap-2"
        >
          <Plus :size="16" />
          {{ $t('CoursesView.newCourse') }}
        </BaseButton>
      </template>
    </PageHeader>
 
    <EntityListState
      :is-loading="courseStore.isLoading && courseStore.courses.length === 0"
      :is-empty="!courseStore.isLoading && courseStore.courses.length === 0"
      :icon="GraduationCap"
      :empty-message="$t('CoursesView.noCourses')"
      :loading-message="$t('CoursesView.loading')"
    >
      <template #empty-action>
        <BaseButton v-if="isStaff" @click="openCreateModal">
          {{ $t('CoursesView.createFirst') }}
        </BaseButton>
      </template>
 
      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <Card
            v-for="course in courseStore.courses"
            :key="course.courseId"
            class="flex flex-col group h-full relative cursor-pointer hover:border-emerald-200"
            @click="goToDetail(course.courseId)"
        >
          <!-- Delete action (top-right) -->
          <button
              v-if="isStaff"
              @click.stop="requestDelete(course)"
              class="absolute top-3 right-3 p-2 hover:bg-red-50 rounded-lg transition z-10"
              :title="$t('CoursesView.deleteTitle')"
          >
            <Trash2 :size="16" class="text-red-600" />
          </button>
 
          <div class="flex items-center gap-4 mb-4">
            <div class="bg-gray-50 p-3 rounded-lg text-blue-600 group-hover:text-primary transition-colors flex items-center justify-center w-[56px] h-[56px] flex-shrink-0 border border-gray-100">
              <GraduationCap :size="32" />
            </div>
            <h3 class="font-bold text-xl text-gray-900 leading-tight pr-10">
              {{ course.name }}
            </h3>
          </div>
 
          <p class="text-gray-600 text-sm mb-6 flex-grow leading-relaxed text-left flex items-center gap-2">
            <template v-if="isStaff">
              <Users :size="14" class="text-gray-400" />
              <span>
                {{ memberCounts[course.courseId] ?? 0 }}
                {{ (memberCounts[course.courseId] ?? 0) === 1 ? $t('CoursesView.memberSingular') : $t('CoursesView.memberPlural') }}
              </span>
            </template>
            <span v-else class="text-gray-400 italic">
              {{ $t('CoursesView.openToView') }}
            </span>
          </p>
 
          <div class="mt-auto">
            <BaseButton
                variant="green"
                class="w-full flex items-center justify-center gap-2"
                @click.stop="goToDetail(course.courseId)"
            >
              {{ $t('CoursesView.openDetails') }}
            </BaseButton>
          </div>
        </Card>
      </div>
    </EntityListState>
 
    <Modal :show="showModal" @close="showModal = false">
      <template #header>
        <h2 class="text-xl font-semibold">{{ $t('CoursesView.createModal.title') }}</h2>
      </template>
 
      <template #body>
        <div class="space-y-5">
          <p class="text-sm text-gray-500">
            {{ $t('CoursesView.createModal.intro') }}
          </p>
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1.5">
              {{ $t('CoursesView.createModal.nameLabel') }}
            </label>
            <BaseInput v-model="formData.name" :placeholder="$t('CoursesView.createModal.namePlaceholder')" required @keyup.enter="saveCourse" />
          </div>
        </div>
      </template>
 
      <template #footer>
        <div class="flex justify-end gap-3">
          <BaseButton variant="ghost" @click="showModal = false">
            {{ $t('CoursesView.createModal.cancel') }}
          </BaseButton>
          <BaseButton @click="saveCourse" :disabled="!formData.name">
            {{ $t('CoursesView.createModal.create') }}
          </BaseButton>
        </div>
      </template>
    </Modal>
 
    <Modal :show="showDeleteModal" @close="closeDeleteModal">
      <template #header>
        <h2 class="text-xl font-semibold text-red-700">{{ $t('CoursesView.deleteModal.title') }}</h2>
      </template>
 
      <template #body>
        <div class="space-y-3">
          <p class="text-gray-700" v-html="$t('CoursesView.deleteModal.confirmPrompt', { name: courseToDelete?.name })"></p>
          <p class="text-sm text-gray-500">
            {{ $t('CoursesView.deleteModal.warning') }}
          </p>
        </div>
      </template>
 
      <template #footer>
        <div class="flex justify-end gap-3">
          <BaseButton variant="ghost" @click="closeDeleteModal" :disabled="isDeleting">
            {{ $t('CoursesView.deleteModal.cancel') }}
          </BaseButton>
          <BaseButton variant="red" @click="confirmDelete" :disabled="isDeleting">
            {{ isDeleting ? $t('CoursesView.deleteModal.deleting') : $t('CoursesView.deleteModal.delete') }}
          </BaseButton>
        </div>
      </template>
    </Modal>
  </div>
</template>