Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sveltekit fetch #414

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions app/src/lib/services/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const courseService = {
courses: new Map<string, Course>(),
courseUrl: "",

async getOrLoadCourse(courseId: string): Promise<Course> {
async getOrLoadCourse(courseId: string, fetchFunction: typeof fetch): Promise<Course> {
let course = this.courses.get(courseId);
let courseUrl = courseId;

Expand All @@ -21,37 +21,41 @@ export const courseService = {
}

try {
const response = await fetch(`https://${courseUrl}/tutors.json`);
const response = await fetchFunction(`https://${courseUrl}/tutors.json`);
if (!response.ok) {
throw new Error(`Fetch failed with status ${response.status}`);
}
const data = await response.json();
course = new Course(data, courseId, courseUrl);
this.courses.set(courseId, course);
} catch (error) {
console.log(error);
console.error(`Error fetching from URL: https://${courseUrl}/tutors.json`);
console.error(error);
throw error;
}
}

return course;
},

async readCourse(courseId: string): Promise<Course> {
const course = await this.getOrLoadCourse(courseId);
async readCourse(courseId: string, fetchFunction: typeof fetch): Promise<Course> {
const course = await this.getOrLoadCourse(courseId, fetchFunction);
currentCourse.set(course);
courseUrl.set(course.url);
week.set(course?.currentWeek);
this.course = course;
return course;
},

async readTopic(courseId: string, topicId: string): Promise<Topic> {
const course = await this.readCourse(courseId);
async readTopic(courseId: string, topicId: string, fetchFunction: typeof fetch): Promise<Topic> {
const course = await this.readCourse(courseId, fetchFunction);
const topic = course.topicIndex.get(topicId);
currentLo.set(topic.lo);
return topic;
},

async readLab(courseId: string, labId: string): Promise<Lab> {
const course = await this.readCourse(courseId);
async readLab(courseId: string, labId: string, fetchFunction: typeof fetch): Promise<Lab> {
const course = await this.readCourse(courseId, fetchFunction);

const lastSegment = labId.substring(labId.lastIndexOf("/") + 1);
if (!lastSegment.startsWith("book")) {
Expand All @@ -66,14 +70,14 @@ export const courseService = {
return lab;
},

async readWall(courseId: string, type: string): Promise<Lo[]> {
const course = await this.readCourse(courseId);
async readWall(courseId: string, type: string, fetchFunction: typeof fetch): Promise<Lo[]> {
const course = await this.readCourse(courseId, fetchFunction);
const wall = course.walls.get(type);
return wall;
},

async readLo(courseId: string, loId: string): Promise<Lo> {
const course = await this.readCourse(courseId);
async readLo(courseId: string, loId: string, fetchFunction: typeof fetch): Promise<Lo> {
const course = await this.readCourse(courseId, fetchFunction);
const lo = course.loIndex.get(loId);
currentLo.set(lo);
return lo;
Expand Down
6 changes: 5 additions & 1 deletion app/src/routes/(auth)/auth/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { page } from "$app/stores";
import Icon from "@iconify/svelte";
import { onMount } from "svelte";

Expand All @@ -14,7 +15,10 @@

async function handleSignInWithGitHub() {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "github"
provider: "github",
options: {
redirectTo: `${$page.url.origin}`
}
});
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions app/src/routes/(course-reader)/course/[courseid]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { currentLo } from "$lib/stores";

export const ssr = false;

export const load = async ({ params, parent }) => {
const course: Course = await courseService.readCourse(params.courseid);
export const load = async ({ params, parent, fetch }) => {
const course: Course = await courseService.readCourse(params.courseid, fetch);

const data = await parent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { courseService } from "$lib/services/course";

export const ssr = false;

export const load: PageLoad = async ({ url, params }) => {
const lab = await courseService.readLab(params.courseid, url.pathname);
export const load: PageLoad = async ({ url, params, fetch }) => {
const lab = await courseService.readLab(params.courseid, url.pathname, fetch);

const lastSegment = url.pathname.substring(url.pathname.lastIndexOf("/") + 1);
if (lastSegment.startsWith("book")) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/routes/(course-reader)/live/[courseid]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { courseService } from "$lib/services/course";
import type { Course } from "$lib/models/course";
import { currentLo } from "$lib/stores";

export const load: PageLoad = async ({ params }) => {
export const load: PageLoad = async ({ params, fetch }) => {
initFirebase(getKeys().firebase);
const course: Course = await courseService.readCourse(params.courseid);
const course: Course = await courseService.readCourse(params.courseid, fetch);
currentLo.set(course.lo);
return {
course: course,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { courseService } from "$lib/services/course";

export const ssr = false;

export const load: PageLoad = async ({ url, params }) => {
const lo = await courseService.readLo(params.courseid, url.pathname);
export const load: PageLoad = async ({ url, params, fetch }) => {
const lo = await courseService.readLo(params.courseid, url.pathname, fetch);
return {
lo: lo
};
Expand Down
4 changes: 2 additions & 2 deletions app/src/routes/(course-reader)/search/[courseid]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { currentLo } from "$lib/stores";

export const ssr = false;

export const load: PageLoad = async ({ params }) => {
const course: Course = await courseService.readCourse(params.courseid);
export const load: PageLoad = async ({ params, fetch }) => {
const course: Course = await courseService.readCourse(params.courseid, fetch);
currentLo.set(course.lo);
return {
course: course,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { courseService } from "$lib/services/course";

export const ssr = false;

export const load: PageLoad = async ({ url, params }) => {
const lo = await courseService.readLo(params.courseid, url.pathname);
export const load: PageLoad = async ({ url, params, fetch }) => {
const lo = await courseService.readLo(params.courseid, url.pathname, fetch);
return {
lo: lo
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { currentLo } from "$lib/stores";

export const ssr = false;

export const load: PageLoad = async ({ params }) => {
export const load: PageLoad = async ({ params, fetch }) => {
let topicId = params.loid;
let unitId = "";
let unitPos = topicId.indexOf("/unit");
Expand All @@ -18,7 +18,7 @@ export const load: PageLoad = async ({ params }) => {
topicId = topicId.slice(0, unitPos);
}
if (topicId.slice(-1) == "/") topicId = topicId.slice(0, -1);
const topic = await courseService.readTopic(params.courseid, topicId);
const topic = await courseService.readTopic(params.courseid, topicId, fetch);
if (unitPos !== -1) {
const unitLo = topic.lo.los.filter((lo) => lo.id == unitId);
currentLo.set(unitLo[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { courseService } from "$lib/services/course";

export const ssr = false;

export const load: PageLoad = async ({ url, params }) => {
export const load: PageLoad = async ({ url, params, fetch }) => {
let videoId = url.pathname;
let videoStartEnd = url.searchParams.toString();
if (videoStartEnd.endsWith("=")) {
Expand All @@ -14,7 +14,7 @@ export const load: PageLoad = async ({ url, params }) => {
} else {
videoId = `${url.pathname}`;
}
const lo = await courseService.readLo(params.courseid, videoId);
const lo = await courseService.readLo(params.courseid, videoId, fetch);
return {
lo: lo
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { currentLo } from "$lib/stores";

export const ssr = false;

export const load: PageLoad = async ({ params }) => {
const course = await courseService.readCourse(params.courseid);
const los = await courseService.readWall(params.courseid, params.type);
export const load: PageLoad = async ({ params, fetch }) => {
const course = await courseService.readCourse(params.courseid, fetch);
const los = await courseService.readWall(params.courseid, params.type, fetch);
const type = params.type;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
4 changes: 2 additions & 2 deletions app/src/routes/(time)/time/[courseid]/[...userid]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const isStringArray = (test: any[]): boolean => {
return Array.isArray(test) && !test.some((value) => typeof value !== "string");
};

export const load: PageLoad = async ({ parent, params }) => {
export const load: PageLoad = async ({ parent, params, fetch }) => {
const data = await parent();

if (data.session) {
initFirebase(getKeys().firebase);
const course: Course = await courseService.readCourse(params.courseid);
const course: Course = await courseService.readCourse(params.courseid, fetch);
const allLabs = course.walls.get("lab");
const user: UserMetric = await fetchUserById(params.courseid, data.session, allLabs);
const users: Map<string, UserMetric> = await fetchAllUsers(params.courseid, allLabs);
Expand Down
Loading