PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 / index.js

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

111 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Component } from '@wordpress/element';
2 import { compose } from '@wordpress/compose';
3 import { withDispatch, withSelect } from '@wordpress/data';
4 import { Content, Meta, Buttons, Questions, Attempts, Result, Status } from './components';
5
6 import store from './store';
7
8 const { chunk } = lodash;
9 class Quiz extends Component {
10 constructor( props ) {
11 super( ...arguments );
12
13 this.state = {
14 currentPage: 1,
15 numPages: 0,
16 pages: [],
17 };
18 }
19
20 componentDidMount() {
21 const { settings, setQuizData } = this.props;
22
23 const { question_ids, questions_per_page } = settings;
24
25 const chunks = chunk( question_ids, questions_per_page );
26
27 settings.currentPage = 1;
28 settings.numPages = chunks.length;
29 settings.pages = chunks;
30
31 const answered = settings.id ? localStorage.getItem( `LP_Quiz_${ settings.id }_Answered` ) : false;
32
33 if ( answered ) {
34 settings.answered = JSON.parse( answered );
35 }
36
37 setQuizData( settings );
38 }
39
40 componentDidUpdate( prevProps, prevState, snapshot ) {
41 const { status } = prevProps;
42 const elQuizContent = document.querySelector( '.quiz-content' );
43
44 if ( status !== undefined && elQuizContent ) {
45 elQuizContent.style.display = 'none';
46 }
47 }
48
49 startQuiz = ( event ) => {
50 this.props.startQuiz();
51 };
52
53 render() {
54 const { status, isReviewing } = this.props;
55
56 const isA =
57 -1 !== [ '', 'completed', 'viewed' ].indexOf( status ) || ! status;
58 const notStarted =
59 -1 !== [ '', 'viewed', undefined ].indexOf( status ) || ! status;
60
61 // Just render content if status !== undefined (meant all data loaded)
62 return (
63 undefined !== status && (
64 <>
65 <div>
66 { ! isReviewing && 'completed' === status && (
67 <Result />
68 ) }
69
70 { ! isReviewing && notStarted && <Meta /> }
71
72 { 'started' === status && <Status /> }
73
74 { ( -1 !== [ 'completed', 'started' ].indexOf( status ) ||
75 isReviewing ) && <Questions /> }
76
77 <Buttons />
78
79 { isA && ! isReviewing && <Attempts /> }
80 </div>
81 </>
82 )
83 );
84 }
85 }
86
87 export default compose( [
88 withSelect( ( select ) => {
89 const { getQuestions, getData } = select( 'learnpress/quiz' );
90
91 return {
92 questions: getQuestions(),
93 status: getData( 'status' ),
94 store: getData(),
95 answered: getData( 'answered' ),
96 isReviewing: getData( 'mode' ) === 'reviewing',
97 questionIds: getData( 'questionIds' ),
98 checkCount: getData( 'instantCheck' ),
99 questionsPerPage: getData( 'questionsPerPage' ) || 1,
100 };
101 } ),
102 withDispatch( ( dispatch ) => {
103 const { setQuizData, startQuiz } = dispatch( 'learnpress/quiz' );
104
105 return {
106 setQuizData,
107 startQuiz,
108 };
109 } ),
110 ] )( Quiz );
111