|
1 | | -import React from 'react'; |
2 | | -import {getConfig} from '@edx/frontend-platform'; |
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { getConfig } from "@edx/frontend-platform"; |
| 3 | +import { getAuthenticatedHttpClient } from "@edx/frontend-platform/auth"; |
| 4 | + |
| 5 | +const CourseList = ({ courseListData }) => { |
| 6 | + const [archivedCourses, setArchivedCourses] = useState(new Set()); |
3 | 7 |
|
4 | | -const CourseList = ({courseListData}) => { |
5 | 8 | // Extract the "visibleList" |
6 | 9 | const courses = courseListData.visibleList; |
7 | | - // Render a list of course names |
| 10 | + |
| 11 | + // Log the full course data structure for debugging |
| 12 | + console.log("DEBUG: Full courseListData structure:", courseListData); |
| 13 | + console.log("DEBUG: First course data object:", courses?.[0]); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + const fetchArchivedCourses = async () => { |
| 17 | + try { |
| 18 | + const client = getAuthenticatedHttpClient(); |
| 19 | + const lmsBaseUrl = getConfig().LMS_BASE_URL; |
| 20 | + console.log( |
| 21 | + "DEBUG: Fetching archived courses from:", |
| 22 | + `${lmsBaseUrl}/sample-plugin/api/v1/course-archive-status/`, |
| 23 | + ); |
| 24 | + |
| 25 | + const response = await client.get( |
| 26 | + `${lmsBaseUrl}/sample-plugin/api/v1/course-archive-status/`, |
| 27 | + { |
| 28 | + params: { is_archived: true }, |
| 29 | + }, |
| 30 | + ); |
| 31 | + |
| 32 | + console.log("DEBUG: Archived courses API response:", response.data); |
| 33 | + |
| 34 | + const archivedCourseIds = new Set( |
| 35 | + response.data.results.map((item) => item.course_id), |
| 36 | + ); |
| 37 | + |
| 38 | + console.log( |
| 39 | + "DEBUG: Archived course IDs from API:", |
| 40 | + Array.from(archivedCourseIds), |
| 41 | + ); |
| 42 | + setArchivedCourses(archivedCourseIds); |
| 43 | + } catch (error) { |
| 44 | + console.error("Failed to fetch archived courses:", error); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + fetchArchivedCourses(); |
| 49 | + }, []); |
| 50 | + |
| 51 | + // Log course IDs we're trying to match against |
| 52 | + console.log("DEBUG: Course IDs from course data:"); |
| 53 | + courses.forEach((courseData, index) => { |
| 54 | + console.log(`Course ${index}:`, { |
| 55 | + cardId: courseData.cardId, |
| 56 | + "courseRun.courseId": courseData.courseRun?.courseId, |
| 57 | + "courseRun object keys": Object.keys(courseData.courseRun || {}), |
| 58 | + "full courseRun object": courseData.courseRun, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + console.log( |
| 63 | + "DEBUG: Archived course IDs to match:", |
| 64 | + Array.from(archivedCourses), |
| 65 | + ); |
| 66 | + |
| 67 | + // Separate courses into active and archived |
| 68 | + const activeCourses = courses.filter((courseData) => { |
| 69 | + const courseId = courseData.courseRun?.courseId; |
| 70 | + const isArchived = archivedCourses.has(courseId); |
| 71 | + console.log( |
| 72 | + `DEBUG: Course "${courseData.course?.courseName}" (ID: ${courseId}) - archived: ${isArchived}`, |
| 73 | + ); |
| 74 | + return !isArchived; |
| 75 | + }); |
| 76 | + |
| 77 | + const archivedCoursesList = courses.filter((courseData) => { |
| 78 | + const courseId = courseData.courseRun?.courseId; |
| 79 | + return archivedCourses.has(courseId); |
| 80 | + }); |
| 81 | + |
| 82 | + console.log("DEBUG: Active courses count:", activeCourses.length); |
| 83 | + console.log("DEBUG: Archived courses count:", archivedCoursesList.length); |
| 84 | + |
| 85 | + const renderCourse = (courseData) => ( |
| 86 | + <div key={courseData.cardId}> |
| 87 | + <h2> |
| 88 | + {courseData.course.courseName} |
| 89 | + <img |
| 90 | + src={getConfig().LMS_BASE_URL + courseData.course.bannerImgSrc} |
| 91 | + alt={courseData.course.courseName} |
| 92 | + ></img> |
| 93 | + </h2> |
| 94 | + </div> |
| 95 | + ); |
| 96 | + |
8 | 97 | return ( |
9 | 98 | <div> |
10 | | - {courses.map(courseData => ( |
11 | | - <> |
12 | | - <h2> |
13 | | - {courseData.course.courseName} |
14 | | - <img src={getConfig().LMS_BASE_URL + courseData.course.bannerImgSrc}></img> |
15 | | - </h2> |
16 | | - {console.log(courseData.cardId)} |
17 | | - </> |
18 | | - ))} |
| 99 | + {activeCourses.map(renderCourse)} |
| 100 | + |
| 101 | + {archivedCoursesList.length > 0 && ( |
| 102 | + <div> |
| 103 | + <h2>Archived Courses</h2> |
| 104 | + {archivedCoursesList.map(renderCourse)} |
| 105 | + </div> |
| 106 | + )} |
19 | 107 | </div> |
20 | | - ) |
21 | | -} |
| 108 | + ); |
| 109 | +}; |
22 | 110 |
|
23 | 111 | export default CourseList; |
0 commit comments