PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / assets / src / apps / js / frontend / quiz / components / timer / index.js

index.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9, at assets/src/apps/js/frontend/quiz/components/timer/index.js

79 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Edit: React hook.
3 *
4 * @author Nhamdv - ThimPress
5 */
6 import { useEffect, useState } from '@wordpress/element';
7 import { select, dispatch } from '@wordpress/data';
8
9 const Timer = () => {
10 const { getData } = select( 'learnpress/quiz' );
11 const { submitQuiz } = dispatch( 'learnpress/quiz' );
12
13 const totalTime = getData( 'totalTime' );
14 const durationTime = getData( 'duration' );
15 /* const endTime = getData( 'endTime' );
16
17 const d1 = new Date( endTime.replace( /-/g, '/' ) );
18 const d2 = new Date();
19 const tz = new Date().getTimezoneOffset();
20 const t = parseInt( ( d1.getTime() / 1000 ) - ( ( d2.getTime() / 1000 ) + ( tz * 60 ) ) );*/
21
22 const [ seconds, setSeconds ] = useState( totalTime );
23 let [ timeSpend, setTimeSpend ] = useState( 0 );
24 const limitTime = totalTime > 0;
25
26 useEffect( () => {
27 const myInterval = setInterval( () => {
28 if ( limitTime ) {
29 let remainSeconds = seconds;
30 remainSeconds -= 1;
31
32 if ( remainSeconds > 0 ) {
33 setSeconds( remainSeconds );
34 timeSpend++;
35 setTimeSpend( durationTime - remainSeconds );
36 } else {
37 clearInterval( myInterval );
38 submitQuiz();
39 }
40 } else { // Apply when set duration = 0
41 timeSpend++;
42 setTimeSpend( timeSpend );
43 setSeconds( timeSpend );
44 }
45 }, 1000 );
46
47 return () => clearInterval( myInterval );
48 }, [ seconds, timeSpend ] );
49
50 const formatTime = ( separator = ':' ) => {
51 const t = [];
52 let m;
53
54 if ( totalTime < 3600 ) {
55 t.push( ( seconds - ( seconds % 60 ) ) / 60 );
56 t.push( seconds % 60 );
57 } else if ( totalTime ) {
58 t.push( ( seconds - ( seconds % 3600 ) ) / 3600 );
59 m = seconds % 3600;
60 t.push( ( m - ( m % 60 ) ) / 60 );
61 t.push( m % 60 );
62 }
63
64 return t.map( ( a ) => {
65 return a < 10 ? `0${ a }` : a;
66 } ).join( separator );
67 };
68
69 return (
70 <div className="countdown">
71 <i className="fas fa-stopwatch"></i>
72 <span>{ formatTime() }</span>
73 <input type="hidden" name="lp-quiz-time-spend" value={ timeSpend } />
74 </div>
75 );
76 };
77
78 export default Timer;
79