forked from openedx/frontend-app-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressTab.jsx
78 lines (69 loc) · 3.34 KB
/
ProgressTab.jsx
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
import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { breakpoints, useWindowSize } from '@edx/paragon';
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform';
import CertificateStatus from './certificate-status/CertificateStatus';
import CourseCompletion from './course-completion/CourseCompletion';
import CourseGrade from './grades/course-grade/CourseGrade';
import DetailedGrades from './grades/detailed-grades/DetailedGrades';
import GradeSummary from './grades/grade-summary/GradeSummary';
import ProgressHeader from './ProgressHeader';
import RelatedLinks from './related-links/RelatedLinks';
import { useModel } from '../../generic/model-store';
const ProgressTab = () => {
const {
courseId,
} = useSelector(state => state.courseHome);
const {
gradesFeatureIsFullyLocked,
} = useModel('progress', courseId);
const applyLockedOverlay = gradesFeatureIsFullyLocked ? 'locked-overlay' : '';
const [visibility, setVisibility] = useState({});
const [isLoaded, setIsLoaded] = useState(false);
// If the visibility is undefined before loading is complete, then hide the component,
// however if it's still false after loading is complete that means the visibility just
// isn't configured, in which case default to being visible.
const isVisible = (component) => visibility?.[`show${component}`] ?? isLoaded;
useEffect(async () => {
const authenticatedUser = getAuthenticatedUser();
const url = new URL(`${getConfig().LMS_BASE_URL}/api/courses/v2/blocks/`);
url.searchParams.append('course_id', courseId);
url.searchParams.append('username', authenticatedUser ? authenticatedUser.username : '');
url.searchParams.append('requested_fields', 'other_course_settings');
const { data } = await getAuthenticatedHttpClient().get(url.href, {});
setVisibility(data.blocks[data.root]?.other_course_settings?.progressPage ?? {});
setIsLoaded(true);
}, [courseId]);
const windowWidth = useWindowSize().width;
if (windowWidth === undefined) {
// Bail because we don't want to load <CertificateStatus/> twice, emitting 'visited' events both times.
// This is a hacky solution, since the user can resize the screen and still get two visited events.
// But I'm leaving a larger refactor as an exercise to a future reader.
return null;
}
const wideScreen = windowWidth >= breakpoints.large.minWidth;
return (
<>
<ProgressHeader />
<div className="row w-100 m-0">
{/* Main body */}
<div className="col-12 col-md-8 p-0">
<CourseCompletion />
{!wideScreen && isVisible('CertificateStatus') && <CertificateStatus />}
{isVisible('Grades') && <CourseGrade />}
<div className={`grades my-4 p-4 rounded raised-card ${applyLockedOverlay}`} aria-hidden={gradesFeatureIsFullyLocked}>
{isVisible('GradeSummary') && <GradeSummary />}
{isVisible('GradeSummary') && <DetailedGrades />}
</div>
</div>
{/* Side panel */}
<div className="col-12 col-md-4 p-0 px-md-4">
{wideScreen && isVisible('CertificateStatus') && <CertificateStatus />}
{isVisible('RelatedLinks') && <RelatedLinks />}
</div>
</div>
</>
);
};
export default ProgressTab;