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

118 lines
3.3 KiB
Svelte
Raw Normal View History

2023-05-20 23:53:28 +00:00
<script>
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';
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 = [];
async function fetchCommunityQuestions(orgId) {
if (!orgId) return;
isLoading = true;
2023-05-20 23:53:28 +00:00
const { data, error } = await supabase
.from('community_question')
.select(
`
organization_id,
title,
votes,
created_at,
slug,
comments:community_answer(count),
author:organizationmember!community_question_author_id_fkey!inner(
profile!inner(fullname)
)
`
)
.eq('organization_id', orgId);
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,
slug: discussion.slug,
author: discussion?.author?.profile?.fullname,
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);
</script>
<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 discussions as discussion}
<div class="w-full flex border-bottom-c p-5">
<Vote value={discussion.votes} />
<div class="discussion-topic-author">
<h4>
<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>
</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 />
<h3 class="dark:text-white text-2xl my-5">No Questions asked</h3>
<p class="dark:text-white w-1/3 text-center">Ask a question to the community</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;
}
.discussion-topic-author {
margin-right: 20px;
}
.discussion-topic-author span {
font-size: 12px;
}
</style>