-
-
Notifications
You must be signed in to change notification settings - Fork 94
feature-1035 Add iFrame exercise type and related components #1114
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
+356
−3
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 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
135 changes: 135 additions & 0 deletions
135
...mponents/exercises/components/CreateExercise/components/IframeExercise/IframeExercise.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,135 @@ | ||
| import { ExerciseComponentProps } from "@components/routes/AssignmentBuilder/components/exercises/components/CreateExercise/types/ExerciseTypes"; | ||
| import { FC } from "react"; | ||
|
|
||
| import { CreateExerciseFormType } from "@/types/exercises"; | ||
| import { createExerciseId } from "@/utils/exercise"; | ||
| import { generateIframePreview } from "@/utils/preview/iframePreview"; | ||
|
|
||
| import { IFRAME_STEP_VALIDATORS } from "../../config/stepConfigs"; | ||
| import { useBaseExercise } from "../../hooks/useBaseExercise"; | ||
| import { useExerciseStepNavigation } from "../../hooks/useExerciseStepNavigation"; | ||
| import { ExerciseLayout } from "../../shared/ExerciseLayout"; | ||
| import { validateCommonFields } from "../../utils/validation"; | ||
|
|
||
| import { IframeExerciseSettings } from "./IframeExerciseSettings"; | ||
| import { IframePreview } from "./IframePreview"; | ||
| import { IframeUrlInput } from "./components/IframeUrlInput"; | ||
|
|
||
| const IFRAME_STEPS = [{ label: "iFrame URL" }, { label: "Settings" }, { label: "Preview" }]; | ||
|
|
||
| // Define the default form data | ||
| const getDefaultFormData = (): Partial<CreateExerciseFormType> => ({ | ||
| name: createExerciseId(), | ||
| author: "", | ||
| topic: "", | ||
| chapter: "", | ||
| subchapter: "", | ||
| tags: "", | ||
| points: 1, | ||
| difficulty: 3, | ||
| htmlsrc: "", | ||
| question_type: "iframe", | ||
| iframeSrc: "" | ||
| }); | ||
|
|
||
| // Create a wrapper for generateIframePreview to match the expected type | ||
| const generatePreview = (data: Partial<CreateExerciseFormType>): string => { | ||
| return generateIframePreview(data.iframeSrc || "", data.name || ""); | ||
| }; | ||
|
|
||
| export const IframeExercise: FC<ExerciseComponentProps> = ({ | ||
| initialData, | ||
| onSave, | ||
| onCancel, | ||
| resetForm, | ||
| onFormReset, | ||
| isEdit = false | ||
| }) => { | ||
| const { | ||
| formData, | ||
| activeStep, | ||
| isSaving, | ||
| updateFormData, | ||
| handleSettingsChange, | ||
| isCurrentStepValid, | ||
| goToNextStep, | ||
| goToPrevStep, | ||
| handleSave: baseHandleSave, | ||
| setActiveStep | ||
| } = useBaseExercise({ | ||
| initialData, | ||
| steps: IFRAME_STEPS, | ||
| exerciseType: "iframe", | ||
| generatePreview, | ||
| validateStep: (step, data) => { | ||
| const errors = IFRAME_STEP_VALIDATORS[step](data); | ||
|
|
||
| return errors.length === 0; | ||
| }, | ||
| validateForm: validateCommonFields, | ||
| getDefaultFormData, | ||
| onSave: onSave as (data: Partial<CreateExerciseFormType>) => Promise<void>, | ||
| onCancel, | ||
| resetForm, | ||
| onFormReset, | ||
| isEdit | ||
| }); | ||
|
|
||
| // Use our centralized navigation and validation hook | ||
| const { validation, handleNext, handleStepSelect, handleSave, stepsValidity } = | ||
| useExerciseStepNavigation({ | ||
| data: formData, | ||
| activeStep, | ||
| setActiveStep, | ||
| stepValidators: IFRAME_STEP_VALIDATORS, | ||
| goToNextStep, | ||
| goToPrevStep, | ||
| steps: IFRAME_STEPS, | ||
| handleBaseSave: baseHandleSave, | ||
| generateHtmlSrc: generatePreview, | ||
| updateFormData | ||
| }); | ||
|
|
||
| // Render step content | ||
| const renderStepContent = () => { | ||
| switch (activeStep) { | ||
| case 0: // iFrame URL | ||
| return ( | ||
| <IframeUrlInput | ||
| iframeSrc={formData.iframeSrc || ""} | ||
| onChange={(url: string) => updateFormData("iframeSrc", url)} | ||
| /> | ||
| ); | ||
|
|
||
| case 1: // Settings | ||
| return <IframeExerciseSettings formData={formData} onChange={handleSettingsChange} />; | ||
|
|
||
| case 2: // Preview | ||
| return <IframePreview iframeSrc={formData.iframeSrc || ""} name={formData.name || ""} />; | ||
|
|
||
| default: | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <ExerciseLayout | ||
| title="iFrame Exercise" | ||
| exerciseType="iframe" | ||
| isEdit={isEdit} | ||
| steps={IFRAME_STEPS} | ||
| activeStep={activeStep} | ||
| isCurrentStepValid={isCurrentStepValid} | ||
| isSaving={isSaving} | ||
| stepsValidity={stepsValidity} | ||
| onCancel={onCancel} | ||
| onBack={goToPrevStep} | ||
| onNext={handleNext} | ||
| onSave={handleSave} | ||
| onStepSelect={handleStepSelect} | ||
| validation={validation} | ||
| > | ||
| {renderStepContent()} | ||
| </ExerciseLayout> | ||
| ); | ||
| }; |
22 changes: 22 additions & 0 deletions
22
.../exercises/components/CreateExercise/components/IframeExercise/IframeExerciseSettings.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,22 @@ | ||
| import { FC } from "react"; | ||
|
|
||
| import { CreateExerciseFormType } from "@/types/exercises"; | ||
|
|
||
| import { | ||
| BaseExerciseSettings, | ||
| BaseExerciseSettingsContent | ||
| } from "../../shared/BaseExerciseSettingsContent"; | ||
|
|
||
| interface IframeExerciseSettingsProps { | ||
| formData: Partial<CreateExerciseFormType>; | ||
| onChange: (settings: Partial<CreateExerciseFormType>) => void; | ||
| } | ||
|
|
||
| export const IframeExerciseSettings: FC<IframeExerciseSettingsProps> = ({ formData, onChange }) => { | ||
| return ( | ||
| <BaseExerciseSettingsContent<BaseExerciseSettings> | ||
| initialData={formData} | ||
| onSettingsChange={onChange} | ||
| /> | ||
| ); | ||
| }; |
17 changes: 17 additions & 0 deletions
17
...omponents/exercises/components/CreateExercise/components/IframeExercise/IframePreview.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,17 @@ | ||
| import { ExercisePreview } from "@components/routes/AssignmentBuilder/components/exercises/components/ExercisePreview/ExercisePreview"; | ||
| import { FC } from "react"; | ||
|
|
||
| import { generateIframePreview } from "@/utils/preview/iframePreview"; | ||
|
|
||
| interface IframePreviewProps { | ||
| iframeSrc: string; | ||
| name: string; | ||
| } | ||
|
|
||
| export const IframePreview: FC<IframePreviewProps> = ({ iframeSrc, name }) => { | ||
| return ( | ||
| <div style={{ display: "flex", alignItems: "start", justifyContent: "center" }}> | ||
| <ExercisePreview htmlsrc={generateIframePreview(iframeSrc || "", name || "")} /> | ||
| </div> | ||
| ); | ||
| }; |
78 changes: 78 additions & 0 deletions
78
...ercises/components/CreateExercise/components/IframeExercise/components/IframeUrlInput.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,78 @@ | ||
| import { InputText } from "primereact/inputtext"; | ||
| import { FC } from "react"; | ||
|
|
||
| import { useValidation } from "../../../shared/ExerciseLayout"; | ||
| import styles from "../../../shared/styles/CreateExercise.module.css"; | ||
|
|
||
| interface IframeUrlInputProps { | ||
| iframeSrc: string; | ||
| onChange: (url: string) => void; | ||
| } | ||
|
|
||
| const isValidUrl = (url: string): boolean => { | ||
| if (!url.trim()) return false; | ||
| try { | ||
| new URL(url); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| export const IframeUrlInput: FC<IframeUrlInputProps> = ({ iframeSrc, onChange }) => { | ||
| const { shouldShowValidation } = useValidation(); | ||
| const isEmpty = !iframeSrc?.trim(); | ||
| const isInvalidUrl = iframeSrc?.trim() && !isValidUrl(iframeSrc); | ||
| const shouldShowError = (isEmpty || isInvalidUrl) && shouldShowValidation; | ||
|
|
||
| return ( | ||
| <> | ||
| <div className={styles.formField}> | ||
| <label htmlFor="iframeSrc" className="font-medium block mb-2"> | ||
| iFrame Source URL * | ||
| </label> | ||
| <InputText | ||
| id="iframeSrc" | ||
| value={iframeSrc} | ||
| onChange={(e) => onChange(e.target.value)} | ||
| placeholder="https://example.com/exercise" | ||
| className={`w-full ${shouldShowError ? "p-invalid" : ""}`} | ||
| /> | ||
| {shouldShowError && isEmpty && ( | ||
| <small className="p-error mt-1 block">iFrame URL is required</small> | ||
| )} | ||
| {shouldShowError && isInvalidUrl && ( | ||
| <small className="p-error mt-1 block">Please enter a valid URL</small> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className={styles.questionTips}> | ||
| <i className="pi pi-lightbulb" style={{ marginRight: "4px" }}></i> | ||
| <span> | ||
| Tip: Enter a valid URL that can be embedded in an iframe (e.g., videos, interactive | ||
| content, external tools). | ||
| </span> | ||
| </div> | ||
|
|
||
| {iframeSrc && isValidUrl(iframeSrc) && ( | ||
| <div className="mt-4"> | ||
| <label className="font-medium block mb-2">Preview</label> | ||
| <div | ||
| style={{ | ||
| border: "1px solid var(--surface-border)", | ||
| borderRadius: "6px", | ||
| overflow: "hidden" | ||
| }} | ||
| > | ||
| <iframe | ||
| src={iframeSrc} | ||
| title="iFrame Preview" | ||
| style={{ width: "100%", height: "400px", border: "none" }} | ||
| allowFullScreen | ||
| /> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </> | ||
| ); | ||
| }; | ||
1 change: 1 addition & 0 deletions
1
...ponents/exercises/components/CreateExercise/components/IframeExercise/components/index.ts
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 @@ | ||
| export { IframeUrlInput } from "./IframeUrlInput"; |
3 changes: 3 additions & 0 deletions
3
...Builder/components/exercises/components/CreateExercise/components/IframeExercise/index.ts
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,3 @@ | ||
| export { IframeExercise } from "./IframeExercise"; | ||
| export { IframeExerciseSettings } from "./IframeExerciseSettings"; | ||
| export { IframePreview } from "./IframePreview"; |
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
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.
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.
Don't specify a height, as many iframe exercises will request a resize and this prevents students from seeing the entire question.
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.
Actually I can take care of this and then I'll merge.