Skip to content

Commit

Permalink
Add logic for tracking the currently selected pilot
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Oct 28, 2017
1 parent 02dcc9e commit 593e570
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/reducers/rootReducer.js
@@ -1,11 +1,13 @@
import {combineReducers} from "redux";

import entitiesReducer from "./entitiesReducer";
import pilotsReducer from "features/pilots/pilotsReducer";
import tabReducer from "features/tabs/tabsReducer";
import unitInfoReducer from "features/unitInfo/unitInfoReducer";

const rootReducer = combineReducers({
entities : entitiesReducer,
pilots : pilotsReducer,
unitInfo : unitInfoReducer,
tabs : tabReducer,
});
Expand Down
8 changes: 8 additions & 0 deletions src/features/pilots/pilotsActions.js
@@ -0,0 +1,8 @@
import {PILOT_SELECT} from "./pilotsConstants";

export function selectPilot(pilotID) {
return {
type : PILOT_SELECT,
payload : {currentPilot : pilotID},
};
}
1 change: 1 addition & 0 deletions src/features/pilots/pilotsConstants.js
@@ -0,0 +1 @@
export const PILOT_SELECT = "PILOT_SELECT";
25 changes: 25 additions & 0 deletions src/features/pilots/pilotsReducer.js
@@ -0,0 +1,25 @@
import {createReducer} from "common/utils/reducerUtils";

import {PILOT_SELECT} from "./pilotsConstants";

const initialState = {
currentPilot : null
};

export function selectPilot(state, payload) {
const prevSelectedPilot = state.currentPilot;
const newSelectedPilot = payload.currentPilot;

const isSamePilot = prevSelectedPilot === newSelectedPilot;

return {
// Deselect entirely if it's a second click on the same pilot,
// otherwise go ahead and select the one that was clicked
currentPilot : isSamePilot ? null : newSelectedPilot,
};
}


export default createReducer(initialState, {
[PILOT_SELECT] : selectPilot,
});
8 changes: 8 additions & 0 deletions src/features/pilots/pilotsSelectors.js
@@ -0,0 +1,8 @@
import {createSelector} from "reselect";

export const selectPilots = state => state.pilots;

export const selectCurrentPilot = createSelector(
selectPilots,
pilots => pilots.currentPilot,
);

0 comments on commit 593e570

Please sign in to comment.