Skip to content

Commit

Permalink
feat: optimise course utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jouwdan committed Jul 27, 2023
1 parent 79cd7c8 commit a842983
Showing 1 changed file with 19 additions and 43 deletions.
62 changes: 19 additions & 43 deletions packages/tutors-reader-lib/src/utils/course-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,59 @@ export interface CourseSummary {
}

export function isValidCourseName(course: string) {
let isValid = true;
if (course.length > 27 && course[24] == "-" && course[25] == "-") {
isValid = false;
} else {
if (course.startsWith("main--") || course.startsWith("master--")) {
isValid = false;
}
if (course.startsWith("deploy-preview")) {
isValid = false;
}
}
return isValid;
const invalidPatterns = /^(main--|master--|deploy-preview--)|-{2}/;
return !invalidPatterns.test(course) && course.length <= 27;
}

export async function getCourseSummary(courseId: string): Promise<CourseSummary> {
const response = await fetch(`https://${courseId}.netlify.app/tutors.json`);
const lo = await response.json();
const courseTime: CourseSummary = {
const courseUrl = `${courseId}.netlify.app`;

return {
title: lo.title,
img: lo.img.replace("{{COURSEURL}}", `${courseId}.netlify.app`),
img: lo.img.replace("{{COURSEURL}}", courseUrl),
icon: lo.properties?.icon,
route: `https://reader.tutors.dev/course/${courseId}.netlify.app`,
route: `https://reader.tutors.dev/course/${courseUrl}`,
visits: 0,
count: 0,
isPrivate: lo.properties?.private,
currentLo: null,
studentIds: new Set<string>()
};
return courseTime;
}

export function updateLo(root: string, course: Course, currentLo: Lo) {
const lo = {
icon: {},
icon:
currentLo.type === "course" ? currentLo.icon : currentLo.frontMatter?.icon ? { type: currentLo.frontMatter.icon["type"], color: currentLo.frontMatter.icon["color"] } : {},
img: currentLo.img,
title: currentLo.title,
courseTitle: course.lo.title,
subRoute: currentLo.route,
isPrivate: 0,
isPrivate: course.lo.properties?.private ? course.lo.properties.private : 0,
tutorsTimeId: getTutorsTimeId(course)
};
if (currentLo.type === "course" && currentLo.icon) {
lo.icon = currentLo.icon;
} else {
if (currentLo?.frontMatter?.icon) {
lo.icon = {
type: currentLo.frontMatter.icon["type"]
};
if (currentLo.frontMatter.icon["color"]) {
lo.icon.color = currentLo.frontMatter.icon["color"];
}
}
}
if (course.lo.properties?.private) {
lo.isPrivate = course.lo.properties?.private as unknown as number;
}

writeObj(`${root}/lo`, lo);
}

function generateTutorsTimeId() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

function getTutorsTimeId(course: Course) {
if (course.authLevel > 0) {
return window.localStorage.id;
} else {
if (window.localStorage.tutorsTimeId != null) {
return window.localStorage.tutorsTimeId;
} else {
window.localStorage.tutorsTimeId = generateTutorsTimeId();
return window.localStorage.tutorsTimeId;
}
}

if (!window.localStorage.tutorsTimeId) {
window.localStorage.tutorsTimeId = generateTutorsTimeId();
}

return window.localStorage.tutorsTimeId;
}

0 comments on commit a842983

Please sign in to comment.