-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCourse.jsx
More file actions
150 lines (138 loc) · 6.44 KB
/
Course.jsx
File metadata and controls
150 lines (138 loc) · 6.44 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
import './styles.scss'
import 'vite/modulepreload-polyfill'
import render from '../src/render.jsx';
import Base from '../src/components/Base.jsx'
import FilterListIcon from '@mui/icons-material/FilterList';
import DescriptionIcon from '@mui/icons-material/Description';
import BallotIcon from '@mui/icons-material/Ballot';
import { useState } from 'react';
import { Box, Grid, Button, Dialog } from '@mui/material'
import LessonForm from './components/LessonForm.jsx';
import QuizForm from './components/QuizForm.jsx';
import ContentTable from './components/ContentTable.jsx';
import { getCookie } from '../src/utils.js';
function Course() {
const platformBaseUrl = localStorage.getItem('platformBaseUrl');
const [dialogOpen, setDialogOpen] = useState(false)
const [dialogContent, setDialogContent] = useState(null)
const [lessonCache, setLessonCache] = useState("")
const [contentLoaded, setContentLoaded] = useState(false)
const userRole = localStorage.getItem('userRole');
const apiBaseUrl = localStorage.getItem('apiBaseUrl');
const organizationId = localStorage.getItem('activeOrganizationId');
const resetDialog = () => {
setDialogOpen(false);
setContentLoaded(false);
}
const handleClose = (event, reason) => {
if (reason !== "backdropClick" && reason !== "escapeKeyDown") {
setDialogOpen(false);
}
}
const getContent = async (contentId, ) => {
console.log("Fetching content with ID:", contentId);
const response = await fetch(`${apiBaseUrl}/organizations/${organizationId}/courses/${course_id}/contents/${contentId}/`, {
method: 'GET',
headers: {
'X-CSRFToken': getCookie('csrftoken')
},
});
if (response.ok) {
const data = await response.json();
console.log("Content data:", data);
return data;
} else {
console.error('Error fetching content:', response.statusText);
return null;
}
}
const translateOptions = (options) => {
return options.map((opt) => ({
optionText: opt.text,
isCorrect: opt.is_correct,
editMode: false
}));
}
const translateQuestions = (questions) => {
return questions.map((q) => ({
text: q.text,
options: translateOptions(q.answers),
}));
}
const tableEventHandler = async (event) => {
console.log("Event triggered from ContentTable", event);
if (event.type === 'content_loaded') {
setContentLoaded(true);
}
if (event.type === 'content_clicked') {
const content = await getContent(event.content_id);
if (content.type == 'lesson') {
console.log("Opening lesson editor for content:", content);
setDialogOpen(true);
setDialogContent(<LessonForm
header="Update Lesson"
initialTitle={content.lesson.title}
initialContent={content.lesson.content}
onContentChange={setLessonCache}
cancelCallback={() => {setLessonCache(""); setDialogOpen(false);}}
successCallback={resetDialog}
courseId={course_id}
lessonId={content.lesson.id}
initialWaitingPeriod={content.waiting_period}
contentId={content.id} />);
} else if (content.type == 'quiz') {
console.log("Opening quiz editor for content:", content);
setDialogOpen(true);
setDialogContent(<QuizForm
cancelCallback={() => setDialogOpen(false)}
successCallback={resetDialog}
courseId={course_id}
quizId={content.quiz.id}
contentId={content.id}
initialTitle={content.quiz.title}
initialRequiredScore={content.quiz.required_score}
initialQuestions={translateQuestions(content.quiz.questions)}
initialWaitingPeriod={content.waiting_period}
/>);
}
}
}
return (
<Base
breadCrumbList={[
{label: 'Course Management', href: platformBaseUrl + '/courses', index: 0},
{label: course_title, href: '#', index: 1}
]}
bottomDrawerParams={{
icon: <FilterListIcon />,
children: <div>Filter Options Here</div>,
}}
showOrganizationSwitcher={false}
>
<Grid size={{xs: 12, md: 9}} py={2} pl={2}>
<Box p={2} sx={{ border: '1px solid', borderColor: 'grey.300', borderRadius: 1, minHeight: 300 }}>
{userRole !== 'viewer' && <><Button variant="contained" startIcon={<DescriptionIcon />} sx={{ marginBottom: 2 }} onClick={() => {
setDialogContent(<LessonForm
header="New Lesson"
initialContent={lessonCache}
onContentChange={setLessonCache}
cancelCallback={() => setDialogOpen(false)}
successCallback={resetDialog}
courseId={course_id} />);
setDialogOpen(true);}}>Add a Lesson</Button>
<Button variant="contained" startIcon={<BallotIcon />} sx={{ marginBottom: 2, marginLeft: 1 }} onClick={() => {
setDialogContent(<QuizForm
cancelCallback={() => setDialogOpen(false)}
successCallback={resetDialog}
courseId={course_id} />);
setDialogOpen(true);}}>Add a Quiz</Button></> }
<ContentTable courseId={course_id} loaded={contentLoaded} eventHandler={(event) => tableEventHandler(event)} />
</Box>
</Grid>
<Dialog open={dialogOpen} onClose={handleClose} fullWidth maxWidth="lg" sx={{ xs: { width: '100%' }, md: { width: '80%' }, lg: { maxWidth: '70%' } }}>
{dialogContent}
</Dialog>
</Base>
)
}
render({children: <Course />});