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 * @returns {Observable<boolean>} Emits true whenever a given filter config should be shown
*/ */
private isActive(): Observable<boolean> { private isActive(): Observable<boolean> {
return this.selectedValues$.pipe(
switchMap((isActive) => {
if (isNotEmpty(isActive)) {
return observableOf(true); return observableOf(true);
// return this.selectedValues$.pipe( } else {
// switchMap((isActive) => { return this.searchConfigService.searchOptions.pipe(
// if (isNotEmpty(isActive)) { switchMap((options) => {
// return observableOf(true); return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe(
// } else { filter((RD) => !RD.isLoading),
// return this.searchConfigService.searchOptions.pipe( map((valuesRD) => {
// switchMap((options) => { return valuesRD.payload.totalElements > 0
// return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe( }),)
// filter((RD) => !RD.isLoading), }
// map((valuesRD) => { ))
// return valuesRD.payload.totalElements > 0 }
// }),) }),
// } startWith(true));
// ))
// }
// }),
// startWith(true));
} }
} }

View File

@@ -6,6 +6,7 @@ import { AuthEffects } from './auth/auth.effects';
import { JsonPatchOperationsEffects } from './json-patch/json-patch-operations.effects'; import { JsonPatchOperationsEffects } from './json-patch/json-patch-operations.effects';
import { ServerSyncBufferEffects } from './cache/server-sync-buffer.effects'; import { ServerSyncBufferEffects } from './cache/server-sync-buffer.effects';
import { ObjectUpdatesEffects } from './data/object-updates/object-updates.effects'; import { ObjectUpdatesEffects } from './data/object-updates/object-updates.effects';
import { RouteEffects } from '../shared/services/route.effects';
export const coreEffects = [ export const coreEffects = [
RequestEffects, RequestEffects,
@@ -14,5 +15,6 @@ export const coreEffects = [
AuthEffects, AuthEffects,
JsonPatchOperationsEffects, JsonPatchOperationsEffects,
ServerSyncBufferEffects, ServerSyncBufferEffects,
ObjectUpdatesEffects ObjectUpdatesEffects,
RouteEffects
]; ];

View File

@@ -10,6 +10,7 @@ export const RouteActionTypes = {
SET_PARAMETERS: type('dspace/core/route/SET_PARAMETERS'), SET_PARAMETERS: type('dspace/core/route/SET_PARAMETERS'),
ADD_QUERY_PARAMETER: type('dspace/core/route/ADD_QUERY_PARAMETER'), ADD_QUERY_PARAMETER: type('dspace/core/route/ADD_QUERY_PARAMETER'),
ADD_PARAMETER: type('dspace/core/route/ADD_PARAMETER'), ADD_PARAMETER: type('dspace/core/route/ADD_PARAMETER'),
RESET: type('dspace/core/route/RESET'),
}; };
/* tslint:disable:max-classes-per-file */ /* 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 */ /* tslint:enable:max-classes-per-file */
/** /**
* A type to encompass all RouteActions * 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 { import {
AddParameterAction, AddParameterAction,
AddQueryParameterAction, AddQueryParameterAction,
RouteAction, RouteActions,
RouteActionTypes, SetParametersAction, SetQueryParametersAction 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 { export interface RouteState {
queryParams: Params; queryParams: Params;
params: Params; params: Params;
} }
/**
* The initial route state
*/
const initialState: RouteState = { const initialState: RouteState = {
queryParams: {}, queryParams: {},
params: {} 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) { switch (action.type) {
case RouteActionTypes.RESET: {
return initialState
}
case RouteActionTypes.SET_PARAMETERS: { case RouteActionTypes.SET_PARAMETERS: {
return setParameters(state, action as SetParametersAction, 'params'); 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 { function addParameter(state: RouteState, action: AddParameterAction | AddQueryParameterAction, paramType: string): RouteState {
const subState = state[paramType]; const subState = state[paramType];
const existingValues = subState[action.payload.key] || []; 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 }); const newSubstate = Object.assign(subState, { [action.payload.key]: newValues });
return Object.assign({}, state, { [paramType]: newSubstate }); 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 { function setParameters(state: RouteState, action: SetParametersAction | SetQueryParametersAction, paramType: string): RouteState {
return Object.assign({}, state, { [paramType]: action.payload }); return Object.assign({}, state, { [paramType]: action.payload });
} }

View File

@@ -14,23 +14,44 @@ import { isEqual } from 'lodash';
import { AddUrlToHistoryAction } from '../history/history.actions'; import { AddUrlToHistoryAction } from '../history/history.actions';
import { historySelector } from '../history/selectors'; import { historySelector } from '../history/selectors';
import { SetParametersAction, SetQueryParametersAction } from './route.action'; import { SetParametersAction, SetQueryParametersAction } from './route.actions';
import { CoreState } from '../../core/core.reducers'; import { CoreState } from '../../core/core.reducers';
import { hasValue } from '../empty.util'; import { hasValue } from '../empty.util';
import { coreSelector } from '../../core/core.selectors'; import { coreSelector } from '../../core/core.selectors';
/**
* Selector to select all route parameters from the store
*/
export const routeParametersSelector = createSelector( export const routeParametersSelector = createSelector(
coreSelector, coreSelector,
(state: CoreState) => state.route.params (state: CoreState) => state.route.params
); );
/**
* Selector to select all query parameters from the store
*/
export const queryParametersSelector = createSelector( export const queryParametersSelector = createSelector(
coreSelector, coreSelector,
(state: CoreState) => state.route.queryParams (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); 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); 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> { export function parameterSelector(key: string, paramsSelector: (state: CoreState) => Params): MemoizedSelector<CoreState, string> {
return createSelector(paramsSelector, (state: Params) => { return createSelector(paramsSelector, (state: Params) => {
if (hasValue(state)) { if (hasValue(state)) {