| 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 |
|