PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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.4.4, at assets/src/apps/js/frontend/quiz/components/timer/index.js

77 lines 1.9 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 let totalTime = getData( 'totalTime' );
13 const durationTime = getData( 'duration' );
14 const timeSpendG = getData( 'timeSpend' );
15 const [ seconds, setSeconds ] = useState( totalTime );
16 let [ timeSpend, setTimeSpend ] = useState( timeSpendG );
17
18 useEffect( () => {
19 const myInterval = setInterval( () => {
20 if ( durationTime > 0 ) {
21 let remainSeconds = seconds;
22 remainSeconds -= 1;
23
24 remainSeconds = wp.hooks.applyFilters( 'js-lp-quiz-remaining_time', remainSeconds, durationTime );
25
26 if ( remainSeconds > 0 ) {
27 setSeconds( remainSeconds );
28 timeSpend++;
29 setTimeSpend( durationTime - remainSeconds );
30 } else {
31 clearInterval( myInterval );
32 submitQuiz();
33 }
34 } else { // Apply when set duration = 0
35 timeSpend++;
36 setTimeSpend( timeSpend );
37 setSeconds( timeSpend );
38 }
39 }, 1000 );
40
41 return () => clearInterval( myInterval );
42 }, [ seconds, timeSpend ] );
43
44 const formatTime = ( separator = ':' ) => {
45 const t = [];
46 let m;
47
48 if ( timeSpendG ) {
49 totalTime = timeSpendG;
50 }
51
52 if ( totalTime < 3600 ) {
53 t.push( ( seconds - ( seconds % 60 ) ) / 60 );
54 t.push( seconds % 60 );
55 } else if ( totalTime ) {
56 t.push( ( seconds - ( seconds % 3600 ) ) / 3600 );
57 m = seconds % 3600;
58 t.push( ( m - ( m % 60 ) ) / 60 );
59 t.push( m % 60 );
60 }
61
62 return t.map( ( a ) => {
63 return a < 10 ? `0${ a }` : a;
64 } ).join( separator );
65 };
66
67 return (
68 <div className="countdown">
69 <i className="lp-icon-stopwatch"></i>
70 <span>{ formatTime() }</span>
71 <input type="hidden" name="lp-quiz-time-spend" value={ timeSpend } />
72 </div>
73 );
74 };
75
76 export default Timer;
77