-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Text search epics re-implementation This is a first example of how to implement async functionalities using redux-observable. Added also a test with redux-mock-store. * Search Text now supports nominatim + WFS search * Improved error management * Added loading status * Debouncing of search events * Support cancellation on purge * Merge result and sort by priority * add redux-mock-store to test redux-observable
- Loading branch information
1 parent
f2a8d82
commit 7d03732
Showing
14 changed files
with
544 additions
and
25 deletions.
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
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,34 @@ | ||
/** | ||
* Copyright 2017, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const WFS = require('./WFS'); | ||
const assign = require('object-assign'); | ||
|
||
|
||
const toNominatim = (fc) => | ||
fc.features && fc.features.map( (f) => ({ | ||
boundingbox: f.properties.bbox, | ||
lat: 1, | ||
lon: 1, | ||
display_name: `${f.properties.STATE_NAME} (${f.properties.STATE_ABBR})` | ||
|
||
})); | ||
|
||
|
||
module.exports = { | ||
nominatim: (searchText) => require('./Nominatim').geocode(searchText).then( res => res.data), | ||
wfs: (searchText, {url, typeName, queriableAttributes, outputFormat="application/json", predicate ="ILIKE", ...params }) => { | ||
return WFS.getFeatureSimple(url, | ||
assign({ | ||
maxFeatures: 10, | ||
startIndex: 0, | ||
typeName, | ||
outputFormat, | ||
cql_filter: queriableAttributes.map( attr => `${attr} ${predicate} '%${searchText}%'`).join(' OR ') | ||
}, params)).then( response => toNominatim(response )); | ||
} | ||
}; |
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,54 @@ | ||
|
||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
var expect = require('expect'); | ||
|
||
const configureMockStore = require('redux-mock-store').default; | ||
const { createEpicMiddleware } = require('redux-observable'); | ||
const { searchTextStarted, TEXT_SEARCH_RESULTS_LOADED, TEXT_SEARCH_LOADING } = require('../../actions/search'); | ||
const {searchEpic} = require('../search'); | ||
const epicMiddleware = createEpicMiddleware(searchEpic); | ||
const mockStore = configureMockStore([epicMiddleware]); | ||
|
||
describe('searchEpic', () => { | ||
let store; | ||
beforeEach(() => { | ||
store = mockStore(); | ||
}); | ||
|
||
afterEach(() => { | ||
// nock.cleanAll(); | ||
epicMiddleware.replaceEpic(searchEpic); | ||
}); | ||
|
||
it('produces the search epic', (done) => { | ||
let action = { | ||
...searchTextStarted("TEST"), | ||
services: [{ | ||
type: 'wfs', | ||
options: { | ||
url: 'base/web/client/test-resources/wfs/Wyoming.json', | ||
typeName: 'topp:states', | ||
queriableAttributes: ['STATE_NAME'] | ||
} | ||
}] | ||
}; | ||
|
||
store.dispatch( action ); | ||
|
||
setTimeout(() => { | ||
let actions = store.getActions(); | ||
expect(actions.length).toBe(4); | ||
expect(actions[1].type).toBe(TEXT_SEARCH_LOADING); | ||
expect(actions[2].type).toBe(TEXT_SEARCH_RESULTS_LOADED); | ||
expect(actions[3].type).toBe(TEXT_SEARCH_LOADING); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); |
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,41 @@ | ||
/** | ||
* Copyright 2017, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// var GeoCodingApi = require('../api/Nominatim'); | ||
|
||
const {TEXT_SEARCH_STARTED, TEXT_SEARCH_RESULTS_PURGE, TEXT_SEARCH_RESET, searchTextLoading, searchResultLoaded, searchResultError} = require('../actions/search'); | ||
const Rx = require('rxjs'); | ||
const services = require('../api/searchText'); | ||
|
||
const get = require('lodash'); | ||
const searchEpic = action$ => | ||
action$.ofType(TEXT_SEARCH_STARTED) | ||
.debounceTime(250) | ||
.mergeMap( action => | ||
Rx.Observable.forkJoin( | ||
(action.services || [ {type: "nominatim"} ]) | ||
.map( service => services[service.type](action.searchText, service.options) | ||
.then( (response= []) => response.map(result => ({...result, __SERVICE__: service})).slice(0, service.max || 10 / services.length) ) | ||
) | ||
).concatAll() | ||
// ----[a]------------------ | ||
// --------[c]-------------- | ||
// -------------[b]--------- | ||
.scan( (oldRes, newRes) => [...oldRes, ...newRes].sort( (a, b) => get(a, "_SERVICE__.priority") > get(b, "_SERVICE__.priority") )) | ||
// ----[a]-[a,c]-[a,b,c]----- | ||
.map((results) => searchResultLoaded(results, false)) | ||
.takeUntil(action$.ofType([ TEXT_SEARCH_STARTED, TEXT_SEARCH_RESULTS_PURGE, TEXT_SEARCH_RESET])) | ||
.startWith(searchTextLoading(true)) | ||
.concat([searchTextLoading(false)]) | ||
.catch(e => Rx.Observable.from([searchResultError(e), searchTextLoading(false)])) | ||
|
||
); | ||
|
||
module.exports = { | ||
searchEpic | ||
}; |
Oops, something went wrong.