2023-12-13 14:03:02 +01:00
|
|
|
<script lang="ts">
|
2025-11-01 15:49:29 +01:00
|
|
|
import { untrack } from 'svelte';
|
2023-08-02 19:35:27 +01:00
|
|
|
import { goto } from '$app/navigation';
|
2025-11-21 12:18:04 +01:00
|
|
|
import * as Select from '@cio/ui/base/select';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { Search } from '@cio/ui/custom/search';
|
2025-11-01 15:49:29 +01:00
|
|
|
import MessageCirclePlusIcon from '@lucide/svelte/icons/message-circle-plus';
|
|
|
|
|
|
2025-11-21 12:18:04 +01:00
|
|
|
import { profile } from '$lib/utils/store/user';
|
|
|
|
|
import { t } from '$lib/utils/functions/translations';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { courses } from '$lib/features/course/utils/store';
|
2025-11-21 12:18:04 +01:00
|
|
|
import { calDateDiff } from '$lib/utils/functions/date';
|
|
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
|
|
|
|
import { fetchCourses } from '$lib/utils/services/courses';
|
|
|
|
|
import { currentOrg, currentOrgPath } from '$lib/utils/store/org';
|
2025-12-02 09:54:38 +01:00
|
|
|
import MessageSquareMoreIcon from '@lucide/svelte/icons/message-square-more';
|
2025-11-21 12:18:04 +01:00
|
|
|
|
2025-12-02 09:54:38 +01:00
|
|
|
import { CommunityListLoader } from '../components';
|
2025-11-21 12:18:04 +01:00
|
|
|
import Vote from '$lib/components/Vote/index.svelte';
|
|
|
|
|
import CoursesEmptyIcon from '$lib/components/Icons/CoursesEmptyIcon.svelte';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { Empty } from '@cio/ui/custom/empty';
|
|
|
|
|
import * as Page from '@cio/ui/base/page';
|
|
|
|
|
|
|
|
|
|
import { AskCommunityButton } from '../components';
|
2023-05-20 23:53:28 +00:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
interface Props {
|
|
|
|
|
isLMS?: boolean;
|
|
|
|
|
}
|
2023-08-17 14:05:01 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let { isLMS = false }: Props = $props();
|
2023-05-20 23:53:28 +00:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let isLoading = $state(false);
|
|
|
|
|
let discussions = $state([]);
|
|
|
|
|
let searchValue = $state('');
|
|
|
|
|
let allCourses: any[] = $state([]);
|
|
|
|
|
let selectedId = $state('');
|
|
|
|
|
|
|
|
|
|
function fetchCommunityQuestions(orgId?: string, profileId?: string) {
|
2024-02-13 22:17:15 +01:00
|
|
|
if (!orgId || !profileId) return;
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
untrack(async () => {
|
|
|
|
|
isLoading = true;
|
2024-02-13 22:17:15 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
if ($courses.length) {
|
|
|
|
|
allCourses = [...$courses];
|
|
|
|
|
} else {
|
|
|
|
|
const courseResult = (await fetchCourses(profileId, orgId)) || { allCourses: [] };
|
|
|
|
|
allCourses = courseResult.allCourses;
|
|
|
|
|
}
|
2024-02-13 22:17:15 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
const courseIds = allCourses.map((course) => course.id);
|
|
|
|
|
const courseIdsFilter = `(${courseIds.join(',')})`;
|
|
|
|
|
|
|
|
|
|
const { data, error } = await supabase
|
|
|
|
|
.from('community_question')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
2023-05-20 23:53:28 +00:00
|
|
|
organization_id,
|
2024-02-13 22:17:15 +01:00
|
|
|
course_id,
|
2023-05-20 23:53:28 +00:00
|
|
|
title,
|
|
|
|
|
votes,
|
|
|
|
|
created_at,
|
|
|
|
|
slug,
|
|
|
|
|
comments:community_answer(count),
|
2023-12-13 14:03:02 +01:00
|
|
|
author:profile(
|
|
|
|
|
fullname
|
2024-02-13 22:17:15 +01:00
|
|
|
),
|
|
|
|
|
course!inner (
|
|
|
|
|
title
|
2023-05-20 23:53:28 +00:00
|
|
|
)
|
|
|
|
|
`
|
2025-11-01 15:49:29 +01:00
|
|
|
)
|
|
|
|
|
.filter('course_id', 'in', courseIdsFilter)
|
|
|
|
|
.order('created_at', { ascending: false });
|
|
|
|
|
console.log('data', data);
|
|
|
|
|
console.log('error', error);
|
2024-02-13 22:17:15 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
isLoading = false;
|
2023-05-20 23:53:28 +00:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
if (error) {
|
|
|
|
|
console.error('Error loading community', error);
|
|
|
|
|
return goto(isLMS ? '/lms' : $currentOrgPath);
|
|
|
|
|
}
|
2023-05-20 23:53:28 +00:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
discussions =
|
|
|
|
|
data?.map((discussion) => ({
|
|
|
|
|
title: discussion.title,
|
|
|
|
|
courseId: discussion.course_id,
|
|
|
|
|
courseTitle: discussion.course?.title,
|
|
|
|
|
slug: discussion.slug,
|
|
|
|
|
author: discussion?.author?.fullname,
|
|
|
|
|
comments: discussion.comments?.[0]?.count || 0,
|
|
|
|
|
votes: discussion.votes,
|
|
|
|
|
createdAt: calDateDiff(discussion.created_at)
|
|
|
|
|
})) || [];
|
|
|
|
|
});
|
2023-05-20 23:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
$effect(() => {
|
|
|
|
|
fetchCommunityQuestions($currentOrg.id, $profile.id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let filteredDiscussions = $derived(
|
|
|
|
|
discussions.filter(
|
|
|
|
|
(discussion) =>
|
|
|
|
|
discussion.title.toLowerCase().includes(searchValue.toLowerCase()) &&
|
|
|
|
|
(!selectedId || discussion.courseId === selectedId)
|
|
|
|
|
)
|
2024-02-13 22:17:15 +01:00
|
|
|
);
|
2023-05-20 23:53:28 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-12-02 09:54:38 +01:00
|
|
|
<Page.BodyHeader>
|
|
|
|
|
<Search placeholder={$t('community.find_question')} bind:value={searchValue} />
|
|
|
|
|
|
|
|
|
|
<Select.Root type="single" bind:value={selectedId}>
|
|
|
|
|
<Select.Trigger class="w-full bg-gray-100 dark:bg-neutral-800">
|
|
|
|
|
<p class="truncate">
|
|
|
|
|
{selectedId ? allCourses.find((course) => course.id === selectedId)?.title : $t('community.all')}
|
|
|
|
|
</p>
|
|
|
|
|
</Select.Trigger>
|
|
|
|
|
<Select.Content>
|
|
|
|
|
<Select.Item value="">{$t('community.all')}</Select.Item>
|
|
|
|
|
{#each allCourses as course}
|
|
|
|
|
<Select.Item value={course.id}>{course.title}</Select.Item>
|
|
|
|
|
{/each}
|
|
|
|
|
</Select.Content>
|
|
|
|
|
</Select.Root>
|
|
|
|
|
</Page.BodyHeader>
|
|
|
|
|
|
|
|
|
|
{#if isLoading}
|
|
|
|
|
<CommunityListLoader />
|
|
|
|
|
<CommunityListLoader />
|
|
|
|
|
<CommunityListLoader />
|
|
|
|
|
<CommunityListLoader />
|
|
|
|
|
{:else}
|
|
|
|
|
{#each filteredDiscussions as discussion}
|
|
|
|
|
<div
|
|
|
|
|
class="border-c m-auto my-4 flex flex-wrap items-center justify-center rounded bg-gray-100 lg:justify-start dark:bg-neutral-800"
|
|
|
|
|
>
|
2025-01-04 05:55:14 +01:00
|
|
|
<div class="border-bottom-c flex w-full p-3">
|
2023-09-05 10:23:42 +00:00
|
|
|
<Vote value={discussion.votes} />
|
2025-01-04 05:55:14 +01:00
|
|
|
<div class="flex flex-col gap-y-0.5 text-sm">
|
2024-02-13 22:17:15 +01:00
|
|
|
<h4 class="mt-0">
|
2025-11-01 15:49:29 +01:00
|
|
|
<a class="text-black dark:text-white" href="{isLMS ? '/lms' : $currentOrgPath}/community/{discussion.slug}">
|
2023-09-05 10:23:42 +00:00
|
|
|
{discussion.title}
|
|
|
|
|
</a>
|
|
|
|
|
</h4>
|
|
|
|
|
<span class="text-gray-600 dark:text-white">
|
|
|
|
|
{discussion.author} asked {discussion.createdAt}
|
|
|
|
|
</span>
|
2024-02-13 22:17:15 +01:00
|
|
|
<a class="m-0" href="/courses/{discussion.courseId}">
|
2025-01-04 05:55:14 +01:00
|
|
|
<span class="text-primary-200 text-primary-700 p-0 text-xs dark:text-black">
|
2024-02-13 22:17:15 +01:00
|
|
|
#{discussion.courseTitle}
|
|
|
|
|
</span>
|
|
|
|
|
</a>
|
2023-09-05 10:23:42 +00:00
|
|
|
</div>
|
2025-11-01 15:49:29 +01:00
|
|
|
<div class="flex-grow"></div>
|
2023-09-05 10:23:42 +00:00
|
|
|
<div class="flex items-center">
|
2025-11-01 15:49:29 +01:00
|
|
|
<MessageCirclePlusIcon size={16} />
|
2023-09-05 10:23:42 +00:00
|
|
|
<span class="ml-1">{discussion.comments}</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-12-02 09:54:38 +01:00
|
|
|
</div>
|
|
|
|
|
{:else}
|
|
|
|
|
<Empty
|
|
|
|
|
title={$t('community.no_question')}
|
|
|
|
|
description={$t('community.ask_a_question')}
|
|
|
|
|
icon={MessageSquareMoreIcon}
|
|
|
|
|
variant="page"
|
|
|
|
|
>
|
|
|
|
|
<AskCommunityButton />
|
|
|
|
|
</Empty>
|
|
|
|
|
{/each}
|
|
|
|
|
{/if}
|
2023-05-20 23:53:28 +00:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
h4 {
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
font-weight: 900;
|
|
|
|
|
word-break: break-word;
|
|
|
|
|
overflow-wrap: break-word;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
|
|
|
|
</style>
|