Skip to content

Commit 1c5017d

Browse files
committed
execute search from url when present
1 parent 933cd8e commit 1c5017d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

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
}

0 commit comments

Comments
 (0)