feat: filter community questions by course (#157)
* checkpoint save * checkpoint * feat: course dropdown * added comments * feat: generate db migration * fixes * PR Fixes * PR Fixes * refactoring * show empty state * improve styling * fix styling issue * PR Fixes * PR Fixes * PR Fixes * dropdown for edit mode * test db schema change on staging * fix: change course_id to courseId * fix theming issue --------- Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
co-authored by
Rotimi Best
parent
75e32a22f6
commit
13ab070178
@@ -154,7 +154,6 @@ This repo is a mono repo that consists of 3 projects:
|
||||
```env
|
||||
PUBLIC_SUPABASE_URL=<API URL>
|
||||
PUBLIC_SUPABASE_ANON_KEY=<anon key>
|
||||
PUBLIC_SUPABASE_BUCKET_URL=<Inbucket URL>
|
||||
```
|
||||
|
||||
- To view the Supabase studio, open the Studio URL from the result of `pnpm supabase start`
|
||||
|
||||
@@ -127,6 +127,15 @@ code {
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.bx--list-box__menu-item__option {
|
||||
white-space: normal;
|
||||
}
|
||||
.bx--list-box--xl .bx--list-box__menu-item,
|
||||
.bx--list-box--lg .bx--list-box__menu-item,
|
||||
.bx--list-box__menu-item__option {
|
||||
height: fit-content !important;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 457px) {
|
||||
.org-slot {
|
||||
min-width: 100%;
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
<IconButton {onClick} {color} {contained} {size}>
|
||||
{#if size === 'large'}
|
||||
<CloseIcon size={24} class="carbon-icon dark:text-white" title="Delete" />
|
||||
<CloseIcon size={24} class="carbon-icon dark:text-black" title="Delete" />
|
||||
{:else}
|
||||
<CloseIcon size={20} class="carbon-icon dark:text-white" title="Delete" />
|
||||
<CloseIcon size={20} class="carbon-icon dark:text-black" title="Delete" />
|
||||
{/if}
|
||||
</IconButton>
|
||||
<!-- color="text-primary-700 bg-primary-200 dark:bg-gray-200 dark:text-black" -->
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
{:else}
|
||||
<TextChip
|
||||
value={shortenName($currentOrg.name)}
|
||||
className="bg-primary-200 font-bold mr-2"
|
||||
className="bg-primary-200 font-bold mr-2 dark:text-black"
|
||||
/>
|
||||
{/if}
|
||||
<span class="line-clamp-1">
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="h-[70px] w-full border border-gray-100 dark:bg-neutral-800 mr-4 mb-4 mt-2 rounded-md transition ease-in-out relative flex pt-3 px-3"
|
||||
class="h-[70px] w-full border border-gray-100 dark:bg-neutral-800 mx-4 my-2 rounded-md transition ease-in-out relative flex pt-3 px-3"
|
||||
>
|
||||
<div class="w-10 flex justify-evenly flex-col px-1 mr-2 rounded-md">
|
||||
<SkeletonPlaceholder style="width: 100%; height: 40%; border-radius: 4px;" />
|
||||
|
||||
@@ -9,20 +9,34 @@
|
||||
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';
|
||||
|
||||
export let isLMS = false;
|
||||
|
||||
let isLoading = false;
|
||||
let discussions = [];
|
||||
let searchValue = '';
|
||||
let allCourses: any[] = [];
|
||||
let selectedId = '';
|
||||
|
||||
async function fetchCommunityQuestions(orgId?: string) {
|
||||
if (!orgId) return;
|
||||
async function fetchCommunityQuestions(orgId?: string, profileId?: string) {
|
||||
if (!orgId || !profileId) return;
|
||||
isLoading = true;
|
||||
|
||||
const courseResult = (await fetchCourses(profileId, orgId)) || { allCourses: [] };
|
||||
allCourses = courseResult.allCourses;
|
||||
|
||||
const courseIds = allCourses.map((course) => course.id);
|
||||
const courseIdsFilter = `(${courseIds.join(',')})`;
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('community_question')
|
||||
.select(
|
||||
`
|
||||
organization_id,
|
||||
course_id,
|
||||
title,
|
||||
votes,
|
||||
created_at,
|
||||
@@ -30,13 +44,17 @@
|
||||
comments:community_answer(count),
|
||||
author:profile(
|
||||
fullname
|
||||
),
|
||||
course!inner (
|
||||
title
|
||||
)
|
||||
`
|
||||
)
|
||||
.eq('organization_id', orgId)
|
||||
.filter('course_id', 'in', courseIdsFilter)
|
||||
.order('created_at', { ascending: false });
|
||||
console.log('data', data);
|
||||
console.log('error', error);
|
||||
|
||||
isLoading = false;
|
||||
|
||||
if (error) {
|
||||
@@ -47,6 +65,8 @@
|
||||
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,
|
||||
@@ -55,9 +75,29 @@
|
||||
})) || [];
|
||||
}
|
||||
|
||||
$: fetchCommunityQuestions($currentOrg.id);
|
||||
$: fetchCommunityQuestions($currentOrg.id, $profile.id);
|
||||
$: filteredDiscussions = discussions.filter(
|
||||
(discussion) =>
|
||||
discussion.title.toLowerCase().includes(searchValue.toLowerCase()) &&
|
||||
(!selectedId || discussion.courseId === selectedId)
|
||||
);
|
||||
</script>
|
||||
|
||||
<div class="flex justify-between">
|
||||
<Search
|
||||
placeholder="Find Question"
|
||||
bind:value={searchValue}
|
||||
searchClass="mr-2"
|
||||
class=" bg-gray-100 dark:bg-neutral-800"
|
||||
/>
|
||||
<Dropdown
|
||||
class="w-[25%]"
|
||||
size="xl"
|
||||
label="Select Course"
|
||||
items={allCourses.map((course) => ({ id: course.id, text: course.title }))}
|
||||
bind:selectedId
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
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"
|
||||
>
|
||||
@@ -67,11 +107,11 @@
|
||||
<CommunityLoader />
|
||||
<CommunityLoader />
|
||||
{:else}
|
||||
{#each discussions as discussion}
|
||||
<div class="w-full flex border-bottom-c p-5">
|
||||
{#each filteredDiscussions as discussion}
|
||||
<div class="w-full flex border-bottom-c p-3">
|
||||
<Vote value={discussion.votes} />
|
||||
<div class="discussion-topic-author">
|
||||
<h4>
|
||||
<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}"
|
||||
@@ -82,6 +122,11 @@
|
||||
<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">
|
||||
@@ -107,11 +152,4 @@
|
||||
overflow-wrap: break-word;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.discussion-topic-author {
|
||||
margin-right: 20px;
|
||||
}
|
||||
.discussion-topic-author span {
|
||||
font-size: 12px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -92,6 +92,8 @@
|
||||
function gotoSetting(pathname) {
|
||||
goto(`${$currentOrgPath}/settings${pathname}`);
|
||||
}
|
||||
|
||||
$: console.log('theme', $currentOrg.theme);
|
||||
</script>
|
||||
|
||||
<Grid class="border-c rounded border-gray-200 w-full mt-5">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export function setTheme(theme: string) {
|
||||
export function setTheme(theme?: string) {
|
||||
// In case the default theme is added but another theme exists
|
||||
if (!theme && document.body.className.includes('theme-')) {
|
||||
const regex = /theme-[\w]+/gi;
|
||||
@@ -7,8 +7,8 @@ export function setTheme(theme: string) {
|
||||
}
|
||||
|
||||
// In case theme already exists in dom, don't add
|
||||
if (document.body.className.includes(theme)) return;
|
||||
if (document.body.className.includes(theme || '')) return;
|
||||
|
||||
localStorage.setItem('theme', theme);
|
||||
document.body.className = document.body.className.concat(' ', theme);
|
||||
localStorage.setItem('theme', theme || '');
|
||||
document.body.className = document.body.className.concat(' ', theme || '');
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ const askCommunityValidationSchema = z.object({
|
||||
body: z.string().min(20, {
|
||||
message: 'Must be 10 or more characters long',
|
||||
invalid_type_error: 'Must not be empty'
|
||||
}),
|
||||
courseId: z.string().min(36, {
|
||||
message: 'Select a course',
|
||||
invalid_type_error: 'Select a course'
|
||||
})
|
||||
});
|
||||
const commentInCommunityValidationSchema = z.object({
|
||||
|
||||
@@ -200,6 +200,7 @@ export async function getCourseBySiteName(siteName: string) {
|
||||
}
|
||||
|
||||
export async function getCurrentOrg(siteName: string, justGet = false) {
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('organization')
|
||||
.select(
|
||||
@@ -214,6 +215,9 @@ export async function getCurrentOrg(siteName: string, justGet = false) {
|
||||
)
|
||||
.eq('siteName', siteName)
|
||||
.returns<CurrentOrg[]>();
|
||||
console.log('data =', data);
|
||||
console.log('error =', error);
|
||||
|
||||
|
||||
const isDataEmpty = !data?.[0];
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ export const load = async ({ url, cookies }): Promise<LoadOutput> => {
|
||||
response.isOrgSite = debugMode || answer;
|
||||
response.orgSiteName = debugMode ? _orgSiteName : subdomain;
|
||||
response.org = (await getCurrentOrg(response.orgSiteName, true)) || null;
|
||||
|
||||
|
||||
if (!response.org && !dev) {
|
||||
throw redirect(307, 'https://app.classroomio.com/404?type=org');
|
||||
|
||||
@@ -184,9 +184,8 @@
|
||||
goto(`/org/${orgRes.currentOrg.siteName}`);
|
||||
}
|
||||
}
|
||||
if (typeof orgRes?.currentOrg?.theme === 'string') {
|
||||
setTheme(orgRes.currentOrg.theme);
|
||||
}
|
||||
|
||||
setTheme(orgRes?.currentOrg?.theme);
|
||||
}
|
||||
|
||||
if (!profileData && !isPublicRoute($page.url?.pathname)) {
|
||||
@@ -286,7 +285,7 @@
|
||||
{#if data.skipAuth}
|
||||
<PlayQuiz />
|
||||
{:else if data.isOrgSite && !path}
|
||||
<OrgLandingPage orgSiteName={data.orgSiteName} org={data.org} />
|
||||
<OrgLandingPage orgSiteName={data.orgSiteName} org={data.org || {}} />
|
||||
{:else}
|
||||
<main class="dark:bg-black">
|
||||
{#if $navigating && $delayedPreloading}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import pluralize from 'pluralize';
|
||||
import TrashCanIcon from 'carbon-icons-svelte/lib/TrashCan.svelte';
|
||||
import { SkeletonPlaceholder, SkeletonText } from 'carbon-components-svelte';
|
||||
import { Dropdown, SkeletonPlaceholder, SkeletonText } from 'carbon-components-svelte';
|
||||
import ArrowLeftIcon from 'carbon-icons-svelte/lib/ArrowLeft.svelte';
|
||||
import Vote from '$lib/components/Vote/index.svelte';
|
||||
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
||||
@@ -10,7 +10,7 @@
|
||||
import Avatar from '$lib/components/Avatar/index.svelte';
|
||||
import IconButton from '$lib/components/IconButton/index.svelte';
|
||||
import CheckmarkOutlineIcon from 'carbon-icons-svelte/lib/CheckmarkOutline.svelte';
|
||||
import { isOrgAdmin } from '$lib/utils/store/org';
|
||||
import { currentOrg, isOrgAdmin } from '$lib/utils/store/org';
|
||||
import { profile } from '$lib/utils/store/user';
|
||||
import { supabase } from '$lib/utils/functions/supabase';
|
||||
import {
|
||||
@@ -23,6 +23,7 @@
|
||||
import TextEditor from '$lib/components/TextEditor/index.svelte';
|
||||
import { calDateDiff } from '$lib/utils/functions/date';
|
||||
import { browser } from '$app/environment';
|
||||
import { fetchCourses } from '$lib/components/Courses/api.js';
|
||||
|
||||
export let data;
|
||||
const { slug } = data;
|
||||
@@ -49,12 +50,14 @@
|
||||
createdAt: string;
|
||||
comments: Comment[];
|
||||
totalComments: number;
|
||||
courseId: string;
|
||||
}
|
||||
|
||||
let question: Question;
|
||||
let comment = '';
|
||||
let errors: {
|
||||
title?: string;
|
||||
courseId?: string;
|
||||
} = {};
|
||||
let isValidAnswer = false; // V2 allow admin mark an answer as accepted
|
||||
let resetInput = 1;
|
||||
@@ -77,10 +80,12 @@
|
||||
};
|
||||
let editContent = {
|
||||
title: '',
|
||||
body: ''
|
||||
body: '',
|
||||
courseId: ''
|
||||
};
|
||||
|
||||
let editorInstance = false;
|
||||
let fetchedCourses = [];
|
||||
|
||||
function mapResToQuestion(data): Question {
|
||||
return {
|
||||
@@ -103,10 +108,16 @@
|
||||
comment: c.body,
|
||||
createdAt: calDateDiff(c.created_at)
|
||||
})),
|
||||
totalComments: 0
|
||||
totalComments: 0,
|
||||
courseId: data.course_id
|
||||
};
|
||||
}
|
||||
|
||||
async function getCourses(userId: string | null, orgId: string) {
|
||||
const coursesResults = await fetchCourses(userId, orgId);
|
||||
fetchedCourses = coursesResults.allCourses;
|
||||
}
|
||||
|
||||
async function fetchCommunityQuestion(slug: string) {
|
||||
if (!slug) return;
|
||||
|
||||
@@ -119,6 +130,7 @@
|
||||
body,
|
||||
votes,
|
||||
created_at,
|
||||
course_id,
|
||||
slug,
|
||||
comments:community_answer(
|
||||
id,
|
||||
@@ -127,7 +139,10 @@
|
||||
created_at,
|
||||
author:profile(id, fullname, avatar_url)
|
||||
),
|
||||
author:profile(id, fullname, avatar_url)
|
||||
author:profile(id, fullname, avatar_url),
|
||||
course!inner (
|
||||
title
|
||||
)
|
||||
`
|
||||
)
|
||||
.eq('slug', slug)
|
||||
@@ -227,8 +242,7 @@
|
||||
|
||||
async function handleQuestionEdit() {
|
||||
if (isEditMode) {
|
||||
const fields = { title: editContent.title, body: editContent.body };
|
||||
errors = askCommunityValidation(fields);
|
||||
errors = askCommunityValidation(editContent);
|
||||
console.log('handleQuestionEdit errors', errors);
|
||||
|
||||
if (Object.keys(errors).length) {
|
||||
@@ -240,8 +254,7 @@
|
||||
editorInstance = !editorInstance;
|
||||
|
||||
if (!isEditMode) {
|
||||
const fields = { title: editContent.title, body: editContent.body };
|
||||
errors = askCommunityValidation(fields);
|
||||
errors = askCommunityValidation(editContent);
|
||||
console.log('handleQuestionEdit errors', errors);
|
||||
|
||||
if (Object.keys(errors).length) {
|
||||
@@ -249,21 +262,27 @@
|
||||
}
|
||||
const { error } = await supabase
|
||||
.from('community_question')
|
||||
.update(fields)
|
||||
.update({
|
||||
...editContent,
|
||||
course_id: editContent.courseId
|
||||
})
|
||||
.match({ id: question.id });
|
||||
if (error) {
|
||||
console.error('Error: handleQuestionEdit', error);
|
||||
snackbar.error('Error - Please try again later');
|
||||
} else {
|
||||
question.title = fields.title;
|
||||
question.body = fields.body;
|
||||
question.title = editContent.title;
|
||||
question.body = editContent.body;
|
||||
question.courseId = editContent.courseId;
|
||||
|
||||
editContent.title = '';
|
||||
editContent.body = '';
|
||||
editContent.courseId = '';
|
||||
}
|
||||
} else {
|
||||
editContent.title = question.title;
|
||||
editContent.body = question.body;
|
||||
editContent.courseId = question.courseId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +343,11 @@
|
||||
}
|
||||
|
||||
$: browser && fetchCommunityQuestion(slug);
|
||||
$: {
|
||||
if ($profile.id && $currentOrg.id) {
|
||||
getCourses($profile.id, $currentOrg.id);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -369,6 +393,13 @@
|
||||
className="w-full mr-2"
|
||||
errorMessage={errors.title}
|
||||
/>
|
||||
<Dropdown
|
||||
class="w-[25%] h-fit"
|
||||
size="xl"
|
||||
label="Select Course"
|
||||
items={fetchedCourses.map((course) => ({ id: course.id, text: course.title }))}
|
||||
bind:selectedId={editContent.courseId}
|
||||
/>
|
||||
{:else}
|
||||
<div class="flex items-center">
|
||||
<Vote
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import ArrowLeftIcon from 'carbon-icons-svelte/lib/ArrowLeft.svelte';
|
||||
import { Dropdown } from 'carbon-components-svelte';
|
||||
import { currentOrg } from '$lib/utils/store/org';
|
||||
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
||||
import { askCommunityValidation } from '$lib/utils/functions/validator';
|
||||
@@ -10,15 +11,25 @@
|
||||
import TextEditor from '$lib/components/TextEditor/index.svelte';
|
||||
import TextField from '$lib/components/Form/TextField.svelte';
|
||||
import { profile } from '$lib/utils/store/user';
|
||||
import { fetchCourses } from '$lib/components/Courses/api';
|
||||
|
||||
let errors: {
|
||||
title?: string;
|
||||
courseId?: string;
|
||||
} = {};
|
||||
let fields = {
|
||||
title: '',
|
||||
body: ''
|
||||
body: '',
|
||||
courseId: ''
|
||||
};
|
||||
|
||||
let fetchedCourses = [];
|
||||
|
||||
async function getCourses(userId: string | null, orgId: string) {
|
||||
const coursesResults = await fetchCourses(userId, orgId);
|
||||
fetchedCourses = coursesResults.allCourses;
|
||||
}
|
||||
|
||||
async function handleSave() {
|
||||
errors = askCommunityValidation(fields);
|
||||
console.log('handleSave errors', errors);
|
||||
@@ -30,7 +41,9 @@
|
||||
const { data: question, error } = await supabase
|
||||
.from('community_question')
|
||||
.insert({
|
||||
...fields,
|
||||
title: fields.title,
|
||||
body: fields.body,
|
||||
course_id: fields.courseId,
|
||||
organization_id: $currentOrg.id,
|
||||
author_profile_id: $profile.id,
|
||||
votes: 0,
|
||||
@@ -44,11 +57,17 @@
|
||||
} else {
|
||||
const slug = question[0]?.slug;
|
||||
console.log('Success: asking question in lms', question, slug);
|
||||
snackbar.success();
|
||||
snackbar.success('Question Submitted!');
|
||||
|
||||
goto(`/lms/community/${slug}`);
|
||||
}
|
||||
}
|
||||
|
||||
$: {
|
||||
if ($profile.id && $currentOrg.id) {
|
||||
getCourses($profile.id, $currentOrg.id);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -66,8 +85,22 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 p-2">
|
||||
<TextField bind:value={fields.title} placeholder="Title" errorMessage={errors.title} />
|
||||
<div class="mb-3 p-2 flex gap-x-5 justify-between">
|
||||
<TextField
|
||||
bind:value={fields.title}
|
||||
placeholder="Title"
|
||||
errorMessage={errors.title}
|
||||
className="w-[75%]"
|
||||
/>
|
||||
<Dropdown
|
||||
class="w-[25%]"
|
||||
size="xl"
|
||||
label="Select Course"
|
||||
invalid={!!errors.courseId}
|
||||
invalidText={errors.courseId}
|
||||
items={fetchedCourses.map((course) => ({ id: course.id, text: course.title }))}
|
||||
bind:selectedId={fields.courseId}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<TextEditor
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import pluralize from 'pluralize';
|
||||
import TrashCanIcon from 'carbon-icons-svelte/lib/TrashCan.svelte';
|
||||
import { SkeletonPlaceholder, SkeletonText } from 'carbon-components-svelte';
|
||||
import { Dropdown, SkeletonPlaceholder, SkeletonText } from 'carbon-components-svelte';
|
||||
import ArrowLeftIcon from 'carbon-icons-svelte/lib/ArrowLeft.svelte';
|
||||
import Vote from '$lib/components/Vote/index.svelte';
|
||||
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
||||
@@ -10,7 +10,7 @@
|
||||
import Avatar from '$lib/components/Avatar/index.svelte';
|
||||
import IconButton from '$lib/components/IconButton/index.svelte';
|
||||
import CheckmarkOutlineIcon from 'carbon-icons-svelte/lib/CheckmarkOutline.svelte';
|
||||
import { currentOrgPath, isOrgAdmin } from '$lib/utils/store/org';
|
||||
import { currentOrgPath, isOrgAdmin, currentOrg } from '$lib/utils/store/org';
|
||||
import { profile } from '$lib/utils/store/user';
|
||||
import { supabase } from '$lib/utils/functions/supabase';
|
||||
import {
|
||||
@@ -23,6 +23,7 @@
|
||||
import TextEditor from '$lib/components/TextEditor/index.svelte';
|
||||
import { calDateDiff } from '$lib/utils/functions/date';
|
||||
import { browser } from '$app/environment';
|
||||
import { fetchCourses } from '$lib/components/Courses/api.js';
|
||||
|
||||
export let data;
|
||||
const { slug } = data;
|
||||
@@ -49,6 +50,7 @@
|
||||
createdAt: string;
|
||||
comments: Comment[];
|
||||
totalComments: number;
|
||||
courseId: string;
|
||||
}
|
||||
|
||||
let question: Question;
|
||||
@@ -77,10 +79,12 @@
|
||||
};
|
||||
let editContent = {
|
||||
title: '',
|
||||
body: ''
|
||||
body: '',
|
||||
courseId: ''
|
||||
};
|
||||
|
||||
let editorInstance = false;
|
||||
let fetchedCourses = [];
|
||||
|
||||
function mapResToQuestion(data): Question {
|
||||
return {
|
||||
@@ -103,10 +107,16 @@
|
||||
comment: c.body,
|
||||
createdAt: calDateDiff(c.created_at)
|
||||
})),
|
||||
totalComments: 0
|
||||
totalComments: 0,
|
||||
courseId: data.course_id
|
||||
};
|
||||
}
|
||||
|
||||
async function getCourses(userId: string | null, orgId: string) {
|
||||
const coursesResults = await fetchCourses(userId, orgId);
|
||||
fetchedCourses = coursesResults.allCourses;
|
||||
}
|
||||
|
||||
async function fetchCommunityQuestion(slug: string) {
|
||||
if (!slug) return;
|
||||
|
||||
@@ -119,6 +129,7 @@
|
||||
body,
|
||||
votes,
|
||||
created_at,
|
||||
course_id,
|
||||
slug,
|
||||
comments:community_answer(
|
||||
id,
|
||||
@@ -127,7 +138,10 @@
|
||||
created_at,
|
||||
author:profile(id, fullname, avatar_url)
|
||||
),
|
||||
author:profile(id, fullname, avatar_url)
|
||||
author:profile(id, fullname, avatar_url),
|
||||
course!inner (
|
||||
title
|
||||
)
|
||||
`
|
||||
)
|
||||
.eq('slug', slug)
|
||||
@@ -227,8 +241,7 @@
|
||||
|
||||
async function handleQuestionEdit() {
|
||||
if (isEditMode) {
|
||||
const fields = { title: editContent.title, body: editContent.body };
|
||||
errors = askCommunityValidation(fields);
|
||||
errors = askCommunityValidation(editContent);
|
||||
console.log('handleQuestionEdit errors', errors);
|
||||
|
||||
if (Object.keys(errors).length) {
|
||||
@@ -240,8 +253,7 @@
|
||||
editorInstance = !editorInstance;
|
||||
|
||||
if (!isEditMode) {
|
||||
const fields = { title: editContent.title, body: editContent.body };
|
||||
errors = askCommunityValidation(fields);
|
||||
errors = askCommunityValidation(editContent);
|
||||
console.log('handleQuestionEdit errors', errors);
|
||||
|
||||
if (Object.keys(errors).length) {
|
||||
@@ -249,21 +261,27 @@
|
||||
}
|
||||
const { error } = await supabase
|
||||
.from('community_question')
|
||||
.update(fields)
|
||||
.update({
|
||||
...editContent,
|
||||
course_id: editContent.courseId
|
||||
})
|
||||
.match({ id: question.id });
|
||||
if (error) {
|
||||
console.error('Error: handleQuestionEdit', error);
|
||||
snackbar.error('Error - Please try again later');
|
||||
} else {
|
||||
question.title = fields.title;
|
||||
question.body = fields.body;
|
||||
question.title = editContent.title;
|
||||
question.body = editContent.body;
|
||||
question.courseId = editContent.courseId;
|
||||
|
||||
editContent.title = '';
|
||||
editContent.body = '';
|
||||
editContent.courseId = '';
|
||||
}
|
||||
} else {
|
||||
editContent.title = question.title;
|
||||
editContent.body = question.body;
|
||||
editContent.courseId = question.courseId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +342,11 @@
|
||||
}
|
||||
|
||||
$: browser && fetchCommunityQuestion(slug);
|
||||
$: {
|
||||
if ($profile.id && $currentOrg.id) {
|
||||
getCourses($profile.id, $currentOrg.id);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -372,6 +395,13 @@
|
||||
className="w-full mr-2"
|
||||
errorMessage={errors.title}
|
||||
/>
|
||||
<Dropdown
|
||||
class="w-[25%] h-fit"
|
||||
size="xl"
|
||||
label="Select Course"
|
||||
items={fetchedCourses.map((course) => ({ id: course.id, text: course.title }))}
|
||||
bind:selectedId={editContent.courseId}
|
||||
/>
|
||||
{:else}
|
||||
<div class="flex items-center">
|
||||
<Vote
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { Dropdown } from 'carbon-components-svelte';
|
||||
import ArrowLeftIcon from 'carbon-icons-svelte/lib/ArrowLeft.svelte';
|
||||
import { currentOrgPath, currentOrg } from '$lib/utils/store/org';
|
||||
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
||||
@@ -10,15 +11,27 @@
|
||||
import TextEditor from '$lib/components/TextEditor/index.svelte';
|
||||
import TextField from '$lib/components/Form/TextField.svelte';
|
||||
import { profile } from '$lib/utils/store/user';
|
||||
import { fetchCourses } from '$lib/components/Courses/api';
|
||||
|
||||
export let data;
|
||||
|
||||
let errors: {
|
||||
title?: string;
|
||||
courseId?: string;
|
||||
} = {};
|
||||
let fields = {
|
||||
title: '',
|
||||
body: ''
|
||||
body: '',
|
||||
courseId: ''
|
||||
};
|
||||
|
||||
let fetchedCourses = [];
|
||||
|
||||
async function getCourses(userId: string | null, orgId: string) {
|
||||
const coursesResults = await fetchCourses(userId, orgId);
|
||||
fetchedCourses = coursesResults.allCourses;
|
||||
}
|
||||
|
||||
async function handleSave() {
|
||||
errors = askCommunityValidation(fields);
|
||||
console.log('handleSave errors', errors);
|
||||
@@ -30,11 +43,13 @@
|
||||
const { data: question, error } = await supabase
|
||||
.from('community_question')
|
||||
.insert({
|
||||
...fields,
|
||||
title: fields.title,
|
||||
body: fields.body,
|
||||
organization_id: $currentOrg.id,
|
||||
author_profile_id: $profile.id,
|
||||
votes: 0,
|
||||
slug: generateSlug(fields.title)
|
||||
slug: generateSlug(fields.title),
|
||||
course_id: fields.courseId
|
||||
})
|
||||
.select();
|
||||
|
||||
@@ -49,6 +64,12 @@
|
||||
goto(`${$currentOrgPath}/community/${slug}`);
|
||||
}
|
||||
}
|
||||
|
||||
$: {
|
||||
if ($profile.id && $currentOrg.id) {
|
||||
getCourses($profile.id, $currentOrg.id);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -69,8 +90,22 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 p-2">
|
||||
<TextField bind:value={fields.title} placeholder="Title" errorMessage={errors.title} />
|
||||
<div class="mb-3 p-2 flex gap-x-5 justify-between">
|
||||
<TextField
|
||||
bind:value={fields.title}
|
||||
placeholder="Title"
|
||||
errorMessage={errors.title}
|
||||
className="w-[75%]"
|
||||
/>
|
||||
<Dropdown
|
||||
class="w-[25%]"
|
||||
size="xl"
|
||||
label="Select Course"
|
||||
invalid={!!errors.courseId}
|
||||
invalidText={errors.courseId}
|
||||
items={fetchedCourses.map((course) => ({ id: course.id, text: course.title }))}
|
||||
bind:selectedId={fields.courseId}
|
||||
/>
|
||||
</div>
|
||||
<div class="px-2">
|
||||
<TextEditor
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
create sequence "public"."test_tenant_id_seq";
|
||||
|
||||
create table "public"."test_tenant" (
|
||||
"id" integer not null default nextval('test_tenant_id_seq'::regclass),
|
||||
"details" text
|
||||
);
|
||||
|
||||
truncate "public"."community_question" cascade;
|
||||
|
||||
alter table "public"."community_question" add column "course_id" uuid not null;
|
||||
|
||||
alter sequence "public"."test_tenant_id_seq" owned by "public"."test_tenant"."id";
|
||||
|
||||
CREATE UNIQUE INDEX test_tenant_pkey ON public.test_tenant USING btree (id);
|
||||
|
||||
alter table "public"."test_tenant" add constraint "test_tenant_pkey" PRIMARY KEY using index "test_tenant_pkey";
|
||||
|
||||
alter table "public"."community_question" add constraint "community_question_course_id_fkey" FOREIGN KEY (course_id) REFERENCES course(id) not valid;
|
||||
|
||||
alter table "public"."community_question" validate constraint "community_question_course_id_fkey";
|
||||
+1
-1
@@ -496,7 +496,7 @@ INSERT INTO "public"."question_answer" ("id", "answers", "question_id", "open_an
|
||||
--
|
||||
|
||||
INSERT INTO "storage"."buckets" ("id", "name", "owner", "created_at", "updated_at", "public", "avif_autodetection", "file_size_limit", "allowed_mime_types", "owner_id") VALUES
|
||||
('avatars', 'avatars', true, '2023-11-22 09:29:04.533114+00', '2023-11-22 09:29:04.533114+00', true, false, NULL, NULL, NULL);
|
||||
('avatars', 'avatars', NULL, '2023-11-22 09:29:04.533114+00', '2023-11-22 09:29:04.533114+00', true, false, NULL, NULL, NULL);
|
||||
|
||||
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user