From 6e76c0d48e00baa4c6c7318a1752f3c0151b5d22 Mon Sep 17 00:00:00 2001 From: Lorenz Hilpert Date: Thu, 17 Sep 2020 14:40:13 +0200 Subject: [PATCH] #975 cleanup search results --- .../customer/shelf/details/details.actions.ts | 2 +- .../customer/shelf/search/search.actions.ts | 2 + .../customer/shelf/search/search.effects.ts | 104 +++++------------- .../customer/shelf/search/search.reducer.ts | 82 +++++--------- 4 files changed, 61 insertions(+), 129 deletions(-) diff --git a/apps/sales/src/app/store/customer/shelf/details/details.actions.ts b/apps/sales/src/app/store/customer/shelf/details/details.actions.ts index e4cacf02d..f176bcbea 100644 --- a/apps/sales/src/app/store/customer/shelf/details/details.actions.ts +++ b/apps/sales/src/app/store/customer/shelf/details/details.actions.ts @@ -6,7 +6,7 @@ import { ResponseArgsOfOrderItemSubsetDTO, } from '@swagger/oms'; -const prefix = '[CUSTOMER] [SHELF] [SEARCH]'; +const prefix = '[CUSTOMER] [SHELF] [DETAILS]'; export const fetchDetailsByOrderNumber = createAction(`${prefix} Fetch Details By Order Number`, props<{ orderNumber: string }>()); diff --git a/apps/sales/src/app/store/customer/shelf/search/search.actions.ts b/apps/sales/src/app/store/customer/shelf/search/search.actions.ts index 68c3eab75..9b89e7c78 100644 --- a/apps/sales/src/app/store/customer/shelf/search/search.actions.ts +++ b/apps/sales/src/app/store/customer/shelf/search/search.actions.ts @@ -50,6 +50,8 @@ export const setTimestamp = createAction(`${prefix} Set Timestamp`, props<{ id: export const fetchResult = createAction(`${prefix} Fetch Result`, props<{ id: number; skip?: number; take?: number }>()); +export const cleanup = createAction(`${prefix} Cleanup`); + export const fetchResultDone = createAction( `${prefix} Fetch Result Done`, props<{ diff --git a/apps/sales/src/app/store/customer/shelf/search/search.effects.ts b/apps/sales/src/app/store/customer/shelf/search/search.effects.ts index 19e9d6b6f..6708601d5 100644 --- a/apps/sales/src/app/store/customer/shelf/search/search.effects.ts +++ b/apps/sales/src/app/store/customer/shelf/search/search.effects.ts @@ -1,19 +1,10 @@ import { Injectable } from '@angular/core'; -import { Store } from '@ngrx/store'; -import { Actions, createEffect, ofType } from '@ngrx/effects'; +import { Action, Store } from '@ngrx/store'; +import { Actions, createEffect, ofType, OnInitEffects } from '@ngrx/effects'; import { Actions as NgxsActions, ofActionDispatched } from '@ngxs/store'; -import { - AddProcess, - DeleteProcess, -} from 'apps/sales/src/app/core/store/actions/process.actions'; +import { AddProcess, DeleteProcess } from 'apps/sales/src/app/core/store/actions/process.actions'; import * as actions from './search.actions'; -import { - switchMap, - withLatestFrom, - catchError, - map, - flatMap, -} from 'rxjs/operators'; +import { switchMap, withLatestFrom, catchError, map, flatMap } from 'rxjs/operators'; import { StrictHttpResponse, ListResponseArgsOfOrderItemListItemDTO, @@ -22,16 +13,13 @@ import { } from '@swagger/oms'; import { SearchStateFacade } from './search.facade'; import { of, NEVER } from 'rxjs'; -import { - inputDtoToSelectedFilters, - inputDtoToPrimaryFilterOption, -} from './mappers'; +import { inputDtoToSelectedFilters, inputDtoToPrimaryFilterOption } from './mappers'; import { SelectFilter } from 'apps/sales/src/app/modules/filter'; import { PrimaryFilterOption } from 'apps/sales/src/app/modules/shelf/defs'; import { isNullOrUndefined, isNumber } from 'util'; @Injectable() -export class SearchEffects { +export class SearchEffects implements OnInitEffects { constructor( private actions$: Actions, private ngxsActions$: NgxsActions, @@ -55,10 +43,7 @@ export class SearchEffects { ofType(actions.fetchResult), switchMap((a) => of(a).pipe( - withLatestFrom( - this.searchStateFacade.getProcess$(a.id), - this.searchStateFacade.selectedFilter$ - ), + withLatestFrom(this.searchStateFacade.getProcess$(a.id), this.searchStateFacade.selectedFilter$), flatMap(([_, process, filter]) => this.abholfach .AbholfachWarenausgabeResponse({ @@ -72,14 +57,8 @@ export class SearchEffects { take: isNumber(a.take) ? a.take : 20, }) .pipe( - catchError((err) => - of< - StrictHttpResponse - >(err) - ), - map((response) => - actions.fetchResultDone({ id: a.id, response }) - ) + catchError((err) => of>(err)), + map((response) => actions.fetchResultDone({ id: a.id, response })) ) ) ) @@ -95,9 +74,7 @@ export class SearchEffects { const result = action.response.body; const shouldSetTimestamp = !!result.skip; return [ - ...(shouldSetTimestamp - ? [actions.setTimestamp({ id: action.id })] - : []), + ...(shouldSetTimestamp ? [actions.setTimestamp({ id: action.id })] : []), actions.setHits({ id: action.id, hits: result.hits || 0 }), actions.addResult({ id: action.id, result: result.result || [] }), ]; @@ -109,12 +86,7 @@ export class SearchEffects { clearResult$ = createEffect(() => { return this.actions$.pipe( - ofType( - actions.resetPrimaryFilters, - actions.resetSelectFilters, - actions.setSelectFilters, - actions.setInput - ), + ofType(actions.resetPrimaryFilters, actions.resetSelectFilters, actions.setSelectFilters, actions.setInput), map((action) => actions.clearResults({ id: action.id })) ); }); @@ -124,12 +96,8 @@ export class SearchEffects { ofType(actions.fetchFilters), flatMap((action) => this.abholfach.AbholfachGetWarenausgabeFilterResponse().pipe( - catchError((err) => - of>(err) - ), - map((response) => - actions.fetchFiltersDone({ id: action.id, response }) - ) + catchError((err) => of>(err)), + map((response) => actions.fetchFiltersDone({ id: action.id, response })) ) ) ) @@ -142,20 +110,12 @@ export class SearchEffects { if (action.response.ok) { const result = action.response.body; - const selectedFiltersRaw = result.result.filter( - (filter) => !!filter.options - ); - const primaryFiltersRaw = result.result.filter((filter) => - isNullOrUndefined(filter.options) - ); + const selectedFiltersRaw = result.result.filter((filter) => !!filter.options); + const primaryFiltersRaw = result.result.filter((filter) => isNullOrUndefined(filter.options)); - const selectFilters: SelectFilter[] = selectedFiltersRaw.map( - (input) => inputDtoToSelectedFilters(input) - ); + const selectFilters: SelectFilter[] = selectedFiltersRaw.map((input) => inputDtoToSelectedFilters(input)); - const primaryFilters: PrimaryFilterOption[] = primaryFiltersRaw.map( - (input) => inputDtoToPrimaryFilterOption(input) - ); + const primaryFilters: PrimaryFilterOption[] = primaryFiltersRaw.map((input) => inputDtoToPrimaryFilterOption(input)); return [ actions.setSelectFilters({ @@ -175,26 +135,22 @@ export class SearchEffects { ); initAddProcess() { - this.ngxsActions$ - .pipe(ofActionDispatched(AddProcess)) - .subscribe((action) => { - if (action instanceof AddProcess) { - this.store.dispatch( - actions.addSearchProcess({ id: action.payload.id }) - ); + this.ngxsActions$.pipe(ofActionDispatched(AddProcess)).subscribe((action) => { + if (action instanceof AddProcess) { + this.store.dispatch(actions.addSearchProcess({ id: action.payload.id })); - this.searchStateFacade.fetchFilters(action.payload.id); - } - }); + this.searchStateFacade.fetchFilters(action.payload.id); + } + }); + } + + ngrxOnInitEffects(): Action { + return actions.cleanup(); } initRemoveProcess() { - this.ngxsActions$ - .pipe(ofActionDispatched(DeleteProcess)) - .subscribe((action: DeleteProcess) => { - this.store.dispatch( - actions.removeSearchProcess({ id: action.payload.id }) - ); - }); + this.ngxsActions$.pipe(ofActionDispatched(DeleteProcess)).subscribe((action: DeleteProcess) => { + this.store.dispatch(actions.removeSearchProcess({ id: action.payload.id })); + }); } } diff --git a/apps/sales/src/app/store/customer/shelf/search/search.reducer.ts b/apps/sales/src/app/store/customer/shelf/search/search.reducer.ts index 12d90f283..7f17430bf 100644 --- a/apps/sales/src/app/store/customer/shelf/search/search.reducer.ts +++ b/apps/sales/src/app/store/customer/shelf/search/search.reducer.ts @@ -1,24 +1,19 @@ import { createReducer, Action, on } from '@ngrx/store'; -import { - INITIAL_SEARCH_STATE, - SearchState, - searchStateAdapter, - INITIAL_SEARCH_PROCESS, -} from './search.state'; +import { INITIAL_SEARCH_STATE, SearchState, searchStateAdapter, INITIAL_SEARCH_PROCESS } from './search.state'; import * as actions from './search.actions'; import { SearchProcessState } from './defs'; const _searchReducer = createReducer( INITIAL_SEARCH_STATE, - on(actions.addSearchProcess, (s, a) => - searchStateAdapter.addOne({ ...INITIAL_SEARCH_PROCESS, id: a.id }, s) - ), - on(actions.removeSearchProcess, (s, a) => - searchStateAdapter.removeOne(a.id, s) - ), - on(actions.setInput, (s, a) => - searchStateAdapter.updateOne({ id: a.id, changes: { input: a.input } }, s) + on(actions.cleanup, (s) => + Object.values(s.entities).reduce( + (state, process) => searchStateAdapter.updateOne({ id: process.id, changes: { result: [] } }, state), + s + ) ), + on(actions.addSearchProcess, (s, a) => searchStateAdapter.addOne({ ...INITIAL_SEARCH_PROCESS, id: a.id }, s)), + on(actions.removeSearchProcess, (s, a) => searchStateAdapter.removeOne(a.id, s)), + on(actions.setInput, (s, a) => searchStateAdapter.updateOne({ id: a.id, changes: { input: a.input } }, s)), on(actions.setSelectFilters, (s, a) => searchStateAdapter.updateOne( { @@ -51,19 +46,17 @@ const _searchReducer = createReducer( changes: { filters: { ...s.entities[a.id].filters, - selectedFilters: s.entities[a.id].filters.selectedFilters.map( - (filter) => { - let options = filter.options || []; - if (filter.options) { - options = filter.options.map((o) => ({ - ...o, - selected: o.initial_selected_state, - })); - } - - return { ...filter, options }; + selectedFilters: s.entities[a.id].filters.selectedFilters.map((filter) => { + let options = filter.options || []; + if (filter.options) { + options = filter.options.map((o) => ({ + ...o, + selected: o.initial_selected_state, + })); } - ), + + return { ...filter, options }; + }), }, }, }, @@ -77,12 +70,10 @@ const _searchReducer = createReducer( changes: { filters: { ...s.entities[a.id].filters, - primaryFilters: s.entities[a.id].filters.primaryFilters.map( - (primaryFilter) => ({ - ...primaryFilter, - selected: primaryFilter.initial_selected_state, - }) - ), + primaryFilters: s.entities[a.id].filters.primaryFilters.map((primaryFilter) => ({ + ...primaryFilter, + selected: primaryFilter.initial_selected_state, + })), }, }, }, @@ -101,10 +92,7 @@ const _searchReducer = createReducer( ) ), on(actions.clearResults, (s, a) => - searchStateAdapter.updateOne( - { id: a.id, changes: { result: [], state: SearchProcessState.INIT } }, - s - ) + searchStateAdapter.updateOne({ id: a.id, changes: { result: [], state: SearchProcessState.INIT } }, s) ), on(actions.addResult, (s, a) => { const addSubsetIds = a.result.map((item) => item.orderItemSubsetId); @@ -123,12 +111,8 @@ const _searchReducer = createReducer( s ); }), - on(actions.clearHits, (s, a) => - searchStateAdapter.updateOne({ id: a.id, changes: { hits: undefined } }, s) - ), - on(actions.setHits, (s, a) => - searchStateAdapter.updateOne({ id: a.id, changes: { hits: a.hits } }, s) - ), + on(actions.clearHits, (s, a) => searchStateAdapter.updateOne({ id: a.id, changes: { hits: undefined } }, s)), + on(actions.setHits, (s, a) => searchStateAdapter.updateOne({ id: a.id, changes: { hits: a.hits } }, s)), on(actions.setTimestamp, (s, a) => searchStateAdapter.updateOne( { @@ -138,18 +122,8 @@ const _searchReducer = createReducer( s ) ), - on(actions.fetchResult, (s, a) => - searchStateAdapter.updateOne( - { id: a.id, changes: { state: SearchProcessState.FETCHING } }, - s - ) - ), - on(actions.fetchResultDone, (s, a) => - searchStateAdapter.updateOne( - { id: a.id, changes: { state: SearchProcessState.FETCHED } }, - s - ) - ), + on(actions.fetchResult, (s, a) => searchStateAdapter.updateOne({ id: a.id, changes: { state: SearchProcessState.FETCHING } }, s)), + on(actions.fetchResultDone, (s, a) => searchStateAdapter.updateOne({ id: a.id, changes: { state: SearchProcessState.FETCHED } }, s)), on(actions.setPendingInput, (s, a) => searchStateAdapter.updateOne( {