@@ -25,13 +25,19 @@ import { groupExamLocksSelector } from '../../redux/selectors/groupExamLocks';
2525import { lockedStudentsOfGroupSelector } from '../../redux/selectors/usersGroups' ;
2626import { loggedInUserIdSelector } from '../../redux/selectors/auth' ;
2727import { isLoggedAsSuperAdmin , loggedInUserSelector } from '../../redux/selectors/users' ;
28+ import { getJsData } from '../../redux/helpers/resourceManager' ;
2829
2930import withLinks from '../../helpers/withLinks' ;
3031import { hasPermissions , safeGet } from '../../helpers/common' ;
3132import ResourceRenderer from '../../components/helpers/ResourceRenderer' ;
3233import { isExam } from '../../helpers/groups' ;
3334
35+ const REFRESH_INTERVAL = 1 ; // [s]
36+
3437class GroupExams extends Component {
38+ state = { examInProgress : false } ;
39+ intervalHandler = null ;
40+
3541 static loadAsync = ( { groupId } , dispatch ) =>
3642 Promise . all ( [
3743 dispatch ( fetchGroupIfNeeded ( groupId ) ) . then ( ( { value : group } ) =>
@@ -41,11 +47,31 @@ class GroupExams extends Component {
4147 ) ,
4248 ] ) ;
4349
50+ static getDerivedStateFromProps ( { group } ) {
51+ const groupJs = getJsData ( group ) ;
52+ const examInProgress = Boolean ( groupJs && isExam ( groupJs ) ) ;
53+ return { examInProgress } ;
54+ }
55+
56+ periodicRefresh = ( ) => {
57+ const newState = GroupExams . getDerivedStateFromProps ( this . props , this . state ) ;
58+ if ( newState . examInProgress !== this . state . examInProgress ) {
59+ this . setState ( newState ) ;
60+ }
61+ } ;
62+
4463 componentDidMount ( ) {
4564 this . props . loadAsync ( ) ;
4665 if ( this . props . params . examId ) {
4766 this . props . loadGroupExamLocks ( ) ;
4867 }
68+
69+ if ( window && 'setInterval' in window ) {
70+ if ( this . intervalHandler ) {
71+ window . clearInterval ( this . intervalHandler ) ;
72+ }
73+ this . intervalHandler = window . setInterval ( this . periodicRefresh , REFRESH_INTERVAL * 1000 ) ;
74+ }
4975 }
5076
5177 componentDidUpdate ( prevProps ) {
@@ -57,6 +83,13 @@ class GroupExams extends Component {
5783 }
5884 }
5985
86+ componentWillUnmount ( ) {
87+ if ( this . intervalHandler ) {
88+ window . clearInterval ( this . intervalHandler ) ;
89+ this . intervalHandler = null ;
90+ }
91+ }
92+
6093 linkFactory = id => {
6194 const {
6295 params : { groupId, examId = null } ,
@@ -114,19 +147,19 @@ class GroupExams extends Component {
114147 unlimitedHeight >
115148 < GroupExamsTable
116149 exams = { group . privateData . exams }
117- selected = { isExam ( group ) ? null : examId }
118- linkFactory = { isExam ( group ) ? null : this . linkFactory }
150+ selected = { this . state . examInProgress ? null : examId }
151+ linkFactory = { this . state . examInProgress ? null : this . linkFactory }
119152 />
120153 </ Box >
121154 </ Col >
122155 </ Row >
123156
124- { ( examId || isExam ( group ) ) && hasPermissions ( group , 'viewStudents' , 'setExamPeriod' ) && (
157+ { ( examId || this . state . examInProgress ) && hasPermissions ( group , 'viewStudents' , 'setExamPeriod' ) && (
125158 < Row >
126159 < Col xs = { 12 } >
127160 < Box
128161 title = {
129- isExam ( group ) ? (
162+ this . state . examInProgress ? (
130163 < FormattedMessage
131164 id = "app.groupExams.studentsBoxTitle"
132165 defaultMessage = "Participating students"
@@ -140,7 +173,7 @@ class GroupExams extends Component {
140173 }
141174 noPadding
142175 unlimitedHeight >
143- { isExam ( group ) ? (
176+ { this . state . examInProgress ? (
144177 < LockedStudentsTable
145178 groupId = { group . id }
146179 lockedStudents = { lockedStudents }
0 commit comments