Skip to content

Commit

Permalink
Merge pull request #97 from Pearson-Advance/vue/PADV-1412-and
Browse files Browse the repository at this point in the history
fix: obtain courseInfo in CoursesDetailsPage.
  • Loading branch information
anfbermudezme authored Jun 25, 2024
2 parents 2fcf9ca + 0c70c04 commit 34943ad
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/features/Courses/CoursesDetailPage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useState, useEffect, useRef } from 'react';
import React, {
useState, useEffect, useRef, useMemo,
} from 'react';
import { Link, useParams, useHistory } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';

Expand All @@ -25,18 +27,27 @@ const CoursesDetailPage = () => {
const [currentPage, setCurrentPage] = useState(initialPage);
const [isOpenModal, openModal, closeModal] = useToggle(false);

const defaultCourseInfo = {
const defaultCourseInfo = useMemo(() => ({
numberOfStudents: '-',
numberOfPendingStudents: '-',
masterCourseId: '-',
};
}), []);

const courseInfo = useSelector((state) => state.courses.table.data)
.find((course) => course?.masterCourseName === courseId) || defaultCourseInfo;
const lastCourseInfoRef = useRef(courseInfo);

useEffect(() => {
if (courseInfo !== defaultCourseInfo) {
lastCourseInfoRef.current = courseInfo;
}
}, [courseInfo, defaultCourseInfo]);

const nextCourseInfo = lastCourseInfoRef.current;
const institution = useSelector((state) => state.main.selectedInstitution);
const classes = useSelector((state) => state.classes.table);
const totalStudents = courseInfo.numberOfStudents + courseInfo.numberOfPendingStudents;
const courseDetailsLink = `${getConfig().LEARNING_MICROFRONTEND_URL}/course/${courseInfo.masterCourseId}/home`;
const totalStudents = nextCourseInfo.numberOfStudents + nextCourseInfo.numberOfPendingStudents;
const courseDetailsLink = `${getConfig().LEARNING_MICROFRONTEND_URL}/course/${nextCourseInfo.masterCourseId}/home`;

const handlePagination = (targetPage) => {
setCurrentPage(targetPage);
Expand Down Expand Up @@ -89,7 +100,7 @@ const CoursesDetailPage = () => {
<div className="d-flex flex-column align-items-center">
<p className="title">Students enrolled</p>
<span className="value">
{courseInfo.numberOfStudents}
{nextCourseInfo.numberOfStudents}
{' / '}
{totalStudents}
</span>
Expand Down Expand Up @@ -118,7 +129,7 @@ const CoursesDetailPage = () => {
<AddClass
isOpen={isOpenModal}
onClose={closeModal}
courseInfo={courseInfo}
courseInfo={nextCourseInfo}
/>
</div>
<CourseDetailTable count={classes.count} data={classes.data} />
Expand Down

0 comments on commit 34943ad

Please sign in to comment.