Files
classroomio/apps/dashboard/src/lib/components/Courses/index.svelte
T

149 lines
5.2 KiB
Svelte
Raw Normal View History

2023-09-27 04:23:45 +01:00
<script lang="ts">
2021-08-29 07:23:19 +03:00
import Box from '../Box/index.svelte';
import Card from './components/Card/index.svelte';
2024-04-20 14:40:44 +01:00
import List from './components/List/index.svelte';
import CardLoader from './components/Card/Loader.svelte';
import CoursesEmptyIcon from '../Icons/CoursesEmptyIcon.svelte';
2023-09-27 04:23:45 +01:00
import { courseMetaDeta } from './store';
import type { Course } from '$lib/utils/types';
import { globalStore } from '$lib/utils/store/app';
2024-04-20 14:40:44 +01:00
import {
StructuredList,
StructuredListHead,
StructuredListRow,
StructuredListCell,
StructuredListBody
} from 'carbon-components-svelte';
2024-04-19 05:45:48 +01:00
import { t } from '$lib/utils/functions/translations';
2025-10-29 09:57:29 +01:00
import CopyCourseModal from './components/CopyCourseModal/index.svelte';
import DeleteModal from '$lib/components/Modal/DeleteModal.svelte';
import { deleteCourse } from '$lib/utils/services/courses';
import { snackbar } from '$lib/components/Snackbar/store';
import { deleteCourseModal, deleteCourseModalInitialState, courses as coursesStore } from './store';
2023-09-27 04:23:45 +01:00
export let courses: Course[] = [];
2024-04-20 15:05:45 +01:00
export let emptyTitle = $t('courses.course_card.empty_title');
export let emptyDescription = $t('courses.course_card.empty_description');
export let isExplore = false;
function calcProgressRate(progressRate?: number, totalLessons?: number): number {
if (!progressRate || !totalLessons) {
return 0;
}
return Math.round((progressRate / totalLessons) * 100);
}
2025-10-29 09:57:29 +01:00
async function handleDeleteCourse() {
if (!$deleteCourseModal.id) return;
$deleteCourseModal.isDeleting = true;
try {
await deleteCourse($deleteCourseModal.id);
// Remove the course from the courses store
$coursesStore = $coursesStore.filter(course => course.id !== $deleteCourseModal.id);
// Show success message
snackbar.success('snackbar.course_deleted');
// Close modal and reset state
deleteCourseModal.set(deleteCourseModalInitialState);
} catch (error) {
console.error('Error deleting course:', error);
snackbar.error('snackbar.course_settings.error.went_wrong');
// Stop deleting state on error
$deleteCourseModal.isDeleting = false;
}
}
2021-05-06 08:22:25 +03:00
</script>
2025-10-29 09:57:29 +01:00
<CopyCourseModal />
<DeleteModal
onDelete={handleDeleteCourse}
bind:open={$deleteCourseModal.open}
isLoading={$deleteCourseModal.isDeleting}
/>
2021-12-30 22:30:30 +02:00
<div class="mx-auto my-4 w-full">
2021-12-30 22:30:30 +02:00
{#if $courseMetaDeta.isLoading}
2024-04-20 14:40:44 +01:00
<section class={`${$courseMetaDeta.isLoading || courses ? 'cards-container' : ''} `}>
<CardLoader />
<CardLoader />
<CardLoader />
</section>
{:else if $courseMetaDeta.view === 'list'}
<StructuredList selection class="w-full">
<StructuredListHead>
<StructuredListRow head>
2024-04-20 15:05:45 +01:00
<StructuredListCell head>
{$t('courses.course_card.list_view.title')}
</StructuredListCell>
<StructuredListCell head>
{$t('courses.course_card.list_view.description')}
</StructuredListCell>
<StructuredListCell head>
{$t('courses.course_card.list_view.type')}
</StructuredListCell>
2024-04-20 15:05:45 +01:00
<StructuredListCell head>
{$t('courses.course_card.list_view.lessons')}
</StructuredListCell>
<StructuredListCell head>
{$t('courses.course_card.list_view.students')}
</StructuredListCell>
<StructuredListCell head>
{$t('courses.course_card.list_view.published')}
</StructuredListCell>
2024-04-20 14:40:44 +01:00
<StructuredListCell head>{''}</StructuredListCell>
</StructuredListRow>
</StructuredListHead>
<StructuredListBody>
{#each courses as courseData}
<List
id={courseData.id}
title={courseData.title}
type={$t(`course.navItem.settings.${courseData.type.toLowerCase()}`)}
2024-04-20 14:40:44 +01:00
description={courseData.description}
isPublished={courseData.is_published}
totalLessons={courseData.total_lessons}
totalStudents={courseData.total_students}
/>
{/each}
</StructuredListBody>
</StructuredList>
2021-12-30 22:30:30 +02:00
{:else}
<section class={`relative ${$courseMetaDeta.isLoading || courses ? 'cards-container' : ''} `}>
2024-04-20 14:40:44 +01:00
{#each courses as courseData}
{#key courseData.id}
<Card
id={courseData.id}
slug={courseData.slug}
2024-04-20 14:40:44 +01:00
bannerImage={courseData.logo || '/images/classroomio-course-img-template.jpg'}
title={courseData.title}
description={courseData.description}
isPublished={courseData.is_published}
type={courseData.type}
2024-04-20 14:40:44 +01:00
currency={courseData.currency}
totalLessons={courseData.total_lessons}
totalStudents={courseData.total_students}
isLMS={$globalStore.isOrgSite}
{isExplore}
2024-04-20 14:40:44 +01:00
progressRate={calcProgressRate(courseData.progress_rate, courseData.total_lessons)}
/>
{/key}
{/each}
</section>
2021-12-30 22:30:30 +02:00
{/if}
2021-05-07 03:43:37 +03:00
</div>
2023-09-27 04:23:45 +01:00
{#if !$courseMetaDeta.isLoading && !courses.length}
<Box className="w-full">
<CoursesEmptyIcon />
<h3 class="my-5 text-2xl dark:text-white">{emptyTitle}</h3>
<p class="w-1/3 text-center dark:text-white">
{emptyDescription}
</p>
</Box>
{/if}