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

Commit

Permalink
Changes following review #1
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed Dec 2, 2020
1 parent aaf2254 commit 84e123a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,6 @@ export const enum CfOrgSpaceSelectMode {
ANY = 2
}


// export const initCfOrgSpaceService = (
// store: Store<CFAppState>,
// cfOrgSpaceService: CfOrgSpaceDataService,
// entityKey: string,
// paginationKey: string): Observable<any> => {
// return store.select(selectPaginationState(entityKey, paginationKey)).pipe(
// filter((pag) => !!pag),
// first(),
// tap(pag => {
// const { cf, org, space } = pag.clientPagination.filter.items;
// if (cf) {
// cfOrgSpaceService.cf.select.next(cf);
// }
// if (org) {
// cfOrgSpaceService.org.select.next(org);
// }
// if (space) {
// cfOrgSpaceService.space.select.next(space);
// }
// })
// );
// };

export const createCfOrSpaceMultipleFilterFn = (
store: Store<CFAppState>,
action: PaginatedAction,
Expand Down Expand Up @@ -362,14 +338,15 @@ export class CfOrgSpaceDataService implements OnDestroy {
}

private setupAutoSelectors(initialCf: string, initialOrg: string) {
console.log(`initialCf: ${initialCf}. initialOrg: ${initialOrg}`); // TODD: RC console.log
// Clear or automatically select org + space given cf
let cfTapped = false;
const orgResetSub = this.cf.select.asObservable().pipe(
startWith(initialCf),
distinctUntilChanged(),
filter(cf => cf !== initialCf),
filter(cf => cfTapped || cf !== initialCf),
withLatestFrom(this.org.list$),
tap(([selectedCF, orgs]) => {
initialCf = 'junk';
cfTapped = true;
if (
!!orgs.length &&
((this.selectMode === CfOrgSpaceSelectMode.FIRST_ONLY && orgs.length === 1) ||
Expand All @@ -385,13 +362,14 @@ export class CfOrgSpaceDataService implements OnDestroy {
this.subs.push(orgResetSub);

// Clear or automatically select space given org
let orgTapped = false;
const spaceResetSub = this.org.select.asObservable().pipe(
startWith(initialOrg),
distinctUntilChanged(),
filter(org => org !== initialOrg),
filter(org => orgTapped || org !== initialOrg),
withLatestFrom(this.space.list$),
tap(([selectedOrg, spaces]) => {
initialOrg = 'junk';
orgTapped = true;
if (
!!spaces.length &&
((this.selectMode === CfOrgSpaceSelectMode.FIRST_ONLY && spaces.length === 1) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class ProfileInfoComponent {
}

clearLocalStorage() {
this.sessionData$.pipe(first()).subscribe(sessionData => LocalStorageService.clear(sessionData, this.confirmationService));
this.sessionData$.pipe(first()).subscribe(sessionData => LocalStorageService.clearLocalStorage(sessionData, this.confirmationService));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
*ngIf="!(isAddingOrSelecting$ | async) && ((dataSource.isLoadingPage$ | async) || (hasRowsOrIsFiltering$ | async))">
<button matTooltip="Reset Filters & Sort" matTooltipShowDelay="1000" mat-icon-button
[disabled]="(dataSource.isLoadingPage$ | async)" (click)="resetFilteringAndSort();">
<mat-icon>remove_circle_outline</mat-icon>
<mat-icon>highlight_off</mat-icon>
</button>
</div>
<!-- Select table or cards view button -->
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/packages/store/src/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class AuthEffect {
} else {
const sessionData = envelope.data;
sessionData.sessionExpiresOn = parseInt(response.headers.get('x-cap-session-expires-on'), 10) * 1000;
LocalStorageService.storageToStore(this.store, sessionData);
LocalStorageService.localStorageToStore(this.store, sessionData);
return [
stratosEntityCatalog.systemInfo.actions.getSystemInfo(true),
new VerifiedSession(sessionData, action.updateEndpoints)
Expand Down
64 changes: 29 additions & 35 deletions src/frontend/packages/store/src/helpers/local-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,42 @@ export class LocalStorageService {
private static Encrypt = true;

/**
* Normally used on app init, move local storage data into the console's store
* Object used to access/update local storage
*/
public static storageToStore(store: Store<DispatchOnlyAppState>, sessionData: SessionData) {
return LocalStorageService.rehydrateDashboardState(store, sessionData);
private static getStorage(): Storage {
return localStorage || window.localStorage;
}

/**
* For the current user dispatch actions that will populate the store with the contents of local storage
* Make a key used by local storage that relates to a section of the user's settings in the console's store
*/
private static makeKey(userId: string, storeKey: LocalStorageSyncTypes) {
return userId + '-' + storeKey;
}

/**
* Normally used on app init, move local storage data into the console's store
*/
private static rehydrateDashboardState(store: Store<DispatchOnlyAppState>, sessionData: SessionData) {
public static localStorageToStore(store: Store<DispatchOnlyAppState>, sessionData: SessionData) {
const storage = LocalStorageService.getStorage();
// We use the username to key the session storage. We could replace this with the users id?
if (storage && sessionData.user) {
const sessionId = LocalStorageService.getDashboardStateSessionId(sessionData.user.name);
const sessionId = LocalStorageService.getLocalStorageSessionId(sessionData.user.name);
if (sessionId) {
LocalStorageService.handleHydrate(
LocalStorageService.localStorageToStoreSection(
LocalStorageSyncTypes.DASHBOARD,
dataForStore => store.dispatch(new HydrateDashboardStateAction(dataForStore)),
storage,
sessionId
);
LocalStorageService.handleHydrate(
LocalStorageService.localStorageToStoreSection(
LocalStorageSyncTypes.PAGINATION,
dataForStore => store.dispatch(new HydratePaginationStateAction(dataForStore)),
storage,
sessionId,
true
);
LocalStorageService.handleHydrate(
LocalStorageService.localStorageToStoreSection(
LocalStorageSyncTypes.LISTS,
dataForStore => store.dispatch(new HydrateListsStateAction(dataForStore)),
storage,
Expand All @@ -63,15 +70,11 @@ export class LocalStorageService {
}
}

private static getStorage(): Storage {
return localStorage || window.localStorage;
}

/**
* For a given storage type fetch it's data for the given user from local storage and dispatch an action that will
* be handled by the reducers for that storage type (dashboard, pagination, etc)
*/
private static handleHydrate(
private static localStorageToStoreSection(
type: LocalStorageSyncTypes,
dispatch: (dataForStore: any) => void,
storage: Storage,
Expand All @@ -88,14 +91,10 @@ export class LocalStorageService {
}
}

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

/**
* This will ensure changes in the store are selectively pushed to local storage
*/
public static localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
public static storeToLocalStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
// This is done to ensure we don't accidentally apply state from session storage from another user.
let globalUserId = null;
return localStorageSync({
Expand All @@ -105,7 +104,7 @@ export class LocalStorageService {
if (globalUserId) {
return true;
}
const userId = LocalStorageService.getDashboardStateSessionId();
const userId = LocalStorageService.getLocalStorageSessionId();
if (userId) {
globalUserId = userId;
return true;
Expand All @@ -117,18 +116,10 @@ export class LocalStorageService {
LocalStorageSyncTypes.LISTS,
{
[LocalStorageSyncTypes.PAGINATION]: {
serialize: (pagination: PaginationState) => LocalStorageService.parseForStorage<PaginationState>(
serialize: (pagination: PaginationState) => LocalStorageService.parseStorePartForLocalStorage<PaginationState>(
pagination,
LocalStorageSyncTypes.PAGINATION
),
// encrypt: (state: string) => {
// console.log('a');
// return state;
// },
// decrypt: (state: string) => {
// console.log('b');
// return state;
// },
},
},
],
Expand All @@ -138,7 +129,10 @@ export class LocalStorageService {
})(reducer);
}

private static getDashboardStateSessionId(username?: string) {
/**
* Get a unique identifier for the user
*/
private static getLocalStorageSessionId(username?: string) {
const prefix = 'stratos-';
if (username) {
return prefix + username;
Expand All @@ -153,7 +147,7 @@ export class LocalStorageService {
/**
* Allow for selective persistence of data. For pagination we only store params and clientPagination
*/
private static parseForStorage<T = any>(storePart: T, type: LocalStorageSyncTypes): object {
private static parseStorePartForLocalStorage<T = any>(storePart: T, type: LocalStorageSyncTypes): object {
switch (type) {
case LocalStorageSyncTypes.PAGINATION:
const pagination: PaginationState = storePart as unknown as PaginationState;
Expand Down Expand Up @@ -189,7 +183,7 @@ export class LocalStorageService {

public static localStorageSize(sessionData: SessionData): number {
const storage = LocalStorageService.getStorage();
const sessionId = LocalStorageService.getDashboardStateSessionId(sessionData.user.name);
const sessionId = LocalStorageService.getLocalStorageSessionId(sessionData.user.name);
if (storage && sessionId) {
return Object.values(LocalStorageSyncTypes).reduce((total, type) => {
const key = LocalStorageService.makeKey(sessionId, type);
Expand All @@ -204,9 +198,9 @@ export class LocalStorageService {
/**
* Clear local storage and the store
*/
public static clear(sessionData: SessionData, confirmationService: ConfirmationDialogService, reloadTo = '/user-profile') {
public static clearLocalStorage(sessionData: SessionData, confirmationService: ConfirmationDialogService, reloadTo = '/user-profile') {
const config: ConfirmationDialogConfig = {
message: 'This will clear your settings in local storage for this address and reload this window',
message: 'This will clear your stored settings and reload the application',
confirm: 'Clear',
critical: true,
title: 'Are you sure?'
Expand All @@ -218,7 +212,7 @@ export class LocalStorageService {
}

const storage = LocalStorageService.getStorage();
const sessionId = LocalStorageService.getDashboardStateSessionId(sessionData.user.name);
const sessionId = LocalStorageService.getLocalStorageSessionId(sessionData.user.name);
if (storage && sessionId) {
Object.values(LocalStorageSyncTypes).forEach(type => {
const key = LocalStorageService.makeKey(sessionId, type);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/packages/store/src/reducers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const appReducers: ActionReducerMap<{}> = {
appReducers,
{
metaReducers: [
LocalStorageService.localStorageSyncReducer
LocalStorageService.storeToLocalStorageSyncReducer
],
runtimeChecks: {
strictStateImmutability: true,
Expand Down

0 comments on commit 84e123a

Please sign in to comment.