Skip to content
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

Allow multiple searches in the state #93

Merged
merged 12 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions libs/search/src/lib/state/effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const initialStateMock = {
aggregations: ES_FIXTURE_AGGS_REQUEST,
},
},
main: {
...initialStateSearchMock,
},
}

const searchServiceMock = {
Expand Down Expand Up @@ -123,11 +126,11 @@ describe('Effects', () => {
})
it('clear results list on setSearch action', () => {
actions$ = hot('-a---', {
a: new SetSearch({ filters: { any: 'abcd' } }),
a: new SetSearch({ filters: { any: 'abcd' } }, 'main'),
})
const expected = hot('-(bc)', {
b: new ClearResults(),
c: new RequestMoreResults(),
b: new ClearResults('main'),
c: new RequestMoreResults('main'),
})

expect(effects.clearResults$).toBeObservable(expected)
Expand All @@ -136,9 +139,9 @@ describe('Effects', () => {

describe('scroll$', () => {
it('clear results list on sortBy action', () => {
actions$ = hot('-a---', { a: new Scroll() })
actions$ = hot('-a---', { a: new Scroll('main') })
const expected = hot('-(b)', {
b: new RequestMoreResults(),
b: new RequestMoreResults('main'),
})
expect(effects.scroll$).toBeObservable(expected)
})
Expand All @@ -155,6 +158,17 @@ describe('Effects', () => {

expect(effects.loadResults$).toBeObservable(expected)
})

it('propagate action search id', () => {
actions$ = hot('-a-', { a: new RequestMoreResults('main') })
const expected = hot('-(bcd)-', {
b: new AddResults([], 'main'),
c: new SetResultsAggregations({ abc: {} }, 'main'),
d: new SetResultsHits(undefined, 'main'),
})

expect(effects.loadResults$).toBeObservable(expected)
})
})

describe('loadMoreOnAggregation$', () => {
Expand Down
140 changes: 82 additions & 58 deletions libs/search/src/lib/state/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ import { Actions, createEffect, ofType } from '@ngrx/effects'
import { select, Store } from '@ngrx/store'
import { SearchResponse } from 'elasticsearch'
import { of } from 'rxjs'
import { map, switchMap, tap, withLatestFrom } from 'rxjs/operators'
import { map, switchMap, withLatestFrom } from 'rxjs/operators'
import { ElasticsearchMetadataModels } from '../elasticsearch/constant'
import { ElasticsearchMapper } from '../elasticsearch/elasticsearch.mapper'
import { ElasticsearchService } from '../elasticsearch/elasticsearch.service'
import {
AddResults,
ClearResults,
RequestMoreOnAggregation,
RequestMoreResults,
PAGINATE,
PatchResultsAggregations,
REQUEST_MORE_ON_AGGREGATION,
REQUEST_MORE_RESULTS,
SetResultsAggregations,
SET_SORT_BY,
UPDATE_FILTERS,
RequestMoreOnAggregation,
RequestMoreResults,
SCROLL,
SearchActions,
SET_FILTERS,
SET_INCLUDE_ON_AGGREGATION,
SET_PAGINATION,
SET_SEARCH,
SET_SORT_BY,
SetIncludeOnAggregation,
SetResultsAggregations,
SetResultsHits,
PatchResultsAggregations,
UPDATE_FILTERS,
UPDATE_REQUEST_AGGREGATION_TERM,
UpdateRequestAggregationTerm,
SET_INCLUDE_ON_AGGREGATION,
SetIncludeOnAggregation,
PAGINATE,
SCROLL,
SET_PAGINATION,
} from './actions'
import { SearchState } from './reducer'
import { getSearchState, getSearchStateSearch } from './selectors'
import { getSearchStateSearch } from './selectors'

@Injectable()
export class SearchEffects {
Expand All @@ -55,53 +56,67 @@ export class SearchEffects {
SET_PAGINATION,
PAGINATE
),
switchMap(() => of(new ClearResults(), new RequestMoreResults()))
switchMap((action: SearchActions) =>
of(new ClearResults(action.id), new RequestMoreResults(action.id))
)
)
)

scroll$ = createEffect(() =>
this.actions$.pipe(
ofType(SCROLL),
map(() => new RequestMoreResults())
map((action: SearchActions) => new RequestMoreResults(action.id))
)
)

loadResults$ = createEffect(() =>
this.actions$.pipe(
ofType(REQUEST_MORE_RESULTS),
switchMap(() => this.authService.authReady()), // wait for auth to be known
withLatestFrom(this.store$.pipe(select(getSearchStateSearch))),
switchMap(([_, state]) =>
this.searchService.search(
'bucket',
JSON.stringify(
this.esService.search(state, ElasticsearchMetadataModels.SUMMARY)
)
)
),
switchMap((response: SearchResponse<any>) => {
const records = this.esMapper.toRecordSummary(
response,
this.searchService.configuration.basePath
switchMap((action: SearchActions) =>
this.authService.authReady().pipe(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it intentional to pipe all the work inside the authReady stream ? Why not go back to the actions$ stream like before ?

withLatestFrom(
this.store$.pipe(select(getSearchStateSearch, action.id))
),
switchMap(([_, state]) =>
this.searchService.search(
'bucket',
JSON.stringify(
this.esService.search(
state,
ElasticsearchMetadataModels.SUMMARY
)
)
)
),
switchMap((response: SearchResponse<any>) => {
const records = this.esMapper.toRecordSummary(
response,
this.searchService.configuration.basePath
)
const aggregations = response.aggregations
return [
new AddResults(records, action.id),
new SetResultsAggregations(aggregations, action.id),
new SetResultsHits(response.hits.total, action.id),
]
})
)
const aggregations = response.aggregations
return [
new AddResults(records),
new SetResultsAggregations(aggregations),
new SetResultsHits(response.hits.total),
]
})
) // wait for auth to be known
)
)

loadMoreOnAggregation$ = createEffect(() => {
return this.actions$.pipe(
ofType<RequestMoreOnAggregation>(REQUEST_MORE_ON_AGGREGATION),
switchMap((action) =>
switchMap((action: RequestMoreOnAggregation) =>
of(
new UpdateRequestAggregationTerm(action.key, {
increment: action.increment,
})
new UpdateRequestAggregationTerm(
action.key,
{
increment: action.increment,
},
action.id
)
)
)
)
Expand All @@ -112,36 +127,45 @@ export class SearchEffects {
ofType<SetIncludeOnAggregation>(SET_INCLUDE_ON_AGGREGATION),
switchMap((action) =>
of(
new UpdateRequestAggregationTerm(action.key, {
include: action.include,
})
new UpdateRequestAggregationTerm(
action.key,
{
include: action.include,
},
action.id
)
)
)
)
})

upateRequestAggregationTerm$ = createEffect(() => {
let aggregationKey
const updateTermAction$ = this.actions$.pipe(
ofType<UpdateRequestAggregationTerm>(UPDATE_REQUEST_AGGREGATION_TERM)
)

return updateTermAction$.pipe(
tap((action) => (aggregationKey = action.key)),
switchMap(() => this.authService.authReady()), // wait for auth to be known
withLatestFrom(this.store$.pipe(select(getSearchStateSearch))),
switchMap(([_, state]) =>
this.searchService.search(
'bucket',
JSON.stringify(
this.esService.buildMoreOnAggregationPayload(state, aggregationKey)
)
switchMap((action) =>
this.authService.authReady().pipe(
withLatestFrom(this.store$.pipe(select(getSearchStateSearch))),
switchMap(([_, state]) =>
this.searchService.search(
'bucket',
JSON.stringify(
this.esService.buildMoreOnAggregationPayload(state, action.key)
)
)
),
map((response: SearchResponse<any>) => {
const aggregations = response.aggregations
return new PatchResultsAggregations(
action.key,
aggregations,
action.id
)
})
)
),
map((response: SearchResponse<any>) => {
const aggregations = response.aggregations
return new PatchResultsAggregations(aggregationKey, aggregations)
})
) // wait for auth to be known
)
})
}