resolved PR feedback and solved possible bug

This commit is contained in:
lotte
2019-05-31 13:10:47 +02:00
parent b352137fb3
commit 16ebaf1bfe
6 changed files with 108 additions and 25 deletions

View File

@@ -129,23 +129,22 @@ export class SearchFilterComponent implements OnInit {
* @returns {Observable<boolean>} Emits true whenever a given filter config should be shown
*/
private isActive(): Observable<boolean> {
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));
}
}

View File

@@ -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
];

View File

@@ -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;

View File

@@ -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<ResetRouteStateAction>}
*/
@Effect() routeChange$ = this.actions$
.pipe(
ofType(fromRouter.ROUTER_NAVIGATION),
map(() => new ResetRouteStateAction())
);
constructor(private actions$: Actions) {
}
}

View File

@@ -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 });
}

View File

@@ -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<CoreState, string> {
return createSelector(paramsSelector, (state: Params) => {
if (hasValue(state)) {