Skip to content

Commit 2247219

Browse files
fix LessonCard render crash and add frontend ObjectId guards
1 parent 412399d commit 2247219

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/actions/bmdashboard/lessonsAction.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ export const setLessons = payload => {
4545
};
4646
};
4747

48+
// A valid MongoDB ObjectId is a 24-character hex string. Lesson author/project
49+
// fields sometimes hold display names (e.g. "James", "project 1") instead of
50+
// real references; skip those so we don't fire doomed 404/500/400 requests.
51+
const isObjectId = id => typeof id === 'string' && /^[0-9a-fA-F]{24}$/.test(id);
52+
4853
export const fetchBMLessons = () => {
4954
return async dispatch => {
5055
try {
5156
const response = await axios.get(ENDPOINTS.BM_LESSONS);
5257
const lessons = response.data;
53-
const authorIds = [...new Set(lessons.map(lesson => lesson.author))];
54-
const projectIds = [...new Set(lessons.map(lesson => lesson.relatedProject))];
58+
const authorIds = [...new Set(lessons.map(lesson => lesson.author).filter(isObjectId))];
59+
const projectIds = [
60+
...new Set(lessons.map(lesson => lesson.relatedProject).filter(isObjectId)),
61+
];
5562

5663
// Keep the more robust approach from honglin-lesson-list-buttons branch
5764
const [authorProfiles, projectDetails] = await Promise.all([
@@ -129,10 +136,11 @@ export const fetchSingleBMLesson = lessonId => {
129136
const response = await axios.get(url);
130137
const lesson = response.data;
131138

132-
// Fetch user profile and project details concurrently
139+
// Fetch user profile and project details concurrently, but only when the
140+
// stored values are real ObjectIds (not display names like "project 1").
133141
const [projectDetails, userProfile] = await Promise.all([
134-
dispatch(fetchProjectById(lesson.relatedProject)),
135-
dispatch(getUserProfile(lesson.author)),
142+
isObjectId(lesson.relatedProject) ? dispatch(fetchProjectById(lesson.relatedProject)) : null,
143+
isObjectId(lesson.author) ? dispatch(getUserProfile(lesson.author)) : null,
136144
]);
137145

138146
// Update the lesson with author and project details

src/components/BMDashboard/LessonList/LessonCard.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ function LessonCard({ filteredLessons, onEditLessonSummary, onDeliteLessonCard,
146146
</button>
147147
</>
148148
) : (
149-
<span>
150-
{parse(
149+
<span>
150+
{ReactHtmlParser(
151151
(lesson?.content || '').length > maxSummaryLength
152152
? `${lesson.content.slice(0, maxSummaryLength)}...`
153-
: lesson?.content || ''
153+
: lesson?.content || '',
154154
)}
155155
</span>
156156
)}

0 commit comments

Comments
 (0)