2024-07-09 21:17:34 +01:00
|
|
|
<script lang="ts">
|
2025-11-01 15:49:29 +01:00
|
|
|
import { onMount, untrack } from 'svelte';
|
2024-07-09 21:17:34 +01:00
|
|
|
import { Search, Dropdown } from 'carbon-components-svelte';
|
|
|
|
|
import { profile } from '$lib/utils/store/user';
|
2024-07-21 03:02:33 +01:00
|
|
|
import { fetchExploreCourses } from '$lib/utils/services/courses';
|
2024-07-09 21:17:34 +01:00
|
|
|
import Courses from '$lib/components/Courses/index.svelte';
|
|
|
|
|
|
2024-07-21 03:02:33 +01:00
|
|
|
import { courseMetaDeta } from '$lib/components/Courses/store';
|
2024-07-09 21:17:34 +01:00
|
|
|
import { currentOrg } from '$lib/utils/store/org';
|
|
|
|
|
import type { Course } from '$lib/utils/types';
|
|
|
|
|
import { t } from '$lib/utils/functions/translations';
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
import { IconButton } from '$lib/components/IconButton';
|
|
|
|
|
import GridIcon from '@lucide/svelte/icons/grid-2x2';
|
|
|
|
|
import ListIcon from '@lucide/svelte/icons/list';
|
2024-07-09 21:17:34 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let searchValue = $state('');
|
|
|
|
|
let selectedId: string | undefined = $state();
|
2024-07-09 21:17:34 +01:00
|
|
|
let hasFetched = false;
|
2025-11-01 15:49:29 +01:00
|
|
|
let exploreCourseList: Course[] = $state([]);
|
2024-07-21 03:02:33 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
const filteredExploreCourses: Course[] = $derived(filterCourses(searchValue, selectedId || '', exploreCourseList));
|
|
|
|
|
|
|
|
|
|
function getCourses(userId?: string, orgId?: string) {
|
2024-07-09 21:17:34 +01:00
|
|
|
if (hasFetched || !userId || !orgId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
// don't rerun this function if any state is updated in this function.
|
|
|
|
|
untrack(async () => {
|
|
|
|
|
$courseMetaDeta.isLoading = true;
|
2024-07-09 21:17:34 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
const coursesResult = await fetchExploreCourses(userId, orgId);
|
2024-07-09 21:17:34 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
$courseMetaDeta.isLoading = false;
|
2024-07-09 21:17:34 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
if (!coursesResult) return;
|
2024-07-09 21:17:34 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
exploreCourseList = coursesResult.allCourses;
|
|
|
|
|
hasFetched = true;
|
|
|
|
|
});
|
2024-07-09 21:17:34 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
function filterCourses(searchValue: string, _selectedId: string | null, courses: Course[]) {
|
|
|
|
|
if (_selectedId) {
|
|
|
|
|
localStorage.setItem('classroomio_filter_course_key', _selectedId);
|
2024-07-09 21:17:34 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
const coursesFiltered = courses.filter((course) => {
|
2024-07-09 21:17:34 +01:00
|
|
|
if (!searchValue || course.title.toLowerCase().includes(searchValue.toLowerCase())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_selectedId === '0') {
|
2025-11-01 15:49:29 +01:00
|
|
|
return coursesFiltered.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
2024-07-09 21:17:34 +01:00
|
|
|
} else if (_selectedId === '1') {
|
2025-11-01 15:49:29 +01:00
|
|
|
return coursesFiltered.sort((a, b) => (b.total_lessons ?? 0) - (a.total_lessons ?? 0));
|
2024-07-09 21:17:34 +01:00
|
|
|
}
|
2025-11-01 15:49:29 +01:00
|
|
|
|
|
|
|
|
return coursesFiltered;
|
2024-07-09 21:17:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setViewPreference = (preference: 'grid' | 'list') => {
|
|
|
|
|
$courseMetaDeta.view = preference;
|
|
|
|
|
localStorage.setItem('courseView', preference);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
const courseView = localStorage.getItem('courseView') as 'grid' | 'list' | null;
|
|
|
|
|
|
|
|
|
|
if (courseView) {
|
|
|
|
|
$courseMetaDeta.view = courseView;
|
|
|
|
|
}
|
2025-11-01 15:49:29 +01:00
|
|
|
|
|
|
|
|
selectedId = localStorage.getItem('classroomio_filter_course_key') || '0';
|
2024-07-09 21:17:34 +01:00
|
|
|
});
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
$effect(() => {
|
2024-07-09 21:17:34 +01:00
|
|
|
getCourses($profile.id, $currentOrg.id);
|
2025-11-01 15:49:29 +01:00
|
|
|
});
|
2024-07-09 21:17:34 +01:00
|
|
|
</script>
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
<section class="w-full md:mx-auto md:max-w-6xl">
|
|
|
|
|
<div class="px-2 py-2 md:px-5 md:py-10">
|
|
|
|
|
<h1 class="text-3xl font-bold dark:text-white">{$t('explore.heading')}</h1>
|
2024-07-09 21:17:34 +01:00
|
|
|
<div class="flex flex-row-reverse">
|
|
|
|
|
<div class="filter-container flex items-end justify-start">
|
|
|
|
|
<Search
|
|
|
|
|
placeholder={$t('courses.search_placeholder')}
|
|
|
|
|
bind:value={searchValue}
|
|
|
|
|
searchClass="mr-2"
|
|
|
|
|
class=" bg-gray-100 dark:bg-neutral-800"
|
|
|
|
|
/>
|
|
|
|
|
<Dropdown
|
|
|
|
|
class="h-full"
|
|
|
|
|
size="xl"
|
|
|
|
|
bind:selectedId
|
|
|
|
|
items={[
|
|
|
|
|
{ id: '0', text: $t('courses.course_filter.date_created') },
|
|
|
|
|
{ id: '1', text: $t('courses.course_filter.lessons') }
|
|
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{#if $courseMetaDeta.view === 'list'}
|
|
|
|
|
<IconButton onClick={() => setViewPreference('grid')}>
|
2025-11-01 15:49:29 +01:00
|
|
|
<GridIcon size={16} />
|
2024-07-09 21:17:34 +01:00
|
|
|
</IconButton>
|
|
|
|
|
{:else}
|
|
|
|
|
<IconButton onClick={() => setViewPreference('list')}>
|
2025-11-01 15:49:29 +01:00
|
|
|
<ListIcon size={16} />
|
2024-07-09 21:17:34 +01:00
|
|
|
</IconButton>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Courses
|
2025-11-01 15:49:29 +01:00
|
|
|
courses={filteredExploreCourses}
|
2024-07-09 21:17:34 +01:00
|
|
|
emptyTitle={$t('explore.empty_heading')}
|
|
|
|
|
emptyDescription={$t('explore.empty_description')}
|
|
|
|
|
isExplore={true}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|