diff --git a/src/app/reducers/rootReducer.js b/src/app/reducers/rootReducer.js index a32055b..07ae956 100644 --- a/src/app/reducers/rootReducer.js +++ b/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, }); diff --git a/src/features/pilots/pilotsActions.js b/src/features/pilots/pilotsActions.js new file mode 100644 index 0000000..326a5a1 --- /dev/null +++ b/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}, + }; +} diff --git a/src/features/pilots/pilotsConstants.js b/src/features/pilots/pilotsConstants.js new file mode 100644 index 0000000..99b2413 --- /dev/null +++ b/src/features/pilots/pilotsConstants.js @@ -0,0 +1 @@ +export const PILOT_SELECT = "PILOT_SELECT"; diff --git a/src/features/pilots/pilotsReducer.js b/src/features/pilots/pilotsReducer.js new file mode 100644 index 0000000..90181a5 --- /dev/null +++ b/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, +}); \ No newline at end of file diff --git a/src/features/pilots/pilotsSelectors.js b/src/features/pilots/pilotsSelectors.js new file mode 100644 index 0000000..16c97b8 --- /dev/null +++ b/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, +);