62530: working on fixing preboot issue with fixed filters/route params

This commit is contained in:
lotte
2019-05-21 17:02:36 +02:00
parent 2d07cc1913
commit 8a5bffa881
8 changed files with 209 additions and 38 deletions

View File

@@ -54,5 +54,4 @@ export class FilteredSearchPageComponent extends SearchPageComponent {
this.searchConfigService.updateFixedFilter(this.fixedFilterQuery);
return this.searchConfigService.paginatedSearchOptions;
}
}

View File

@@ -9,7 +9,7 @@ import {
of as observableOf,
Subscription
} from 'rxjs';
import { filter, flatMap, map } from 'rxjs/operators';
import { filter, flatMap, map, tap } from 'rxjs/operators';
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
import { SearchOptions } from '../search-options.model';
@@ -99,10 +99,8 @@ export class SearchConfigurationService implements OnDestroy {
const defs = defRD.payload;
this.paginatedSearchOptions = new BehaviorSubject<PaginatedSearchOptions>(defs);
this.searchOptions = new BehaviorSubject<SearchOptions>(defs);
this.subs.push(this.subscribeToSearchOptions(defs));
this.subs.push(this.subscribeToPaginatedSearchOptions(defs));
}
)
}
@@ -357,7 +355,8 @@ export class SearchConfigurationService implements OnDestroy {
isNotEmptyOperator(),
map((fixedFilter) => {
return { fixedFilter }
})
}),
tap(t => console.log(t))
);
}

View File

@@ -57,7 +57,6 @@ export class AppComponent implements OnInit, AfterViewInit {
private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics,
private authService: AuthService,
private router: Router,
private routeService: RouteService,
private cssService: CSSVariableService,
private menuService: MenuService,
private windowService: HostWindowService
@@ -77,13 +76,10 @@ export class AppComponent implements OnInit, AfterViewInit {
metadata.listenForRouteChange();
routeService.saveRouting();
if (config.debug) {
console.info(config);
}
this.storeCSSVariables();
}
ngOnInit() {

View File

@@ -13,6 +13,7 @@ import {
objectUpdatesReducer,
ObjectUpdatesState
} from './data/object-updates/object-updates.reducer';
import { routeReducer, RouteState } from '../shared/services/route.reducer';
export interface CoreState {
'cache/object': ObjectCacheState,
@@ -21,7 +22,8 @@ export interface CoreState {
'data/request': RequestState,
'index': MetaIndexState,
'auth': AuthState,
'json/patch': JsonPatchOperationsState
'json/patch': JsonPatchOperationsState,
'route': RouteState
}
export const coreReducers: ActionReducerMap<CoreState> = {
@@ -31,5 +33,6 @@ export const coreReducers: ActionReducerMap<CoreState> = {
'data/request': requestReducer,
'index': indexReducer,
'auth': authReducer,
'json/patch': jsonPatchOperationsReducer
'json/patch': jsonPatchOperationsReducer,
'route': routeReducer
};

View File

@@ -14,7 +14,7 @@ export const IndexActionTypes = {
/* tslint:disable:max-classes-per-file */
/**
* An ngrx action to add an value to the index
* An ngrx action to add a value to the index
*/
export class AddToIndexAction implements Action {
type = IndexActionTypes.ADD;
@@ -40,7 +40,7 @@ export class AddToIndexAction implements Action {
}
/**
* An ngrx action to remove an value from the index
* An ngrx action to remove a value from the index
*/
export class RemoveFromIndexByValueAction implements Action {
type = IndexActionTypes.REMOVE_BY_VALUE;

View File

@@ -0,0 +1,103 @@
import { Action } from '@ngrx/store';
import { type } from '../../shared/ngrx/type';
import { Params } from '@angular/router';
/**
* The list of HrefIndexAction type definitions
*/
export const RouteActionTypes = {
SET_QUERY_PARAMETERS: type('dspace/core/route/SET_QUERY_PARAMETERS'),
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'),
};
/* tslint:disable:max-classes-per-file */
/**
* An ngrx action to set the query parameters
*/
export class SetQueryParametersAction implements Action {
type = RouteActionTypes.SET_QUERY_PARAMETERS;
payload: Params;
/**
* Create a new SetQueryParametersAction
*
* @param parameters
* the query parameters
*/
constructor(parameters: Params) {
this.payload = parameters;
}
}
/**
* An ngrx action to set the parameters
*/
export class SetParametersAction implements Action {
type = RouteActionTypes.SET_PARAMETERS;
payload: Params;
/**
* Create a new SetParametersAction
*
* @param parameters
* the parameters
*/
constructor(parameters: Params) {
this.payload = parameters;
}
}
/**
* An ngrx action to add a query parameter
*/
export class AddQueryParameterAction implements Action {
type = RouteActionTypes.ADD_QUERY_PARAMETER;
payload: {
key: string;
value: string;
};
/**
* Create a new AddQueryParameterAction
*
* @param key
* the key to add
* @param value
* the value of this key
*/
constructor(key: string, value: string) {
this.payload = { key, value };
}
}
/**
* An ngrx action to add a parameter
*/
export class AddParameterAction implements Action {
type = RouteActionTypes.ADD_PARAMETER;
payload: {
key: string;
value: string;
};
/**
* Create a new AddParameterAction
*
* @param key
* the key to add
* @param value
* the value of this key
*/
constructor(key: string, value: string) {
this.payload = { key, value };
}
}
/* tslint:enable:max-classes-per-file */
/**
* A type to encompass all RouteActions
*/
export type RouteAction = SetQueryParametersAction | SetParametersAction | AddQueryParameterAction | AddParameterAction;

View File

@@ -0,0 +1,49 @@
import { Params } from '@angular/router';
import {
AddParameterAction,
AddQueryParameterAction,
RouteAction,
RouteActionTypes, SetParametersAction, SetQueryParametersAction
} from './route.action';
export interface RouteState {
queryParams: Params;
params: Params;
}
const initialState: RouteState = {
queryParams: {},
params: {}
};
export function routeReducer(state = initialState, action: RouteAction): RouteState {
switch (action.type) {
case RouteActionTypes.SET_PARAMETERS: {
return setParameters(state, action as SetParametersAction, 'params');
}
case RouteActionTypes.SET_QUERY_PARAMETERS: {
return setParameters(state, action as SetQueryParametersAction, 'queryParams');
}
case RouteActionTypes.ADD_PARAMETER: {
return addParameter(state, action as AddParameterAction, 'params');
}
case RouteActionTypes.ADD_QUERY_PARAMETER: {
return addParameter(state, action as AddQueryParameterAction, 'queryParams');
}
default: {
return state;
}
}
}
function addParameter(state: RouteState, action: AddParameterAction | AddQueryParameterAction, paramType: string): RouteState {
const subState = state[paramType];
const existingValues = subState[action.payload.key] || [];
const newValues = [...existingValues, action.payload.value];
const newSubstate = Object.assign(subState, { [action.payload.key]: newValues });
return Object.assign({}, state, { [paramType]: newSubstate });
}
function setParameters(state: RouteState, action: SetParametersAction | SetQueryParametersAction, paramType: string): RouteState {
return Object.assign({}, state, { [paramType]: action.payload });
}

View File

@@ -1,4 +1,4 @@
import { distinctUntilChanged, filter, map, mergeMap } from 'rxjs/operators';
import { distinctUntilChanged, filter, map, tap } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {
ActivatedRoute,
@@ -8,24 +8,46 @@ import {
RouterStateSnapshot,
} from '@angular/router';
import { Observable } from 'rxjs';
import { select, Store } from '@ngrx/store';
import { combineLatest, Observable } from 'rxjs';
import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
import { isEqual } from 'lodash';
import { AppState } from '../../app.reducer';
import { AddUrlToHistoryAction } from '../history/history.actions';
import { historySelector } from '../history/selectors';
import { SetParametersAction, SetQueryParametersAction } from './route.action';
import { CoreState } from '../../core/core.reducers';
import { hasValue } from '../empty.util';
import { coreSelector } from '../../core/core.selectors';
export const routeParametersSelector = createSelector(
coreSelector,
(state: CoreState) => state['route'].params
);
export const queryParametersSelector = createSelector(
coreSelector,
(state: CoreState) => state['route'].queryParams
);
export const routeParameterSelector = (key: string) => parameterSelector(key, routeParametersSelector);
export const queryParameterSelector = (key: string) => parameterSelector(key, queryParametersSelector);
export function parameterSelector(key: string, paramsSelector: (state: CoreState) => Params): MemoizedSelector<CoreState, string> {
return createSelector(paramsSelector, (state: Params) => {
if (hasValue(state)) {
return state[key];
} else {
return undefined;
}
});
}
/**
* Service to keep track of the current query parameters
*/
@Injectable()
export class RouteService {
params: Observable<Params>;
constructor(private route: ActivatedRoute, private router: Router, private store: Store<AppState>) {
this.subscribeToRouterParams();
constructor(private route: ActivatedRoute, private router: Router, private store: Store<CoreState>) {
this.saveRouting();
}
/**
@@ -74,11 +96,11 @@ export class RouteService {
}
getRouteParameterValue(paramName: string): Observable<string> {
return this.params.pipe(map((params) => params[paramName]),distinctUntilChanged(),);
return this.store.pipe(select(routeParameterSelector(paramName)), tap((t) => console.log(paramName, t)));
}
getRouteDataValue(datafield: string): Observable<any> {
return this.route.data.pipe(map((data) => data[datafield]),distinctUntilChanged(),);
return this.route.data.pipe(map((data) => data[datafield]), distinctUntilChanged(),);
}
/**
@@ -114,23 +136,23 @@ export class RouteService {
}
public saveRouting(): void {
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe(({ urlAfterRedirects }: NavigationEnd) => {
this.store.dispatch(new AddUrlToHistoryAction(urlAfterRedirects))
combineLatest(this.router.events, this.getRouteParams(), this.route.queryParams)
.pipe(filter(([event, params, queryParams]) => event instanceof NavigationEnd))
.subscribe(([event, params, queryParams]: [NavigationEnd, Params, Params]) => {
console.log(params);
this.store.dispatch(new SetParametersAction(params));
this.store.dispatch(new SetQueryParametersAction(queryParams));
this.store.dispatch(new AddUrlToHistoryAction(event.urlAfterRedirects));
});
}
subscribeToRouterParams() {
this.params = this.router.events.pipe(
mergeMap((event) => {
private getRouteParams(): Observable<Params> {
let active = this.route;
console.log(active);
while (active.firstChild) {
active = active.firstChild;
}
return active.params;
})
);
}
public getHistory(): Observable<string[]> {