-
Notifications
You must be signed in to change notification settings - Fork 0
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
Modify preview-card styles #88
Merged
jomcarvajal
merged 6 commits into
preview-card
from
CORE-675-new-styles-for-preview-card
Jan 31, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5bb4663
modify preview card styles
jomcarvajal ce37ecc
change color of card when is included
jomcarvajal d0756aa
resolve comments - fix style issues
jomcarvajal edac142
change props in storybook of Exercise
jomcarvajal 42d25f7
Add prop method for details button
jomcarvajal d268bce
Refactor props of ExercisePreview
jomcarvajal 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
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
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ExercisePreview } from './ExercisePreview'; | ||
import renderer from 'react-test-renderer'; | ||
import { ExerciseData } from 'src/types'; | ||
|
||
describe('ExercisePreview', () => { | ||
describe('using step data', () => { | ||
|
||
let exercise: ExerciseData; | ||
|
||
beforeEach(() => { | ||
exercise = { | ||
uid: '1@1', | ||
uuid: 'e4e27897-4abc-40d3-8565-5def31795edc', | ||
group_uuid: '20e82bf6-232e-40c8-ba68-2d22c6498f69', | ||
number: 1, | ||
version: 1, | ||
published_at: '2022-09-06T20:32:21.981Z', | ||
context: 'Context', | ||
stimulus_html: '<b>Stimulus HTML</b>', | ||
tags: [], | ||
authors: [{ user_id: 1, name: 'OpenStax' }], | ||
copyright_holders: [{ user_id: 1, name: 'OpenStax' }], | ||
derived_from: [], | ||
is_vocab: false, | ||
solutions_are_public: false, | ||
versions: [1], | ||
questions: [{ | ||
id: '1234@5', | ||
collaborator_solutions: [], | ||
formats: ['true-false'], | ||
stimulus_html: '', | ||
stem_html: '', | ||
is_answer_order_important: false, | ||
answers: [{ | ||
id: '1', | ||
correctness: undefined, | ||
content_html: 'True', | ||
}, { | ||
id: '2', | ||
correctness: undefined, | ||
content_html: 'False', | ||
}], | ||
}], | ||
}; | ||
}); | ||
|
||
it.each` | ||
enableOverlay | selected | description | ||
${true} | ${true} | ${'with overlay and selected true'} | ||
${true} | ${true} | ${'with overlay and selected false'} | ||
${false} | ${false} | ${'without overlay'} | ||
`('matches snapshot %description', ({ enableOverlay, selected }: { enableOverlay: boolean, selected: boolean }) => { | ||
const onIncludeMock = jest.fn(); | ||
const onRemoveMock = jest.fn(); | ||
const onDetailsMock = jest.fn(); | ||
const tree = renderer.create( | ||
<ExercisePreview | ||
exercise={exercise} | ||
enableOverlay={enableOverlay} | ||
selected={selected} | ||
onIncludeHandler={onIncludeMock} | ||
onRemoveHandler={onRemoveMock} | ||
onClickDetails={onDetailsMock} | ||
/> | ||
).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React from "react"; | ||
import { ExerciseData, ExerciseQuestionData, StepBase } from "src/types"; | ||
import { IncludeRemoveQuestion } from "./IncludeRemoveQuestion"; | ||
import { Exercise } from "./Exercise"; | ||
|
||
export interface ExercisePreviewProps { | ||
exercise: ExerciseData; | ||
selected: boolean; | ||
onIncludeHandler: () => void; | ||
onRemoveHandler: () => void; | ||
onClickDetails: () => void; | ||
enableOverlay?: boolean; | ||
} | ||
|
||
/** | ||
* An Exercise version with less interaction with card and grants an Overlay with Include/Remove component | ||
*/ | ||
export const ExercisePreview = ( | ||
{ | ||
exercise, | ||
selected, | ||
onIncludeHandler, | ||
onRemoveHandler, | ||
onClickDetails, | ||
enableOverlay = false, | ||
}: ExercisePreviewProps) => { | ||
|
||
const exercisePreviewProps = (exercise: ExerciseData) => { | ||
const formatAnswerData = (questions: ExerciseQuestionData[]) => questions.map((q) => ( | ||
{ id: q.id, correct_answer_id: (q.answers.find((a) => a.correctness === '1.0')?.id || '') })); | ||
|
||
const questionStateFields = { | ||
available_points: '1.0', | ||
is_completed: true, | ||
answer_id: '1', | ||
free_response: '', | ||
feedback_html: '', | ||
correct_answer_feedback_html: '', | ||
attempts_remaining: 0, | ||
attempt_number: 1, | ||
incorrectAnswerId: 0 | ||
} | ||
|
||
const questionStates = formatAnswerData(exercise.questions).reduce((acc, answer) => { | ||
const { id, correct_answer_id } = answer; | ||
return { ...acc, [id]: { ...questionStateFields, correct_answer_id } }; | ||
}, {}); | ||
|
||
const step: StepBase = { | ||
id: 1, | ||
uid: exercise.uid, | ||
available_points: '1.0', | ||
}; | ||
|
||
return { | ||
canAnswer: true, | ||
needsSaved: true, | ||
hasMultipleAttempts: false, | ||
onAnswerChange: () => undefined, | ||
onAnswerSave: () => undefined, | ||
onNextStep: () => undefined, | ||
apiIsPending: false, | ||
canUpdateCurrentStep: false, | ||
step: step, | ||
questionNumber: exercise.number as number, | ||
numberOfQuestions: exercise.questions.length, | ||
questionStates: questionStates, | ||
show_all_feedback: false, // Hide all feedback | ||
}; | ||
}; | ||
|
||
const includeRemoveQuestionComponent = React.useMemo(() => | ||
<IncludeRemoveQuestion | ||
buttonVariant={selected ? 'remove' : 'include'} | ||
onIncludeHandler={onIncludeHandler} | ||
onRemoveHandler={onRemoveHandler} | ||
onClickDetails={onClickDetails} | ||
/> | ||
, [selected, onIncludeHandler, onRemoveHandler, onClickDetails]); | ||
|
||
return ( | ||
<Exercise | ||
exercise={exercise} | ||
className={selected ? 'preview-card is-selected' : 'preview-card'} | ||
{ | ||
...(enableOverlay | ||
? { | ||
overlayChildren: includeRemoveQuestionComponent, | ||
} | ||
: {}) | ||
} | ||
{...exercisePreviewProps(exercise)} | ||
/> | ||
); | ||
}; |
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
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.
I'd prefer this to take
selected
andonSelectionChange
props, that keeps it flexible on the consuming side