Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
only store pagination sections from lists
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed Nov 27, 2020
1 parent 5987bfb commit 33c5d76
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ export abstract class ListDataSource<T, A = T> extends DataSource<T> implements
}
this.entitySelectConfig = this.getEntitySelectConfig(config.schema);
}
(this.action as PaginatedAction).isList = true;
this.masterAction.isList = true;
}

private getEntitySelectConfig(multiActionConfig: MultiActionConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,20 @@
</div>
<div
*ngIf="!(isAddingOrSelecting$ | async) && ((dataSource.isLoadingPage$ | async) || (hasRowsOrIsFiltering$ | async))">
<button matTooltip="Reset Filters & Sorting" matTooltipShowDelay="1000" mat-icon-button
<button matTooltip="Reset Filters & Sort" matTooltipShowDelay="1000" mat-icon-button
[disabled]="(dataSource.isLoadingPage$ | async)" (click)="resetFilteringAndSort();">
<mat-icon>remove_circle_outline</mat-icon>
</button>
</div>
<!-- Select table or cards view button -->
<div id="list-card-toggle"
*ngIf="config.viewType === 'both' && !(isAddingOrSelecting$ | async) && ((dataSource.isLoadingPage$ | async) || (hasRowsOrIsFiltering$ | async))">
<button mat-icon-button matTooltip="Change view" matTooltipShowDelay="1000"
<button mat-icon-button matTooltip="Card view" matTooltipShowDelay="1000"
[disabled]="(dataSource.isLoadingPage$ | async)" (click)="updateListView('cards');"
*ngIf="(view$ | async)==='table'">
<mat-icon>grid_on</mat-icon>
</button>
<button mat-icon-button matTooltip="Change view" matTooltipShowDelay="1000"
<button mat-icon-button matTooltip="Table view" matTooltipShowDelay="1000"
[disabled]="(dataSource.isLoadingPage$ | async)" (click)="updateListView('table');"
*ngIf="(view$ | async)==='cards'">
<mat-icon>list</mat-icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView
pageSizeOptions: number[],
pageSize: number,
pageIndex: number,
length: number
length: number;
} = {
pageSizeOptions: null,
pageSize: null,
Expand Down Expand Up @@ -602,20 +602,22 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView

resetFilteringAndSort() {
// TODO: RC multi entity type lists
const pAction: PaginatedAction = this.dataSource.action['length'] ? this.dataSource.action[0] : this.dataSource.action
/* tslint:disable-next-line:no-string-literal */
const pAction: PaginatedAction = this.dataSource.action['length'] ? this.dataSource.action[0] : this.dataSource.action;
this.store.dispatch(new ResetPaginationSortFilter(pAction));
// Ensure that the changes are pushed back to the multi filter controls. Ideally the field to store relationship would be two way..
// but it's not for the moment
this.multiFilterManagers.forEach(manager => {
manager.hasOneItem$.pipe(first()).subscribe(hasOneItem => {
// TODO: RC Test
// const selectItem = hasOneItem || ''
if (hasOneItem) {
return;
}
manager.selectItem('')
})
})
// TODO: RC huh
// manager.hasOneItem$.pipe(first()).subscribe(hasOneItem => {
// // TODO: RC Test
// // const selectItem = hasOneItem || ''
// if (hasOneItem) {
// return;
// }
// manager.selectItem('')
// })
});
if (!this.config.isLocal) {
// TODO: RC Fixes space routes, breaks app wall!!
this.store.dispatch(new ResetPagination(pAction, pAction.paginationKey));
Expand Down Expand Up @@ -736,7 +738,7 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView
}

private getRowStateFromRowsState = (row: T): Observable<RowState> =>
this.dataSource.rowsState.pipe(map(state => state[this.dataSource.getRowUniqueId(row)] || getDefaultRowState()))
this.dataSource.rowsState.pipe(map(state => state[this.dataSource.getRowUniqueId(row)] || getDefaultRowState()));

public showAllAfterMax() {
this.dataSource.showAllAfterMax();
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/packages/store/src/actions/pagination.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const REMOVE_ID_FROM_PAGINATION = '[Pagination] Remove id from pagination
export const UPDATE_MAXED_STATE = '[Pagination] Update maxed state';
export const IGNORE_MAXED_STATE = '[Pagination] Ignore maxed state';
export const HYDRATE_PAGINATION_STATE = '[Pagination] Hydrate pagination state';
export const SET_PAGINATION_IS_LIST = '[Pagination] Set Is List';

export function getPaginationKey(type: string, id: string, endpointGuid?: string) {
const key = `${type}-${id}`;
Expand Down Expand Up @@ -191,6 +192,7 @@ export class SetInitialParams extends BasePaginationAction implements Action, Se
public params: PaginationParam,
public keepPages = false,
public overwrite = false,
public isList = false,
) {
super(pEntityConfig);
}
Expand Down Expand Up @@ -248,3 +250,8 @@ export class HydratePaginationStateAction implements Action {
constructor(public paginationState: PaginationState) { }
type = HYDRATE_PAGINATION_STATE;
}

export class SetPaginationIsList implements Action {
constructor(public pagAction: PaginatedAction) { }
type = SET_PAGINATION_IS_LIST;
}
26 changes: 14 additions & 12 deletions src/frontend/packages/store/src/helpers/local-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export enum LocalStorageSyncTypes {

export class LocalStorageService {

// TODO: RC applying filters that aren't in lists
// TODO: RC applying cf filter works, but not org/space
// TODO: RC cleaning sessions storage (entities that don't exist, etc, can be done by storeagesync??)
// TODO: RC backward compatible (load 'user-dashboard' into 'user')
Expand Down Expand Up @@ -47,19 +46,19 @@ export class LocalStorageService {
dataForStore => store.dispatch(new HydrateDashboardStateAction(dataForStore)),
storage,
sessionId
)
);
LocalStorageService.handleHydrate(
LocalStorageSyncTypes.PAGINATION,
dataForStore => store.dispatch(new HydratePaginationStateAction(dataForStore)),
storage,
sessionId
)
);
LocalStorageService.handleHydrate(
LocalStorageSyncTypes.LISTS,
dataForStore => store.dispatch(new HydrateListsStateAction(dataForStore)),
storage,
sessionId
)
);
}
}
}
Expand Down Expand Up @@ -87,7 +86,7 @@ export class LocalStorageService {
}

private static makeKey(userId: string, storeKey: string) {
return userId + '-' + storeKey
return userId + '-' + storeKey;
}

/**
Expand Down Expand Up @@ -131,6 +130,7 @@ export class LocalStorageService {
}

private static getDashboardStateSessionId(username?: string) {
// TODO: RC name should be encoded
const prefix = 'stratos-';
if (username) {
return prefix + username;
Expand All @@ -150,14 +150,16 @@ export class LocalStorageService {
switch (type) {
case LocalStorageSyncTypes.PAGINATION:
const pagination: PaginationState = storePart as unknown as PaginationState;
// TODO: RC Ignore if not from a list!!!!
const abs = Object.keys(pagination).reduce((res, entityTypes) => {
const perEntity = Object.keys(pagination[entityTypes]).reduce((res2, paginationKeysOfEntityType) => {
const paginationSection = pagination[entityTypes][paginationKeysOfEntityType]
const paginationSection = pagination[entityTypes][paginationKeysOfEntityType];
if (!paginationSection.isListPagination) {
return res2;
}
res2[paginationKeysOfEntityType] = {
params: paginationSection.params,
clientPagination: paginationSection.clientPagination
}
};
return res2;
}, {});
if (Object.keys(perEntity).length > 0) {
Expand All @@ -171,7 +173,7 @@ export class LocalStorageService {
}

public static localStorageSize(sessionData: SessionData): number {
const storage = LocalStorageService.getStorage()
const storage = LocalStorageService.getStorage();
const sessionId = LocalStorageService.getDashboardStateSessionId(sessionData.user.name);
if (storage && sessionId) {
return Object.values(LocalStorageSyncTypes).reduce((total, type) => {
Expand All @@ -193,14 +195,14 @@ export class LocalStorageService {
confirm: 'Clear',
critical: true,
title: 'Are you sure?'
}
};

const successAction = res => {
if (!res) {
return;
}

const storage = LocalStorageService.getStorage()
const storage = LocalStorageService.getStorage();
const sessionId = LocalStorageService.getDashboardStateSessionId(sessionData.user.name);
if (storage && sessionId) {
Object.values(LocalStorageSyncTypes).forEach(type => {
Expand All @@ -213,7 +215,7 @@ export class LocalStorageService {
} else {
console.warn('Unable to clear local storage, either storage or session id is missing');
}
}
};

confirmationService.openWithCancel(config, successAction, () => { });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const defaultPaginationEntityState: PaginationEntityState = {
},
maxedState: {
isMaxedMode: false
}
},
isListPagination: false
};

export function getDefaultPaginationEntityState(ignoreMaxed?: boolean): PaginationEntityState {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SetParams } from '../../actions/pagination.actions';
import { SetInitialParams, SetParams } from '../../actions/pagination.actions';
import { PaginationEntityState } from '../../types/pagination.types';
import { removeEmptyParams } from './pagination-reducer.helper';
import { resultPerPageParam, resultPerPageParamDefault } from './pagination-reducer.types';

export function paginationSetParams(state: PaginationEntityState, action: SetParams) {
export function paginationSetParams(state: PaginationEntityState, action: SetInitialParams | SetParams): PaginationEntityState {
let params;
if (action.overwrite) {
// Overwrite any existing values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
tap,
} from 'rxjs/operators';

import { SetInitialParams } from '../../actions/pagination.actions';
import { SetInitialParams, SetPaginationIsList } from '../../actions/pagination.actions';
import { AppState, GeneralEntityAppState } from '../../app-state';
import { entityCatalog } from '../../entity-catalog/entity-catalog';
import { PaginationMonitor } from '../../monitors/pagination-monitor';
Expand Down Expand Up @@ -67,7 +67,6 @@ export function getPaginationKeyFromAction(action: PaginatedAction) {
return apiAction.paginationKey;
}

// TODO: This needs to be a service not just a function! - #3802
export const getPaginationObservables = <T = any, Y extends AppState = AppState>(
{ store, action, paginationMonitor }: {
store: Store<Y>,
Expand All @@ -79,6 +78,9 @@ export const getPaginationObservables = <T = any, Y extends AppState = AppState>
const baseAction = Array.isArray(action) ? action[0] : action;
const paginationKey = paginationMonitor.paginationKey;
const entityKey = paginationMonitor.schema.key;

store.dispatch(new SetPaginationIsList(baseAction));

// FIXME: This will reset pagination every time regardless of if we need to (or just want the pag settings/entities from pagination
// section)
if (baseAction.initialParams) {
Expand Down Expand Up @@ -152,7 +154,7 @@ function paginationParamsString(params: PaginationParam): string {
}


function sortStringify(obj: { [key: string]: string | string[] | number }): string {
function sortStringify(obj: { [key: string]: string | string[] | number; }): string {
const keys = Object.keys(obj).sort();
return keys.reduce((res, key) => {
return res += `${key}-${obj[key]},`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ import {
SET_INITIAL_PARAMS,
SET_PAGE,
SET_PAGE_BUSY,
SET_PAGINATION_IS_LIST,
SET_PARAMS,
SET_RESULT_COUNT,
SetPaginationIsList,
UPDATE_MAXED_STATE,
} from '../../actions/pagination.actions';
import { ApiActionTypes } from '../../actions/request.actions';
import { InitCatalogEntitiesAction } from '../../entity-catalog.actions';
import { entityCatalog } from '../../entity-catalog/entity-catalog';
import { getDefaultStateFromEntityCatalog } from '../../entity-catalog/entity-catalog.store-setup';
import { mergeState } from '../../helpers/reducer.helper';
import { PaginationEntityState, PaginationState } from '../../types/pagination.types';
import { PaginationEntityState, PaginationEntityTypeState, PaginationState } from '../../types/pagination.types';
import { UpdatePaginationMaxedState } from './../../actions/pagination.actions';
import { paginationAddParams } from './pagination-reducer-add-params';
import { paginationClearPages } from './pagination-reducer-clear-pages';
Expand Down Expand Up @@ -165,9 +167,31 @@ function paginate(action, state: PaginationState = {}, updatePagination) {
return paginationResetToStart(state, action as ResetPaginationSortFilter);
}

if (action.type === SET_PAGINATION_IS_LIST) {
return setPaginationIsList(state, action as SetPaginationIsList);
}

return enterPaginationReducer(state, action, updatePagination);
}

function setPaginationIsList(state: PaginationState, action: SetPaginationIsList): PaginationState {
const entityKey = entityCatalog.getEntityKey(action.pagAction);
const existingPag = state[entityKey] ? state[entityKey][action.pagAction.paginationKey] : null;
const pag = existingPag || getDefaultPaginationEntityState();

const entityState: PaginationEntityTypeState = {
...state[entityKey],
[action.pagAction.paginationKey]: {
...pag,
isListPagination: action.pagAction.isList
}
};
return {
...state,
[entityKey]: entityState
};
}

// TODO: RC tidy the hell out of this
/**
* Push data (in the action) from local storage back into the pagination state
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/packages/store/src/types/pagination.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export class PaginationEntityState {
ids = {};
params: PaginationParam;
pageRequests: {
[pageNumber: string]: ListActionState
[pageNumber: string]: ListActionState,
};
clientPagination?: PaginationClientPagination;
/**
* The pagination key from where we share our values.
*/
seed?: string;
maxedState: PaginationMaxedState;
forList = false;
isListPagination = false;
}

export function isPaginatedAction(obj: any): PaginatedAction {
Expand Down

0 comments on commit 33c5d76

Please sign in to comment.