Skip to content

Commit

Permalink
Merge pull request #2600 from flacial/feat/flag-exercise-2
Browse files Browse the repository at this point in the history
student feature: Add the ability to report exercises
  • Loading branch information
flacial authored Dec 11, 2022
2 parents 551c321 + a53a919 commit 4072eed
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 80 deletions.
17 changes: 14 additions & 3 deletions __tests__/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6490,6 +6490,19 @@ a = a + 10
</div>
</div>
</div>
<div>
<button
className="btn newButton container__reportBtn borderless"
onClick={[Function]}
style={
Object {
"color": "mute",
}
}
>
Report a problem
</button>
</div>
</section>
`;

Expand Down Expand Up @@ -6665,9 +6678,7 @@ exports[`Storyshots Components/ExerciseReportCard Basic 1`] = `
<p>
Leave the description empty and try to submit to display success message
</p>
<div
className=""
>
<div>
<button
className="btn newButton container__reportBtn borderless"
onClick={[Function]}
Expand Down
93 changes: 53 additions & 40 deletions components/ExerciseCard/ExerciseCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '@testing-library/jest-dom'
import React from 'react'
import { fireEvent, render } from '@testing-library/react'
import ExerciseCard, { Message } from './ExerciseCard'
import { MockedProvider } from '@apollo/client/testing'

const exampleProblem = `let a = 5
a = a + 10
Expand All @@ -16,16 +17,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.EMPTY}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.EMPTY}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
exerciseId={1}
/>
</MockedProvider>
)

// Test that an error message shows if the user is wrong
Expand All @@ -48,16 +52,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText, getByLabelText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.ERROR}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.ERROR}
setMessage={setMessage}
exerciseId={1}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.ERROR)).toBeInTheDocument()
Expand Down Expand Up @@ -87,16 +94,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { getByRole, queryByText } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={true}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
exerciseId={1}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={true}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.SUCCESS)).toBeInTheDocument()
Expand All @@ -119,16 +129,19 @@ describe('ExerciseCard component', () => {
const submitUserAnswer = jest.fn()

const { queryByText, getByRole } = render(
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
exerciseId={1}
answerShown={false}
setAnswerShown={setAnswerShown}
message={Message.SUCCESS}
setMessage={setMessage}
submitUserAnswer={submitUserAnswer}
/>
</MockedProvider>
)

expect(queryByText(Message.SUCCESS)).toBeInTheDocument()
Expand Down
6 changes: 5 additions & 1 deletion components/ExerciseCard/ExerciseCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Markdown from 'markdown-to-jsx'
import React, { useState } from 'react'
import ExerciseReportCard from '../ExerciseReportCard'
import { NewButton } from '../theme/Button'
import { Text } from '../theme/Text'
import styles from './exerciseCard.module.scss'
Expand All @@ -13,6 +14,7 @@ export type ExerciseCardProps = {
message: Message
setMessage: (message: Message) => void
submitUserAnswer: (userAnswer: string) => void
exerciseId: number
}

export enum Message {
Expand All @@ -29,7 +31,8 @@ const ExerciseCard = ({
setAnswerShown,
message,
setMessage,
submitUserAnswer
submitUserAnswer,
exerciseId
}: ExerciseCardProps) => {
const [studentAnswer, setStudentAnswer] = useState('')

Expand Down Expand Up @@ -102,6 +105,7 @@ const ExerciseCard = ({
</div>
</>
)}
<ExerciseReportCard exerciseId={exerciseId} />
</section>
)
}
Expand Down
4 changes: 2 additions & 2 deletions components/ExerciseReportCard/ExerciseReportCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('ExerciseReportCard Component', () => {

render(
<MockedProvider mocks={mocks}>
<ExerciseReportCard exerciseId={1} answerShown={true} />
<ExerciseReportCard exerciseId={1} />
</MockedProvider>
)

Expand All @@ -54,7 +54,7 @@ describe('ExerciseReportCard Component', () => {

render(
<MockedProvider mocks={mocks}>
<ExerciseReportCard exerciseId={1} answerShown={false} />
<ExerciseReportCard exerciseId={1} />
</MockedProvider>
)

Expand Down
34 changes: 17 additions & 17 deletions components/ExerciseReportCard/ExerciseReportCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ const Body = ({
</Button>
<Button
outline
btnType="info"
color="info"
btnType="mute"
color="mute"
onClick={() => setReportMode(false)}
>
Cancel
Expand All @@ -85,10 +85,9 @@ const Body = ({

type Props = {
exerciseId: number
answerShown: boolean
}

const ExerciseReportCard = ({ exerciseId, answerShown }: Props) => {
const ExerciseReportCard = ({ exerciseId }: Props) => {
const [reportMode, setReportMode] = useState(false)
const [description, setDescription] = useState('')
const [flagExercise, { data, loading, error }] = useFlagExerciseMutation({
Expand All @@ -98,11 +97,9 @@ const ExerciseReportCard = ({ exerciseId, answerShown }: Props) => {
}
})

const marginOnExpand = answerShown ? 'mt-5' : ''

if (data && !loading) {
return (
<div className={`${styles.container} ${marginOnExpand}`}>
<div className={`${styles.container} mt-3`}>
<h6>Reported a mistake in this exercise</h6>
<p className="mb-0">
We appreciate your input. We will shortly investigate the problem that
Expand All @@ -113,17 +110,20 @@ const ExerciseReportCard = ({ exerciseId, answerShown }: Props) => {
}

return (
<div className={marginOnExpand}>
<div>
{reportMode ? (
<Body
data={data}
loading={loading}
error={error}
description={description}
setDescription={setDescription}
flagExercise={flagExercise}
setReportMode={setReportMode}
/>
<>
<hr />
<Body
data={data}
loading={loading}
error={error}
description={description}
setDescription={setDescription}
flagExercise={flagExercise}
setReportMode={setReportMode}
/>
</>
) : (
<Button
className={`${btnStyles.newButton} ${styles.container__reportBtn}`}
Expand Down
6 changes: 2 additions & 4 deletions components/ExerciseReportCard/exerciseReportCard.module.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@use '../../scss/colors.module.scss';

.container {
max-width: 400px;
background-color: hsl(0, 0%, 96%);
padding: 20px;
border: 1px solid hsl(0, 0%, 84%);
Expand All @@ -21,8 +20,7 @@
}

.container__btns__container {
display: grid;
row-gap: 8px;
display: flex;
column-gap: 10px;
margin-top: 18px;
width: 100%;
}
1 change: 1 addition & 0 deletions pages/exercises/[lessonSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const Exercise = ({
})
submitUserAnswer(exercise.id, userAnswer)
}}
exerciseId={exercise.id}
/>
<div className="d-flex justify-content-between mt-4">
{hasPrevious ? (
Expand Down
28 changes: 16 additions & 12 deletions stories/components/ExerciseCard.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MockedProvider } from '@apollo/client/testing'
import React, { useState } from 'react'
import ExerciseCard, { Message } from '../../components/ExerciseCard'

Expand All @@ -19,17 +20,20 @@ export const Basic = () => {
const [message, setMessage] = useState(Message.EMPTY)

return (
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={answerShown}
setAnswerShown={setAnswerShown}
message={message}
setMessage={setMessage}
submitUserAnswer={userAnswer => {
console.log(`User answer submitted: ${userAnswer}`)
}}
/>
<MockedProvider>
<ExerciseCard
problem={exampleProblem}
answer={exampleAnswer}
explanation={exampleExplanation}
answerShown={answerShown}
setAnswerShown={setAnswerShown}
message={message}
setMessage={setMessage}
submitUserAnswer={userAnswer => {
console.log(`User answer submitted: ${userAnswer}`)
}}
exerciseId={1}
/>
</MockedProvider>
)
}
2 changes: 1 addition & 1 deletion stories/components/ExerciseReportCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const Basic = () => {
}
]}
>
<ExerciseReportCard exerciseId={1} answerShown={false} />
<ExerciseReportCard exerciseId={1} />
</MockedProvider>
</div>
)
Expand Down

1 comment on commit 4072eed

@vercel
Copy link

@vercel vercel bot commented on 4072eed Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.