Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 12x 76x 76x 5x 5x 4x 4x 4x 4x 4x 5x 20x 5x 5x 5x 5x 5x 1x 76x 2x 2x 2x 76x 1x 1x 1x 76x 76x 76x 76x 52x 76x 76x 35x 76x 35x 76x 76x 76x 35x 76x 76x 1x 1x 76x 2x 2x 76x 4x 17x 17x 1x 3x | import React, { useState } from "react";
import { Button, Form } from "react-bootstrap";
import { useForm } from "react-hook-form";
import { toast } from "react-toastify";
import axios from "axios";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import CopyConceptGraphModal from "main/components/Courses/CopyConceptGraphModal";
/**
* Sanitizes a value for use in a downloaded filename: trims surrounding
* whitespace, replaces any run of characters that are not letters or digits
* with a single dash, and strips any leading/trailing dashes left behind.
* Returns "" for null/undefined input.
*
* Examples: "CMPSC 8" -> "CMPSC-8"; " Fall 2026! " -> "Fall-2026"; null -> "".
*
* @param {*} value the value to sanitize (coerced to a string)
* @returns {string} the sanitized, filename-safe string
*/
const sanitizeForFilename = (value) =>
String(value ?? "")
.trim()
.replace(/[^a-zA-Z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
export default function ScaffoldTabComponent({
courseId,
courseName,
term,
school,
testIdPrefix,
}) {
const {
register,
formState: { errors },
handleSubmit,
reset,
} = useForm();
const downloadYaml = async () => {
try {
const response = await axios({
url: "/api/concepts/yaml/download",
method: "GET",
params: { courseId },
});
const blob = new Blob([response.data], { type: "application/x-yaml" });
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
const schoolKey = school?.key ?? school;
const filename = [
"Scaffold",
sanitizeForFilename(courseName),
sanitizeForFilename(term),
sanitizeForFilename(schoolKey),
courseId,
]
.filter((part) => part !== "" && part !== undefined && part !== null)
.join("-");
link.setAttribute("download", `${filename}.yml`);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
} catch (error) {
toast.error(`Error downloading concepts YAML: ${error.message}`);
}
};
const objectToAxiosParamsUpload = (formData) => {
const file = new FormData();
file.append("file", formData.upload[0]);
return {
url: "/api/concepts/yaml/upload",
method: "POST",
data: file,
params: {
courseId,
},
};
};
const uploadMutation = useBackendMutation(objectToAxiosParamsUpload, {
onSuccess: (report) => {
toast(
`Concepts replaced: ${report.conceptsCreated} concepts, ` +
`${report.subconceptsCreated} subconcepts, ${report.edgesCreated} edges, ` +
`${report.practiceProblemsCreated} practice problems. ` +
`Saved scaffold state was cleared for ${report.userStatesCleared} user(s).`,
);
reset();
},
onError: (error) => {
toast.error(
`Error uploading concepts YAML: ${JSON.stringify(error.response.data, null, 2)}`,
);
},
});
const [fromCourseId, setFromCourseId] = useState("");
const [showCopyModal, setShowCopyModal] = useState(false);
const { data: courses } = useBackend(
["/api/courses/list"],
{ method: "GET", url: "/api/courses/list" },
// Stryker disable next-line all : don't test default value of empty list
[],
true,
);
const courseList = (courses ?? []).filter(
(course) => String(course.id) !== String(courseId),
);
const instructorCourses = courseList.filter((c) => c.instructorAccess);
const staffCourses = courseList.filter(
(c) => c.staffAccess && !c.instructorAccess,
);
const studentCourses = courseList.filter(
(c) => c.studentAccess && !c.instructorAccess && !c.staffAccess,
);
const isAdmin = courseList.some((c) => c.adminAccess);
const adminCourses = isAdmin ? courseList : [];
const courseOptionLabel = (course) =>
`${course.courseName} ${course.term}, ${course.school?.displayName ?? course.school}, ${course.instructorEmail}, ${course.id}`;
const objectToAxiosParamsCopyConceptGraph = () => ({
url: "/api/jobs/launch/copyConceptGraph",
method: "POST",
params: { fromCourseId, toCourseId: courseId },
});
const copyConceptGraphMutation = useBackendMutation(
objectToAxiosParamsCopyConceptGraph,
{
onSuccess: (job) => {
toast(
`Copy Concept Graph job (id ${job.id}) launched. You can monitor its progress on the Jobs tab.`,
);
},
onError: (error) => {
toast.error(
`Error launching Copy Concept Graph job: ${error.response?.data?.message ?? error.message}`,
);
},
},
);
const confirmCopyConceptGraph = () => {
setShowCopyModal(false);
copyConceptGraphMutation.mutate();
};
return (
<div className="tabComponent" data-testid={`${testIdPrefix}-scaffoldTab`}>
<h2>Scaffold</h2>
<p>
Download this course's concepts, subconcepts, prerequisite edges,
and practice problems as an editable YAML file, or replace them by
uploading one. See <code>docs/yaml-format.md</code> for the file format.
</p>
<Button
onClick={downloadYaml}
data-testid={`${testIdPrefix}-download-yaml-button`}
>
Download Concepts YAML
</Button>
<Form onSubmit={handleSubmit(uploadMutation.mutate)} className="mt-4">
<Form.Group className="mb-2">
<Form.Label htmlFor="concepts-yaml-upload">
Upload Concepts YAML
</Form.Label>
<Form.Control
data-testid={`${testIdPrefix}-upload-yaml-input`}
id="concepts-yaml-upload"
type="file"
accept=".yaml,.yml"
isInvalid={Boolean(errors.upload)}
{...register("upload", { required: true })}
/>
<Form.Control.Feedback type="invalid">
{errors.upload && "Concepts YAML file is required."}
</Form.Control.Feedback>
<Form.Text muted>
Warning: uploading replaces ALL concepts, subconcepts, edges, and
practice problems for this course, and clears every student's
saved scaffold state.
</Form.Text>
</Form.Group>
<Button
type="submit"
data-testid={`${testIdPrefix}-upload-yaml-button`}
className="mt-3"
>
Upload
</Button>
</Form>
<hr />
<h4>Copy Concept Graph from Another Course</h4>
<p>
Replace this course's entire concept graph with a copy of another
course's concept graph. Select the course to copy from below.
</p>
<Form.Group className="mb-2">
<Form.Label htmlFor={`${testIdPrefix}-copy-concept-graph-from-course`}>
From Course
</Form.Label>
<Form.Select
id={`${testIdPrefix}-copy-concept-graph-from-course`}
data-testid={`${testIdPrefix}-copy-concept-graph-from-course-select`}
value={fromCourseId}
onChange={(e) => setFromCourseId(e.target.value)}
>
<option value="">Select a course...</option>
{instructorCourses.length > 0 && (
<optgroup label="Instructor">
{instructorCourses.map((c) => (
<option key={c.id} value={c.id}>
{courseOptionLabel(c)}
</option>
))}
</optgroup>
)}
{staffCourses.length > 0 && (
<optgroup label="Staff">
{staffCourses.map((c) => (
<option key={c.id} value={c.id}>
{courseOptionLabel(c)}
</option>
))}
</optgroup>
)}
{studentCourses.length > 0 && (
<optgroup label="Student">
{studentCourses.map((c) => (
<option key={c.id} value={c.id}>
{courseOptionLabel(c)}
</option>
))}
</optgroup>
)}
{adminCourses.length > 0 && (
<optgroup label="Admin">
{adminCourses.map((c) => (
<option key={c.id} value={c.id}>
{courseOptionLabel(c)}
</option>
))}
</optgroup>
)}
</Form.Select>
</Form.Group>
<Button
variant="danger"
disabled={!fromCourseId}
onClick={() => setShowCopyModal(true)}
data-testid={`${testIdPrefix}-copy-concept-graph-button`}
>
Copy Concept Graph
</Button>
<CopyConceptGraphModal
showModal={showCopyModal}
toggleShowModal={setShowCopyModal}
onConfirm={confirmCopyConceptGraph}
testId={`${testIdPrefix}-copyConceptGraphModal`}
/>
</div>
);
}
|