Skip to content

Commit feae7d0

Browse files
authored
Merge pull request #3667 from openstax/ex-context
Render context along with exercise preview
2 parents 328d467 + 1c5017d commit feae7d0

File tree

8 files changed

+31
-2
lines changed

8 files changed

+31
-2
lines changed

exercises/specs/components/__snapshots__/exercise.spec.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,13 @@ exports[`Exercises component renders and matches snapshot 1`] = `
17331733
Author: Alene
17341734
</div>
17351735
</div>
1736+
<div
1737+
dangerouslySetInnerHTML={
1738+
Object {
1739+
"__html": "<h3>read this chapter first</h3>",
1740+
}
1741+
}
1742+
/>
17361743
<div
17371744
className="openstax-exercise-preview exercise-editing-preview is-displaying-formats is-displaying-feedback card"
17381745
data-exercise-id="1@1"
@@ -5049,6 +5056,7 @@ exports[`Exercises component renders with intro and a multiple questions when ex
50495056
Author: Louvenia
50505057
</div>
50515058
</div>
5059+
50525060
<div
50535061
className="openstax-exercise-preview exercise-editing-preview is-displaying-formats is-displaying-feedback card"
50545062
data-exercise-id="9@1"

exercises/specs/components/exercise.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('Exercises component', () => {
2727
});
2828

2929
it('renders and matches snapshot', () => {
30+
exercise.context = '<h3>read this chapter first</h3>'
3031
expect.snapshot(<Router><Exercise {...props} /></Router>).toMatchSnapshot();
3132
});
3233

exercises/src/components/exercise/preview.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ExercisePreview } from 'shared';
55
import { Link } from 'react-router-dom';
66
import { observer } from 'mobx-react';
77
import { CornerRibbon } from 'shared';
8+
import { ArbitraryHtmlAndMath as HTML } from 'shared';
89

910
const Preview = styled.div`
1011
margin-top: 1.5rem;
@@ -43,6 +44,8 @@ class ExerciseEditingPreview extends React.Component {
4344
<div key="n">{exercise.readOnlyReason}</div>,
4445
]}
4546
>
47+
{exercise.context && <HTML html={exercise.context} />}
48+
4649
<ExercisePreview
4750
questionType="teacher-preview"
4851
className='exercise-editing-preview'

exercises/src/components/preview.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { idType } from 'shared';
99
import { Loading, NotFound } from './exercise-state';
1010
import Controls from './preview/controls';
1111
import { ExercisePreview } from 'shared';
12+
import { ArbitraryHtmlAndMath as HTML } from 'shared';
1213

1314
@observer
1415
export default
@@ -52,6 +53,7 @@ class Preview extends React.Component {
5253

5354
return (
5455
<div className="preview-screen">
56+
{exercise.context && <HTML html={exercise.context} />}
5557
<ExercisePreview
5658
displayAllTags
5759
displayFeedback

exercises/src/components/search.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Search extends React.Component<SearchProps> {
4545
this.titleChangeDisposer = autorun(() => {
4646
document.title = this.search.title
4747
})
48+
props.ux.setSearchDefault(props.history.location.search.replace(/^\?/, ''))
4849
}
4950

5051
componentWillUnmount() {

exercises/src/models/search.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { map } from 'lodash';
22
import {
33
BaseModel, model, runInAction, array, hydrateModel, modelize, observable, computed, action, getParentOf,
44
} from 'shared/model';
5+
import { override } from 'mobx'
56
import Exercise from './exercises/exercise';
67
import urlFor from '../api'
78
import pluralize from 'pluralize';
@@ -146,6 +147,11 @@ class Search extends BaseModel {
146147
)
147148
}
148149

150+
@override update(clause: string) {
151+
const [filter, value] = clause.split(':')
152+
this.clauses.replace([{ filter, value } as any])
153+
154+
}
149155
//called by api
150156
@action async perform() {
151157
this.isPending = true

exercises/src/ux.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { lazyGetter } from 'shared/model';
1+
import { lazyGetter, action } from 'shared/model';
2+
import qs from 'qs';
23
import Search from './models/search';
34

45
export default class ExerciseUX {
56

67
@lazyGetter get search() { return new Search() }
78

9+
@action setSearchDefault(queryString: string) {
10+
const { q: query } = qs.parse(queryString)
11+
if (query && typeof query == 'string') {
12+
this.search.update(query)
13+
this.search.execute()
14+
}
15+
}
816
}

shared/src/model/exercise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class SharedExercise extends BaseModel {
2525

2626
@field published_at?: Time | Date
2727
@field stem_html = ''
28-
28+
@field context = '';
2929
@model(Attachment) attachments = array<Attachment>()
3030
@model(Author) authors = array<Author>()
3131
@model(Author) copyright_holders = array<Author>()

0 commit comments

Comments
 (0)