From 16ebaf1bfe3bca41ec0ba8b8e847e6c81b2ef948 Mon Sep 17 00:00:00 2001 From: lotte Date: Fri, 31 May 2019 13:10:47 +0200 Subject: [PATCH] resolved PR feedback and solved possible bug --- .../search-filter/search-filter.component.ts | 35 +++++++++---------- src/app/core/core.effects.ts | 4 ++- .../{route.action.ts => route.actions.ts} | 15 +++++++- src/app/shared/services/route.effects.ts | 23 ++++++++++++ src/app/shared/services/route.reducer.ts | 33 ++++++++++++++--- src/app/shared/services/route.service.ts | 23 +++++++++++- 6 files changed, 108 insertions(+), 25 deletions(-) rename src/app/shared/services/{route.action.ts => route.actions.ts} (86%) create mode 100644 src/app/shared/services/route.effects.ts diff --git a/src/app/+search-page/search-filters/search-filter/search-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-filter.component.ts index 1e3270a7d0..bfe9f3be63 100644 --- a/src/app/+search-page/search-filters/search-filter/search-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-filter.component.ts @@ -129,23 +129,22 @@ export class SearchFilterComponent implements OnInit { * @returns {Observable} Emits true whenever a given filter config should be shown */ private isActive(): Observable { - return observableOf(true); - // return this.selectedValues$.pipe( - // switchMap((isActive) => { - // if (isNotEmpty(isActive)) { - // return observableOf(true); - // } else { - // return this.searchConfigService.searchOptions.pipe( - // switchMap((options) => { - // return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe( - // filter((RD) => !RD.isLoading), - // map((valuesRD) => { - // return valuesRD.payload.totalElements > 0 - // }),) - // } - // )) - // } - // }), - // startWith(true)); + return this.selectedValues$.pipe( + switchMap((isActive) => { + if (isNotEmpty(isActive)) { + return observableOf(true); + } else { + return this.searchConfigService.searchOptions.pipe( + switchMap((options) => { + return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe( + filter((RD) => !RD.isLoading), + map((valuesRD) => { + return valuesRD.payload.totalElements > 0 + }),) + } + )) + } + }), + startWith(true)); } } diff --git a/src/app/core/core.effects.ts b/src/app/core/core.effects.ts index bb25c49a7a..9ade23e6c5 100644 --- a/src/app/core/core.effects.ts +++ b/src/app/core/core.effects.ts @@ -6,6 +6,7 @@ import { AuthEffects } from './auth/auth.effects'; import { JsonPatchOperationsEffects } from './json-patch/json-patch-operations.effects'; import { ServerSyncBufferEffects } from './cache/server-sync-buffer.effects'; import { ObjectUpdatesEffects } from './data/object-updates/object-updates.effects'; +import { RouteEffects } from '../shared/services/route.effects'; export const coreEffects = [ RequestEffects, @@ -14,5 +15,6 @@ export const coreEffects = [ AuthEffects, JsonPatchOperationsEffects, ServerSyncBufferEffects, - ObjectUpdatesEffects + ObjectUpdatesEffects, + RouteEffects ]; diff --git a/src/app/shared/services/route.action.ts b/src/app/shared/services/route.actions.ts similarity index 86% rename from src/app/shared/services/route.action.ts rename to src/app/shared/services/route.actions.ts index fe54db02e2..968319d260 100644 --- a/src/app/shared/services/route.action.ts +++ b/src/app/shared/services/route.actions.ts @@ -10,6 +10,7 @@ export const RouteActionTypes = { SET_PARAMETERS: type('dspace/core/route/SET_PARAMETERS'), ADD_QUERY_PARAMETER: type('dspace/core/route/ADD_QUERY_PARAMETER'), ADD_PARAMETER: type('dspace/core/route/ADD_PARAMETER'), + RESET: type('dspace/core/route/RESET'), }; /* tslint:disable:max-classes-per-file */ @@ -95,9 +96,21 @@ export class AddParameterAction implements Action { } } +/** + * An ngrx action to reset the route state + */ +export class ResetRouteStateAction implements Action { + type = RouteActionTypes.RESET; +} + /* tslint:enable:max-classes-per-file */ /** * A type to encompass all RouteActions */ -export type RouteAction = SetQueryParametersAction | SetParametersAction | AddQueryParameterAction | AddParameterAction; +export type RouteActions = + SetQueryParametersAction + | SetParametersAction + | AddQueryParameterAction + | AddParameterAction + | ResetRouteStateAction; diff --git a/src/app/shared/services/route.effects.ts b/src/app/shared/services/route.effects.ts new file mode 100644 index 0000000000..687f8f9921 --- /dev/null +++ b/src/app/shared/services/route.effects.ts @@ -0,0 +1,23 @@ +import { map } from 'rxjs/operators'; +import { Injectable } from '@angular/core'; +import { Actions, Effect, ofType } from '@ngrx/effects' +import * as fromRouter from '@ngrx/router-store'; +import { ResetRouteStateAction } from './route.actions'; + +@Injectable() +export class RouteEffects { + /** + * Effect that resets the route state on reroute + * @type {Observable} + */ + @Effect() routeChange$ = this.actions$ + .pipe( + ofType(fromRouter.ROUTER_NAVIGATION), + map(() => new ResetRouteStateAction()) + ); + + constructor(private actions$: Actions) { + + } + +} diff --git a/src/app/shared/services/route.reducer.ts b/src/app/shared/services/route.reducer.ts index 37fe06283b..b078521c11 100644 --- a/src/app/shared/services/route.reducer.ts +++ b/src/app/shared/services/route.reducer.ts @@ -2,22 +2,36 @@ import { Params } from '@angular/router'; import { AddParameterAction, AddQueryParameterAction, - RouteAction, + RouteActions, RouteActionTypes, SetParametersAction, SetQueryParametersAction -} from './route.action'; +} from './route.actions'; +/** + * Interface to represent the parameter state of a current route in the store + */ export interface RouteState { queryParams: Params; params: Params; } +/** + * The initial route state + */ const initialState: RouteState = { queryParams: {}, params: {} }; -export function routeReducer(state = initialState, action: RouteAction): RouteState { +/** + * Reducer function to save the current route parameters and query parameters in the store + * @param state The current or initial state + * @param action The action to perform on the state + */ +export function routeReducer(state = initialState, action: RouteActions): RouteState { switch (action.type) { + case RouteActionTypes.RESET: { + return initialState + } case RouteActionTypes.SET_PARAMETERS: { return setParameters(state, action as SetParametersAction, 'params'); } @@ -36,6 +50,12 @@ export function routeReducer(state = initialState, action: RouteAction): RouteSt } } +/** + * Add a route or query parameter in the store + * @param state The current state + * @param action The add action to perform on the current state + * @param paramType The type of parameter to add: route or query parameter + */ function addParameter(state: RouteState, action: AddParameterAction | AddQueryParameterAction, paramType: string): RouteState { const subState = state[paramType]; const existingValues = subState[action.payload.key] || []; @@ -43,7 +63,12 @@ function addParameter(state: RouteState, action: AddParameterAction | AddQueryPa const newSubstate = Object.assign(subState, { [action.payload.key]: newValues }); return Object.assign({}, state, { [paramType]: newSubstate }); } - +/** + * Set a route or query parameter in the store + * @param state The current state + * @param action The set action to perform on the current state + * @param paramType The type of parameter to set: route or query parameter + */ function setParameters(state: RouteState, action: SetParametersAction | SetQueryParametersAction, paramType: string): RouteState { return Object.assign({}, state, { [paramType]: action.payload }); } diff --git a/src/app/shared/services/route.service.ts b/src/app/shared/services/route.service.ts index 785c14b84c..dc626484c1 100644 --- a/src/app/shared/services/route.service.ts +++ b/src/app/shared/services/route.service.ts @@ -14,23 +14,44 @@ import { isEqual } from 'lodash'; import { AddUrlToHistoryAction } from '../history/history.actions'; import { historySelector } from '../history/selectors'; -import { SetParametersAction, SetQueryParametersAction } from './route.action'; +import { SetParametersAction, SetQueryParametersAction } from './route.actions'; import { CoreState } from '../../core/core.reducers'; import { hasValue } from '../empty.util'; import { coreSelector } from '../../core/core.selectors'; +/** + * Selector to select all route parameters from the store + */ export const routeParametersSelector = createSelector( coreSelector, (state: CoreState) => state.route.params ); + +/** + * Selector to select all query parameters from the store + */ export const queryParametersSelector = createSelector( coreSelector, (state: CoreState) => state.route.queryParams ); +/** + * Selector to select a specific route parameter from the store + * @param key The key of the parameter + */ export const routeParameterSelector = (key: string) => parameterSelector(key, routeParametersSelector); + +/** + * Selector to select a specific query parameter from the store + * @param key The key of the parameter + */ export const queryParameterSelector = (key: string) => parameterSelector(key, queryParametersSelector); +/** + * Function to select a specific parameter from the store + * @param key The key to look for + * @param paramsSelector The selector that selects the parameters to search in + */ export function parameterSelector(key: string, paramsSelector: (state: CoreState) => Params): MemoizedSelector { return createSelector(paramsSelector, (state: Params) => { if (hasValue(state)) {