-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.jsx
More file actions
302 lines (273 loc) · 9.28 KB
/
plugin.jsx
File metadata and controls
302 lines (273 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import React, { useState, useEffect } from "react";
import { getConfig } from "@edx/frontend-platform";
import { getAuthenticatedHttpClient } from "@edx/frontend-platform/auth";
import {
Card,
Container,
Row,
Col,
Badge,
Collapsible,
Button,
Spinner,
Dropdown,
IconButton,
Icon,
} from "@openedx/paragon";
import { Archive, Unarchive, MoreVert } from "@openedx/paragon/icons";
const CourseList = ({ courseListData }) => {
const [archivedCourses, setArchivedCourses] = useState(new Set());
const [loadingStates, setLoadingStates] = useState(new Map());
// Safety check for courseListData
if (!courseListData || !courseListData.visibleList) {
console.log("DEBUG: courseListData not available yet");
return <div>Loading courses...</div>;
}
// Extract the "visibleList"
const courses = courseListData.visibleList;
// Log the full course data structure for debugging
console.log("DEBUG: Full courseListData structure:", courseListData);
console.log("DEBUG: First course data object:", courses?.[0]);
useEffect(() => {
const fetchArchivedCourses = async () => {
try {
const client = getAuthenticatedHttpClient();
const lmsBaseUrl = getConfig().LMS_BASE_URL;
console.log(
"DEBUG: Fetching archived courses from:",
`${lmsBaseUrl}/sample-plugin/api/v1/course-archive-status/`,
);
const response = await client.get(
`${lmsBaseUrl}/sample-plugin/api/v1/course-archive-status/`,
{
params: { is_archived: true },
},
);
console.log("DEBUG: Archived courses API response:", response.data);
const archivedCourseIds = new Set(
response.data.results.map((item) => item.course_id),
);
console.log(
"DEBUG: Archived course IDs from API:",
Array.from(archivedCourseIds),
);
setArchivedCourses(archivedCourseIds);
} catch (error) {
console.error("Failed to fetch archived courses:", error);
}
};
fetchArchivedCourses();
}, []);
// Log course IDs we're trying to match against
console.log("DEBUG: Course IDs from course data:");
courses.forEach((courseData, index) => {
console.log(`Course ${index}:`, {
cardId: courseData.cardId,
"courseRun.courseId": courseData.courseRun?.courseId,
"courseRun object keys": Object.keys(courseData.courseRun || {}),
"full courseRun object": courseData.courseRun,
});
});
console.log(
"DEBUG: Archived course IDs to match:",
Array.from(archivedCourses),
);
// Separate courses into active and archived
const activeCourses = courses.filter((courseData) => {
const courseId = courseData.courseRun?.courseId;
const isArchived = archivedCourses.has(courseId);
console.log(
`DEBUG: Course "${courseData.course?.courseName}" (ID: ${courseId}) - archived: ${isArchived}`,
);
return !isArchived;
});
const archivedCoursesList = courses.filter((courseData) => {
const courseId = courseData.courseRun?.courseId;
return archivedCourses.has(courseId);
});
console.log("DEBUG: Active courses count:", activeCourses.length);
console.log("DEBUG: Archived courses count:", archivedCoursesList.length);
const handleArchiveToggle = async (courseId, isCurrentlyArchived) => {
console.log(
`DEBUG: Toggling archive for course ${courseId}, currently archived: ${isCurrentlyArchived}`,
);
setLoadingStates((prev) => new Map(prev).set(courseId, true));
try {
const client = getAuthenticatedHttpClient();
const lmsBaseUrl = getConfig().LMS_BASE_URL;
const url = `${lmsBaseUrl}/sample-plugin/api/v1/course-archive-status/`;
if (isCurrentlyArchived) {
// Unarchive: Find existing record and update it
const listResponse = await client.get(url, {
params: { course_id: courseId },
});
if (listResponse.data.results.length > 0) {
const existingRecord = listResponse.data.results[0];
await client.patch(`${url}${existingRecord.id}/`, {
course_id: courseId,
is_archived: false,
});
}
// Update local state
setArchivedCourses((prev) => {
const newSet = new Set(prev);
newSet.delete(courseId);
return newSet;
});
} else {
// Archive: Check if record exists first, then create or update
const listResponse = await client.get(url, {
params: { course_id: courseId },
});
if (listResponse.data.results.length > 0) {
// Update existing record
const existingRecord = listResponse.data.results[0];
await client.patch(`${url}${existingRecord.id}/`, {
is_archived: true,
});
} else {
// Create new record
console.log(
`DEBUG: Creating new archive record for course ${courseId}`,
);
const createResponse = await client.post(url, {
course_id: courseId,
is_archived: true,
});
console.log(`DEBUG: Create response:`, createResponse.data);
}
// Update local state
console.log(`DEBUG: Adding course ${courseId} to archived set`);
setArchivedCourses((prev) => {
const newSet = new Set(prev).add(courseId);
console.log(`DEBUG: New archived courses set:`, Array.from(newSet));
return newSet;
});
}
console.log(
`DEBUG: Successfully ${isCurrentlyArchived ? "unarchived" : "archived"} course ${courseId}`,
);
} catch (error) {
console.error(
`Failed to ${isCurrentlyArchived ? "unarchive" : "archive"} course:`,
error,
);
console.error("Error details:", {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
message: error.message,
});
// Could add toast notification here
} finally {
setLoadingStates((prev) => {
const newMap = new Map(prev);
newMap.delete(courseId);
return newMap;
});
}
};
const renderCourse = (courseData, isArchived = false) => {
const courseId = courseData.courseRun?.courseId;
const isLoading = loadingStates.get(courseId);
return (
<Col
key={courseData.cardId}
xs={12}
sm={6}
md={4}
lg={3}
className="mb-4"
>
<Card>
<a href={courseData.courseRun?.homeUrl || '#'}>
<Card.ImageCap
src={getConfig().LMS_BASE_URL + courseData.course.bannerImgSrc}
alt={courseData.course.courseName}
/>
</a>
<Card.Header
title={
<a href={courseData.courseRun?.homeUrl || '#'}>
{courseData.course.courseName}
</a>
}
subtitle={courseData.course.courseNumber}
actions={
<>
{isArchived && <Badge variant="secondary" className="me-2">Archived</Badge>}
<Dropdown>
<Dropdown.Toggle
id={`course-menu-${courseData.cardId}`}
as={IconButton}
src={MoreVert}
iconAs={Icon}
variant="primary"
aria-label="More actions"
/>
<Dropdown.Menu>
{courseData.course.socialShareUrl && (
<Dropdown.Item
href={courseData.course.socialShareUrl}
target="_blank"
rel="noopener noreferrer"
>
View Course About Page
</Dropdown.Item>
)}
</Dropdown.Menu>
</Dropdown>
</>
}
/>
<Card.Section>
{courseData.course.shortDescription && (
<p className="text-muted small">
{courseData.course.shortDescription}
</p>
)}
</Card.Section>
<Card.Footer>
<Button
variant={isArchived ? "outline-primary" : "outline-secondary"}
size="sm"
disabled={isLoading}
onClick={() => handleArchiveToggle(courseId, isArchived)}
iconBefore={
isLoading ? Spinner : isArchived ? Unarchive : Archive
}
>
{isLoading
? "Processing..."
: isArchived
? "Unarchive"
: "Archive"}
</Button>
</Card.Footer>
</Card>
</Col>
);
};
return (
<Container fluid>
<Row>
{activeCourses.map((courseData) => renderCourse(courseData, false))}
</Row>
{archivedCoursesList.length > 0 && (
<div className="mt-5">
<Collapsible
title={`Archived Courses (${archivedCoursesList.length})`}
defaultOpen={false}
>
<Row>
{archivedCoursesList.map((courseData) =>
renderCourse(courseData, true),
)}
</Row>
</Collapsible>
</div>
)}
</Container>
);
};
export default CourseList;