-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.jsx
More file actions
204 lines (189 loc) · 6.03 KB
/
plugin.jsx
File metadata and controls
204 lines (189 loc) · 6.03 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
import React, { useState } 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 }) => {
// Seed the archived-course set from `courseRun.isArchivedByLearner`, which the
// backend plugin's filter pipeline injects into each courseRun in the Learner
// Home /init API response. This avoids a separate GET to course-archive-status
// on every dashboard load. Local toggles below keep this set in sync without
// a refetch.
const [archivedCourses, setArchivedCourses] = useState(() => {
const initial = new Set();
(courseListData?.visibleList || []).forEach((courseData) => {
if (courseData.courseRun?.isArchivedByLearner) {
initial.add(courseData.courseRun.courseId);
}
});
return initial;
});
const [loadingStates, setLoadingStates] = useState(new Map());
if (!courseListData || !courseListData.visibleList) {
return <div>Loading courses...</div>;
}
const courses = courseListData.visibleList;
const activeCourses = courses.filter(
(courseData) => !archivedCourses.has(courseData.courseRun?.courseId),
);
const archivedCoursesList = courses.filter((courseData) =>
archivedCourses.has(courseData.courseRun?.courseId),
);
const handleArchiveToggle = async (courseId, 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/`;
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}/`, {
is_archived: !isCurrentlyArchived,
});
} else {
await client.post(url, {
course_id: courseId,
is_archived: !isCurrentlyArchived,
});
}
setArchivedCourses((prev) => {
const newSet = new Set(prev);
if (isCurrentlyArchived) {
newSet.delete(courseId);
} else {
newSet.add(courseId);
}
return newSet;
});
} catch (error) {
console.error(
`Failed to ${isCurrentlyArchived ? "unarchive" : "archive"} course:`,
error,
);
} 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;