Skip to content

Commit

Permalink
Added windowsMeta actions and reducer.
Browse files Browse the repository at this point in the history
Added activeWindowId state to reducer.
  • Loading branch information
imolorhe committed Nov 26, 2017
1 parent e3f3e18 commit c2c3d16
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 31 deletions.
11 changes: 11 additions & 0 deletions src/app/actions/windows-meta/windows-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action } from '@ngrx/store';

export const SET_ACTIVE_WINDOW_ID = 'SET_ACTIVE_WINDOW_ID';

export class SetActiveWindowIdAction implements Action {
readonly type = SET_ACTIVE_WINDOW_ID;

constructor(public payload: { windowId: string }) {}
}

export type Action = SetActiveWindowIdAction;
6 changes: 4 additions & 2 deletions src/app/containers/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as dialogsActions from '../../actions/dialogs/dialogs';
import * as layoutActions from '../../actions/layout/layout';
import * as docsActions from '../../actions/docs/docs';
import * as windowsActions from '../../actions/windows/windows';
import * as windowsMetaActions from '../../actions/windows-meta/windows-meta';

import { QueryService } from '../../services/query.service';
import { GqlService } from '../../services/gql.service';
Expand Down Expand Up @@ -69,10 +70,11 @@ export class AppComponent {
console.log(data.windows);
this.windowIds = Object.keys(data.windows);
this.windowsArr = this.windowIds.map(id => data.windows[id]);
this.activeWindowId = data.windowsMeta.activeWindowId;

// If the active window has not been set, default it
if (!this.activeWindowId || !data.windows[this.activeWindowId]) {
this.activeWindowId = this.windowIds[0];
this.store.dispatch(new windowsMetaActions.SetActiveWindowIdAction({ windowId: this.windowIds[0] }));
}
});

Expand Down Expand Up @@ -109,7 +111,7 @@ export class AppComponent {
}

setActiveWindow(windowId) {
this.activeWindowId = windowId;
this.store.dispatch(new windowsMetaActions.SetActiveWindowIdAction({ windowId }));
}

removeWindow(windowId) {
Expand Down
58 changes: 30 additions & 28 deletions src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,52 @@ import * as fromDialogs from './dialogs/dialogs';
import * as fromGqlSchema from './gql-schema/gql-schema';
import * as fromDocs from './docs/docs';
import * as fromWindows from './windows';
import * as fromWindowsMeta from './windows-meta/windows-meta';

export interface State {
layout: fromLayout.State;
query: fromQuery.State;
headers: fromHeaders.State;
variables: fromVariables.State;
dialogs: fromDialogs.State;
schema: fromGqlSchema.State;
docs: fromDocs.State;
// windows: fromWindows.State;
layout: fromLayout.State;
query: fromQuery.State;
headers: fromHeaders.State;
variables: fromVariables.State;
dialogs: fromDialogs.State;
schema: fromGqlSchema.State;
docs: fromDocs.State;
}

const reducers = {
layout: fromLayout.layoutReducer,
query: fromQuery.queryReducer,
headers: fromHeaders.headerReducer,
variables: fromVariables.variableReducer,
dialogs: fromDialogs.dialogReducer,
schema: fromGqlSchema.gqlSchemaReducer,
docs: fromDocs.docsReducer,
// windows: fromWindows.windowReducer
layout: fromLayout.layoutReducer,
query: fromQuery.queryReducer,
headers: fromHeaders.headerReducer,
variables: fromVariables.variableReducer,
dialogs: fromDialogs.dialogReducer,
schema: fromGqlSchema.gqlSchemaReducer,
docs: fromDocs.docsReducer
};

// Meta reducer to log actions
export const log = (reducer) => (state: State, action: Action) => {
if (!environment.production) {
console.log(action.type, action);
}
return reducer(state, action);
if (!environment.production) {
console.log(action.type, action);
}
return reducer(state, action);
};

export const keySerializer = (key) => 'altair_' + key;

// Needed to fix the error encountered resolving symbol values statically error for the compose() method
export function createReducer() {
const reducer = compose(
localStorageSync({ keys: ['windows'], rehydrate: true, storageKeySerializer: keySerializer}),
log,
combineReducers
)({ windows: fromWindows.windows(combineReducers(reducers)) });

return reducer;
const reducer = compose(
localStorageSync({ keys: ['windows', 'windowsMeta'], rehydrate: true, storageKeySerializer: keySerializer}),
log,
combineReducers
)({
windows: fromWindows.windows(combineReducers(reducers)),
windowsMeta: fromWindowsMeta.windowsMetaReducer
});

return reducer;
}

export function appReducer(state: State, action: Action) {
return createReducer()(state, action);
return createReducer()(state, action);
}
1 change: 0 additions & 1 deletion src/app/reducers/layout/layout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Action } from '@ngrx/store';

import * as query from '../../actions/query/query';
import * as layout from '../../actions/layout/layout';

export interface State {
Expand Down
20 changes: 20 additions & 0 deletions src/app/reducers/windows-meta/windows-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Action } from '@ngrx/store';

import * as windowsMeta from '../../actions/windows-meta/windows-meta';

export interface State {
activeWindowId: string;
}

const initialState: State = {
activeWindowId: ''
};

export function windowsMetaReducer(state = initialState, action: windowsMeta.Action): State {
switch (action.type) {
case windowsMeta.SET_ACTIVE_WINDOW_ID:
return Object.assign({}, state, { activeWindowId: action.payload.windowId });
default:
return state;
}
}

0 comments on commit c2c3d16

Please sign in to comment.