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

162 lines
4.9 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { goto } from '$app/navigation';
import AddCommentIcon from 'carbon-icons-svelte/lib/AddComment.svelte';
import CommunityLoader from './Loader.svelte';
import Vote from '$lib/components/Vote/index.svelte';
import Box from '$lib/components/Box/index.svelte';
import CoursesEmptyIcon from '$lib/components/Icons/CoursesEmptyIcon.svelte';
import Space from '$lib/components/Space/index.svelte';
import { currentOrgPath, currentOrg } from '$lib/utils/store/org';
import { supabase } from '$lib/utils/functions/supabase';
import { calDateDiff } from '$lib/utils/functions/date';
import { fetchCourses } from '$lib/components/Courses/api';
import { profile } from '$lib/utils/store/user';
import { Search, Dropdown } from 'carbon-components-svelte';
2024-04-19 05:45:48 +01:00
import { t } from '$lib/utils/functions/translations';
2024-05-22 05:49:10 +01:00
import { courses } from '$lib/components/Courses/store';
2023-05-20 23:53:28 +00:00
2023-08-17 14:05:01 +01:00
export let isLMS = false;
let isLoading = false;
2023-05-20 23:53:28 +00:00
let discussions = [];
let searchValue = '';
let allCourses: any[] = [];
let selectedId = '';
2023-05-20 23:53:28 +00:00
async function fetchCommunityQuestions(orgId?: string, profileId?: string) {
if (!orgId || !profileId) return;
isLoading = true;
2024-05-22 05:49:10 +01:00
if ($courses.length) {
allCourses = [...$courses];
} else {
const courseResult = (await fetchCourses(profileId, orgId)) || { allCourses: [] };
allCourses = courseResult.allCourses;
}
const courseIds = allCourses.map((course) => course.id);
const courseIdsFilter = `(${courseIds.join(',')})`;
2023-05-20 23:53:28 +00:00
const { data, error } = await supabase
.from('community_question')
.select(
`
organization_id,
course_id,
2023-05-20 23:53:28 +00:00
title,
votes,
created_at,
slug,
comments:community_answer(count),
author:profile(
fullname
),
course!inner (
title
2023-05-20 23:53:28 +00:00
)
`
)
.filter('course_id', 'in', courseIdsFilter)
.order('created_at', { ascending: false });
2023-05-20 23:53:28 +00:00
console.log('data', data);
console.log('error', error);
isLoading = false;
2023-05-20 23:53:28 +00:00
if (error) {
console.error('Error loading community', error);
2023-08-17 14:05:01 +01:00
return goto(isLMS ? '/lms' : $currentOrgPath);
2023-05-20 23:53:28 +00:00
}
discussions =
data?.map((discussion) => ({
title: discussion.title,
courseId: discussion.course_id,
courseTitle: discussion.course?.title,
2023-05-20 23:53:28 +00:00
slug: discussion.slug,
author: discussion?.author?.fullname,
2023-05-20 23:53:28 +00:00
comments: discussion.comments?.[0]?.count || 0,
votes: discussion.votes,
createdAt: calDateDiff(discussion.created_at)
2023-05-20 23:53:28 +00:00
})) || [];
}
$: fetchCommunityQuestions($currentOrg.id, $profile.id);
$: filteredDiscussions = discussions.filter(
(discussion) =>
discussion.title.toLowerCase().includes(searchValue.toLowerCase()) &&
(!selectedId || discussion.courseId === selectedId)
);
2023-05-20 23:53:28 +00:00
</script>
<div class="flex justify-between">
<Search
2024-04-19 05:45:48 +01:00
placeholder={$t('community.find_question')}
bind:value={searchValue}
searchClass="mr-2"
class=" bg-gray-100 dark:bg-neutral-800"
/>
<Dropdown
2024-04-19 05:45:48 +01:00
class="w-[25%] h-full"
size="xl"
2024-04-19 05:45:48 +01:00
label={$t('community.ask.select_course')}
items={allCourses.map((course) => ({ id: course.id, text: course.title }))}
bind:selectedId
/>
</div>
2023-05-20 23:53:28 +00:00
<div
2023-09-16 22:44:20 +01:00
class="flex items-center justify-center lg:justify-start flex-wrap my-4 m-auto border-c rounded bg-gray-100 dark:bg-neutral-800"
2023-05-20 23:53:28 +00:00
>
{#if isLoading}
<CommunityLoader />
<CommunityLoader />
<CommunityLoader />
<CommunityLoader />
2023-05-20 23:53:28 +00:00
{:else}
{#each filteredDiscussions as discussion}
<div class="w-full flex border-bottom-c p-3">
<Vote value={discussion.votes} />
<div class="text-sm flex flex-col gap-y-0.5">
<h4 class="mt-0">
<a
class="text-black dark:text-white"
href="{isLMS ? '/lms' : $currentOrgPath}/community/{discussion.slug}"
>
{discussion.title}
</a>
</h4>
<span class="text-gray-600 dark:text-white">
{discussion.author} asked {discussion.createdAt}
</span>
<a class="m-0" href="/courses/{discussion.courseId}">
<span class="text-xs text-primary-200 dark:text-black text-primary-700 p-0">
#{discussion.courseTitle}
</span>
</a>
</div>
<Space />
<div class="flex items-center">
<AddCommentIcon size={20} />
<span class="ml-1">{discussion.comments}</span>
</div>
</div>
{:else}
<Box className="w-screen">
<CoursesEmptyIcon />
2024-04-19 05:45:48 +01:00
<h3 class="dark:text-white text-2xl my-5">{$t('community.no_question')}</h3>
<p class="dark:text-white w-1/3 text-center">{$t('community.ask_a_question')}</p>
</Box>
{/each}
{/if}
2023-05-20 23:53:28 +00:00
</div>
<style>
h4 {
font-size: 16px;
font-weight: 900;
word-break: break-word;
overflow-wrap: break-word;
margin: 0;
}
</style>