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

Refactor getNextPrevTopic function. #320

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 9 additions & 42 deletions src/react/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
StudentDetailsContextProvider,
} from "./studentDetails";
import { ChallengesContext, ChallengesContextProvider } from "./challenges";
import { getFlatTopics } from "../../utils/course";

export const SCORMPlayer: React.FC<{
uuid: string;
Expand Down Expand Up @@ -1619,64 +1620,30 @@
[progressMap]
);

// Refactor. getNextPrevTopic. this should be refactored, since lessons are not flat structure any more
// Also this should return both topic and lesson
// Refactored. Added getFlatTopics function with return flatted topics from lessons
// https://github.com/EscolaLMS/sdk/issues/255

const getNextPrevTopic = useCallback(
(topicId: number, next: boolean = true) => {
const lesson: API.Lesson | undefined = program.value?.lessons.find(
(lesson) => !!lesson.topics?.find((topic) => topicId === topic.id)
);

if (program.value === undefined) {
return null;
}

if (!lesson) {
return null;
}
const flatTopics = getFlatTopics(program.value.lessons);

Check warning on line 1633 in src/react/context/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/react/context/index.tsx#L1633

Added line #L1633 was not covered by tests
const currentTopicIndex = flatTopics?.findIndex((fTopic) => fTopic.id === topicId);

const currentLessonIndex = program.value.lessons.findIndex(
(fLesson) => lesson.id === fLesson.id
);
if (currentLessonIndex === undefined) {
if (!currentTopicIndex || currentTopicIndex === -1) {
return null;
}

const currentTopicIndex = (
program.value && program.value.lessons ? program.value.lessons : []
)[currentLessonIndex].topics?.findIndex(
(topic) => Number(topic.id) === Number(topicId)
);

if (currentTopicIndex === undefined) {
return null;
}

const topics = program.value.lessons[currentLessonIndex].topics;

if (next) {
if (Array.isArray(topics) && topics[currentTopicIndex + 1]) {
return topics[currentTopicIndex + 1] || null;
} else {
if (program.value.lessons[currentLessonIndex + 1]) {
const newLesson = program.value.lessons[currentLessonIndex + 1];
return (newLesson.topics && newLesson.topics[0]) || null;
}
if (Array.isArray(flatTopics) && flatTopics[currentTopicIndex + 1]) {
return flatTopics[currentTopicIndex + 1] || null;
}
} else {
if (Array.isArray(topics) && topics[currentTopicIndex - 1]) {
return topics[currentTopicIndex - 1] || null;
} else {
if (program.value.lessons[currentLessonIndex - 1]) {
const newLesson = program.value.lessons[currentLessonIndex - 1];
return (
(newLesson.topics &&
newLesson.topics[newLesson.topics.length - 1]) ||
null
);
}
if (Array.isArray(flatTopics) && flatTopics[currentTopicIndex - 1]) {
return flatTopics[currentTopicIndex - 1] || null;
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/utils/course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { API } from "..";

export function getFlatTopics(lessons: API.Lesson[]): API.Topic[] {
return lessons.reduce<API.Topic[]>(
(acc, l) => [

Check warning on line 5 in src/utils/course.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/course.ts#L4-L5

Added lines #L4 - L5 were not covered by tests
...acc,
...(l?.topics ?? []),
...getFlatTopics(l?.lessons ?? []),
],
[]
);
}
Loading