-
Notifications
You must be signed in to change notification settings - Fork 85
Feat/quiz type puzzle #2548
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
Merged
Merged
Feat/quiz type puzzle #2548
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f593f77
feat(quiz): add puzzle question type with validation and UI components
saadman30 b8cde78
feat(quiz): enhance puzzle question type with grid size handling and …
saadman30 b34948d
Merge branch '4.0.0-dev' into feat/quiz-type-puzzle
saadman30 dde5ba7
refactor: update image input handling in FormPuzzle component
saadman30 8f94450
Merge branch '4.0.0-dev' into feat/quiz-type-puzzle
saadman30 0a103ba
feat: add PuzzlePreview component for quiz question type
saadman30 60122da
refactor: remove outdated comments in QuestionPreviewModal
saadman30 283a88b
Merge branch '4.0.0-dev' into feat/quiz-type-puzzle
saadman30 30a3777
Enhancement: Update Puzzle question type to include difficulty levels…
saadman30 233fb6a
Merge branch '4.0.0-dev' into feat/quiz-type-puzzle
saadman30 ecc8311
Enhancement: Refactor QuestionPreviewModal and PuzzlePreview componen…
saadman30 2f9111d
Enhancement: Improve layout and responsiveness of puzzle question pre…
saadman30 8d7dc9b
Enhancement: Update QuestionPreviewModal to utilize a constant for Tu…
saadman30 a41cd6b
Enhancement: Refactor question types to streamline scale and puzzle c…
saadman30 05ccdd3
Enhancement: Refactor Puzzle question type to improve state managemen…
saadman30 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
assets/src/js/v3/entries/course-builder/components/curriculum/question-types/Puzzle.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { css } from '@emotion/react'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useEffect, useMemo } from 'react'; | ||
| import { Controller, useFieldArray, useFormContext } from 'react-hook-form'; | ||
|
|
||
| import { useQuizModalContext } from '@CourseBuilderContexts/QuizModalContext'; | ||
| import type { QuizForm } from '@CourseBuilderServices/quiz'; | ||
| import FormSelectInput from '@TutorShared/components/fields/FormSelectInput'; | ||
| import FormPuzzle from '@TutorShared/components/fields/quiz/questions/FormPuzzle'; | ||
| import { spacing } from '@TutorShared/config/styles'; | ||
| import { calculateQuizDataStatus } from '@TutorShared/utils/quiz'; | ||
| import { styleUtils } from '@TutorShared/utils/style-utils'; | ||
| import { QuizDataStatus, type QuizQuestionOption } from '@TutorShared/utils/types'; | ||
| import { nanoid } from '@TutorShared/utils/util'; | ||
|
|
||
| const Puzzle = () => { | ||
| const form = useFormContext<QuizForm>(); | ||
| const { activeQuestionId, activeQuestionIndex, validationError, setValidationError } = useQuizModalContext(); | ||
| const activeQuestionDataStatus = | ||
| form.watch(`questions.${activeQuestionIndex}._data_status`) ?? QuizDataStatus.NO_CHANGE; | ||
|
|
||
| const answersPath = `questions.${activeQuestionIndex}.question_answers` as 'questions.0.question_answers'; | ||
| const gridSizePath = | ||
| `questions.${activeQuestionIndex}.question_settings.puzzle_grid_size` as 'questions.0.question_settings.puzzle_grid_size'; | ||
|
|
||
| const { fields: optionsFields } = useFieldArray({ | ||
| control: form.control, | ||
| name: answersPath, | ||
| }); | ||
|
|
||
| const gridSizeOptions = useMemo( | ||
| () => | ||
| [2, 3, 4, 5, 6, 7].map((value) => ({ | ||
| label: `${value} x ${value}`, | ||
| value, | ||
| })), | ||
| [], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!activeQuestionId) { | ||
| return; | ||
| } | ||
| if (optionsFields.length > 0) { | ||
| return; | ||
| } | ||
| const baseAnswer: QuizQuestionOption = { | ||
| _data_status: QuizDataStatus.NEW, | ||
| is_saved: true, | ||
| answer_id: nanoid(), | ||
| belongs_question_id: activeQuestionId, | ||
| belongs_question_type: 'puzzle' as QuizQuestionOption['belongs_question_type'], | ||
| answer_title: '', | ||
| is_correct: '1', | ||
| image_id: undefined, | ||
| image_url: '', | ||
| answer_two_gap_match: '', | ||
| answer_view_format: 'puzzle', | ||
| answer_order: 0, | ||
| }; | ||
| form.setValue(answersPath, [baseAnswer]); | ||
| }, [activeQuestionId, optionsFields.length, answersPath, form]); | ||
|
|
||
| useEffect(() => { | ||
| const currentValue = form.getValues(gridSizePath); | ||
| if (currentValue === undefined || currentValue === null || Number.isNaN(Number(currentValue))) { | ||
| form.setValue(gridSizePath, 4); | ||
| } | ||
| }, [form, gridSizePath]); | ||
|
|
||
| if (optionsFields.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div css={styles.optionWrapper}> | ||
| <Controller | ||
| key={optionsFields[0]?.id} | ||
| control={form.control} | ||
| name={`questions.${activeQuestionIndex}.question_answers.0` as 'questions.0.question_answers.0'} | ||
| render={(answerControllerProps) => ( | ||
| <Controller | ||
| control={form.control} | ||
| name={gridSizePath} | ||
| render={(gridSizeControllerProps) => ( | ||
| <FormPuzzle | ||
| {...answerControllerProps} | ||
| questionId={activeQuestionId} | ||
| validationError={validationError} | ||
| setValidationError={setValidationError} | ||
| gridSizeControl={ | ||
|
saadman30 marked this conversation as resolved.
Outdated
|
||
| <FormSelectInput | ||
| {...gridSizeControllerProps} | ||
| label={__('Grid Size', 'tutor')} | ||
| options={gridSizeOptions} | ||
| helpText={__('Choose puzzle density from 2 x 2 to 7 x 7.', 'tutor')} | ||
| onChange={(option) => { | ||
| gridSizeControllerProps.field.onChange(option.value); | ||
| if (calculateQuizDataStatus(activeQuestionDataStatus, QuizDataStatus.UPDATE)) { | ||
| form.setValue( | ||
| `questions.${activeQuestionIndex}._data_status`, | ||
| calculateQuizDataStatus(activeQuestionDataStatus, QuizDataStatus.UPDATE) as QuizDataStatus, | ||
| ); | ||
| } | ||
| }} | ||
| /> | ||
| } | ||
| /> | ||
| )} | ||
| /> | ||
| )} | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Puzzle; | ||
|
|
||
| const styles = { | ||
| optionWrapper: css` | ||
| ${styleUtils.display.flex('column')}; | ||
| gap: ${spacing[16]}; | ||
| padding-left: ${spacing[40]}; | ||
| `, | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.