-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update WCEU workshop interactivity example with WP 6.5 syntax #122
Merged
juanmaguitar
merged 3 commits into
WordPress:trunk
from
luisherranz:update-wceu-workshop-interactivity-example
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 28 additions & 43 deletions
71
plugins/interactivity-api-quiz-1835fa/src/blocks/quiz-1835fa/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,40 @@ | ||
import { store, getContext, getElement } from '@wordpress/interactivity'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store } from '@wordpress/interactivity'; | ||
|
||
const { state } = store( 'quiz-1835fa-project-store', { | ||
state: { | ||
get isOpen() { | ||
const { id: quizId } = getContext(); | ||
return state.selected === quizId; | ||
get answered() { | ||
return Object.values( state.quizzes ).filter( | ||
( v ) => v.current !== null | ||
).length; | ||
}, | ||
get toggleText() { | ||
return state.isOpen ? state.closeText : state.openText; | ||
get allAnswered() { | ||
return state.answered === Object.keys( state.quizzes ).length; | ||
}, | ||
get isActive() { | ||
const { id: quizId, thisAnswer } = getContext(); | ||
return state.quizzes[ quizId ].current === thisAnswer; | ||
get correct() { | ||
return state.showAnswers | ||
? Object.values( state.quizzes ).filter( | ||
( v ) => v.current === v.correct | ||
).length | ||
: '?'; | ||
}, | ||
get inputAnswer() { | ||
const { id: quizId } = getContext(); | ||
return state.quizzes[ quizId ].current || ''; | ||
get allCorrect() { | ||
return state.correct === Object.keys( state.quizzes ).length; | ||
}, | ||
}, | ||
actions: { | ||
toggle: () => { | ||
const { id: quizId } = getContext(); | ||
if ( state.selected === quizId ) { | ||
state.selected = null; | ||
} else { | ||
state.selected = quizId; | ||
} | ||
}, | ||
closeOnEsc: ( event ) => { | ||
const { ref } = getElement(); | ||
if ( event.key === 'Escape' ) { | ||
state.selected = null; | ||
ref.querySelector( 'button[aria-controls^="quiz-"]' ).focus(); | ||
} | ||
}, | ||
answerBoolean: () => { | ||
const { id: quizId, thisAnswer } = getContext(); | ||
const quiz = state.quizzes[ quizId ]; | ||
quiz.current = quiz.current !== thisAnswer ? thisAnswer : null; | ||
}, | ||
answerInput: ( event ) => { | ||
const { id: quizId } = getContext(); | ||
state.quizzes[ quizId ].current = event.target.value || null; | ||
}, | ||
}, | ||
callbacks: { | ||
focusOnOpen: () => { | ||
const { ref } = getElement(); | ||
if ( state.isOpen ) { | ||
ref.focus(); | ||
} | ||
checkAnswers: () => { | ||
state.showAnswers = true; | ||
state.selected = null; | ||
}, | ||
reset: () => { | ||
state.showAnswers = false; | ||
state.selected = null; | ||
Object.values( state.quizzes ).forEach( ( quiz ) => { | ||
quiz.current = null; | ||
} ); | ||
}, | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 50 additions & 25 deletions
75
plugins/interactivity-api-quiz-1835fa/src/blocks/quiz-progress-1835fa/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,64 @@ | ||
import { store } from '@wordpress/interactivity'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store, getContext, getElement } from '@wordpress/interactivity'; | ||
|
||
const { state } = store( 'quiz-1835fa-project-store', { | ||
state: { | ||
get answered() { | ||
return Object.values( state.quizzes ).filter( | ||
( v ) => v.current !== null | ||
).length; | ||
get isOpen() { | ||
const { id } = getContext(); | ||
return state.selected === id; | ||
}, | ||
get allAnswered() { | ||
return state.answered === Object.keys( state.quizzes ).length; | ||
get toggleText() { | ||
return state.isOpen ? state.closeText : state.openText; | ||
}, | ||
get correct() { | ||
return state.showAnswers | ||
? Object.values( state.quizzes ).filter( | ||
( v ) => v.current === v.correct | ||
).length | ||
: '?'; | ||
get isActive() { | ||
const { id, thisAnswer } = getContext(); | ||
return state.quizzes[ id ].current === thisAnswer; | ||
}, | ||
get allCorrect() { | ||
return state.correct === Object.keys( state.quizzes ).length; | ||
}, | ||
get totalQuizzes() { | ||
return Object.values( state.quizzes ).length; | ||
get inputAnswer() { | ||
const { id } = getContext(); | ||
return state.quizzes[ id ].current || ''; | ||
}, | ||
}, | ||
actions: { | ||
checkAnswers: () => { | ||
state.showAnswers = true; | ||
state.selected = null; | ||
toggle() { | ||
const { id } = getContext(); | ||
|
||
if ( state.selected === id ) { | ||
state.selected = null; | ||
} else { | ||
state.selected = id; | ||
} | ||
}, | ||
reset: () => { | ||
state.showAnswers = false; | ||
Object.values( state.quizzes ).forEach( ( quiz ) => { | ||
closeOnEsc( event ) { | ||
if ( event.key === 'Escape' ) { | ||
state.selected = null; | ||
const { ref } = getElement(); | ||
ref.querySelector( 'button[aria-controls^="quiz-"]' ).focus(); | ||
} | ||
}, | ||
answerBoolean() { | ||
const { id, thisAnswer } = getContext(); | ||
const quiz = state.quizzes[ id ]; | ||
|
||
if ( quiz.current !== thisAnswer ) { | ||
quiz.current = thisAnswer; | ||
} else { | ||
quiz.current = null; | ||
} ); | ||
} | ||
}, | ||
answerInput( event ) { | ||
const { id } = getContext(); | ||
state.quizzes[ id ].current = event.target.value || null; | ||
}, | ||
}, | ||
callbacks: { | ||
focusOnOpen() { | ||
if ( state.isOpen ) { | ||
const { ref } = getElement(); | ||
ref.focus(); | ||
} | ||
}, | ||
}, | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luisherranz the portion of code
<?php echo count( $state['quizzes'] );
is throwing an errorI temporarily fixed the error by adding this and this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state.quizzes.length
is not dynamically changed in the client, so it should be read from a static PHP variable instead of a client getter.Apart from that, we need that value in the server so it's properly server-side rendered.
What's the exact error that it throwed in production?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, it doesn't work right now because
state.quizzes
is an object, so it should beObject.keys(state.quizzes).length
.But, anyway, let's fix it properly 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the error I get with
<?php echo count( $state['quizzes'] ); ?>
on line 33There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait. I know what's happening. The quiz blocks need to be inner blocks of the quiz progress block. Can you change the blueprint to be like that? I'll make the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luisherranz In that case, do we need to set the namespace for the children as it is done here?
Also, are independent blocks unable to share namespace and state/store?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, even though it's not strictly required, it's a good practice to always add the
data-wp-interactive
directive.What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, can two blocks being at the same level (no inner blocks) in different parts of the HTML share namespace and state/store
For example, having two blocks registered by different plugins, can they share namespace (and store/actions) without a parent/child relationship?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yeah, sure 🙂