From 07e8b3a5cb9ce7673b8c712e991c52295953a1b4 Mon Sep 17 00:00:00 2001 From: rotimi-best Date: Sat, 21 Aug 2021 13:26:43 +0300 Subject: [PATCH] Fetch course for CSR --- .../components/Lesson/Materials/index.svelte | 8 ++-- src/components/Courses/api.js | 16 +++++++ src/routes/courses.svelte | 45 +++++++++---------- .../lessons/[...lessonParams]/index.svelte | 6 +++ 4 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 src/components/Courses/api.js diff --git a/src/components/Course/components/Lesson/Materials/index.svelte b/src/components/Course/components/Lesson/Materials/index.svelte index c47086a0..59919cd3 100644 --- a/src/components/Course/components/Lesson/Materials/index.svelte +++ b/src/components/Course/components/Lesson/Materials/index.svelte @@ -1,9 +1,9 @@ {#if mode === MODES.edit} diff --git a/src/components/Courses/api.js b/src/components/Courses/api.js new file mode 100644 index 00000000..5dfacca3 --- /dev/null +++ b/src/components/Courses/api.js @@ -0,0 +1,16 @@ +import { supabase } from '../../utils/functions/supabase'; + +export async function fetchCourses(userId) { + const { data: orgMember } = await supabase + .from('organizationmember') + .select(`organization_id`) + .eq('profile_id', userId) + .single(); + + const { data: allCourses } = await supabase + .from('course') + .select(`id, title, description`) + .eq('organization_id', orgMember.organization_id); + + return { allCourses, organizationId: orgMember.organization_id }; +} diff --git a/src/routes/courses.svelte b/src/routes/courses.svelte index 4f0805a7..8d2a7535 100644 --- a/src/routes/courses.svelte +++ b/src/routes/courses.svelte @@ -1,35 +1,18 @@ @@ -51,6 +34,7 @@ export let allCourses; export let organizationId; + export let cantFetch; let errors = { emails: '', @@ -119,10 +103,25 @@ }); } - onMount(() => { + async function getCourses(userId) { + console.log(`getCourses process.browser`, process.browser); + if (process.browser) { + if (!cantFetch && typeof cantFetch === 'boolean' && !allCourses.length) { + const coursesResult = await fetchCourses(userId); + console.log(`coursesResult`, coursesResult); + + organizationId = coursesResult.organizationId; + courses.set(coursesResult.allCourses); + } + } + } + + onMount(async () => { + console.log(`onMount cantFetch`, cantFetch); courses.set(allCourses); }); - // $: tab = $page.query.tab || 'mine'; + + $: getCourses($profile.id); diff --git a/src/routes/courses/[id]/lessons/[...lessonParams]/index.svelte b/src/routes/courses/[id]/lessons/[...lessonParams]/index.svelte index 5529a1dd..ef3951e6 100644 --- a/src/routes/courses/[id]/lessons/[...lessonParams]/index.svelte +++ b/src/routes/courses/[id]/lessons/[...lessonParams]/index.svelte @@ -80,6 +80,12 @@ afterUpdate(setLesson); $: path = $page.path.replace(/\/exercises[\/ 0-9 a-z -]*/, ''); + + $: { + if (lessonId) { + mode = MODES.view; + } + }