@@ -3,21 +3,59 @@ import { useParams, Link } from 'react-router-dom';
33import { Footer } from '../footer' ;
44import PropTypes from 'prop-types' ;
55import { Katex } from '../utils/katexComponent' ;
6- import { getCoursePk } from '../utils/utils' ;
6+ import { getCoursePk , toggleVisibility } from '../utils/utils' ;
7+ import { useState } from 'react' ;
78
89
9- export const Dashboard = ( { isSuperUser, isFaculty} ) => {
10+ export const Dashboard = ( { isSuperUser, isFaculty, initialVisibleSims } ) => {
1011
1112 let { courseId } = useParams ( ) ;
1213 const coursePk = getCoursePk ( ) ;
14+ const [ visibleSimulations , setVisibleSimulations ] = useState (
15+ initialVisibleSims || [ ]
16+ ) ;
17+
18+ const handleToggle = async ( simId ) => {
19+ const result = await toggleVisibility ( coursePk , simId ) ;
20+ if ( result . status === 'success' ) {
21+ setVisibleSimulations ( prev => {
22+ if ( result . is_visible ) {
23+ return [ ...prev , simId ] ;
24+ } else {
25+ return prev . filter ( id => id !== simId ) ;
26+ }
27+ } ) ;
28+ }
29+ } ;
30+
31+ const isVisible = ( simId ) => {
32+ if ( isSuperUser || isFaculty ) return true ;
33+ return visibleSimulations . includes ( simId ) ;
34+ } ;
35+
36+ const renderToggle = ( simId ) => {
37+ if ( ! isSuperUser ) return null ;
38+ const isShown = visibleSimulations . includes ( simId ) ;
39+ return (
40+ < button
41+ className = { `btn btn-sm ${
42+ isShown ? 'btn-outline-danger' : 'btn-outline-primary'
43+ } mb-3`}
44+ onClick = { ( ) => handleToggle ( simId ) }
45+ >
46+ { isShown ? 'Hide from Students' : 'Show to Students' }
47+ </ button >
48+ ) ;
49+ } ;
1350
1451 return (
1552 < >
1653 < section className = "section-sim-dashboard" >
1754 < div className = "row" >
18- { ( isSuperUser || isFaculty || coursePk === 6 ) && (
55+ { isVisible ( 1 ) && (
1956 < div className = "col-lg-5 p-4 mx-0 mx-lg-3 my-3 mx-lg-0
2057 simulation-card" >
58+ { renderToggle ( 1 ) }
2159 < h2 className = "h2-primary" >
2260 < span className = "h2-secondary d-block"
2361 data-cy = "sim-1" >
@@ -46,9 +84,10 @@ export const Dashboard = ({ isSuperUser, isFaculty}) => {
4684 </ Link >
4785 </ div >
4886 ) }
49- { ( isSuperUser || isFaculty || coursePk === 6 ) && (
87+ { isVisible ( 2 ) && (
5088 < div className = "col-lg-5 p-4 mx-0 mx-lg-3 my-3 mx-lg-0
5189 simulation-card" >
90+ { renderToggle ( 2 ) }
5291 < h2 className = "h2-primary" >
5392 < span className = "h2-secondary d-block"
5493 data-cy = "sim-2" >
@@ -73,9 +112,10 @@ export const Dashboard = ({ isSuperUser, isFaculty}) => {
73112 </ Link >
74113 </ div >
75114 ) }
76- { ( isSuperUser || isFaculty || coursePk === 6 ) && (
115+ { isVisible ( 3 ) && (
77116 < div className = "col-lg-5 p-4 mx-0 mx-lg-3 my-3 mx-lg-0
78117 simulation-card" >
118+ { renderToggle ( 3 ) }
79119 < h2 className = "h2-primary" >
80120 < span className = "h2-secondary d-block"
81121 data-cy = "sim-3" >
@@ -102,9 +142,10 @@ export const Dashboard = ({ isSuperUser, isFaculty}) => {
102142 </ Link >
103143 </ div >
104144 ) }
105- { ( isSuperUser || isFaculty || coursePk === 6 ) && (
145+ { isVisible ( 4 ) && (
106146 < div className = "col-lg-5 p-4 mx-0 mx-lg-3 my-3 mx-lg-0
107147 simulation-card" >
148+ { renderToggle ( 4 ) }
108149 < h2 className = "h2-primary" >
109150 < span className = "h2-secondary d-block"
110151 data-cy = "sim-4" >
@@ -138,5 +179,6 @@ export const Dashboard = ({ isSuperUser, isFaculty}) => {
138179
139180Dashboard . propTypes = {
140181 isSuperUser : PropTypes . bool ,
141- isFaculty : PropTypes . bool
182+ isFaculty : PropTypes . bool ,
183+ initialVisibleSims : PropTypes . array
142184} ;
0 commit comments