Skip to content

Commit 54794ac

Browse files
committed
Add logic for tracking the currently selected mech
1 parent 19c3c3a commit 54794ac

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

src/app/reducers/rootReducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import entitiesReducer from "./entitiesReducer";
44
import tabReducer from "features/tabs/tabReducer";
55
import unitInfoReducer from "features/unitInfo/unitInfoReducer";
66
import pilotsReducer from "features/pilots/pilotsReducer";
7+
import mechsReducer from "features/mechs/mechsReducer";
78

89

910
const rootReducer = combineReducers({
1011
entities : entitiesReducer,
1112
unitInfo : unitInfoReducer,
1213
pilots : pilotsReducer,
14+
mechs : mechsReducer,
1315
tabs : tabReducer,
1416
});
1517

src/features/mechs/mechSelectors.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import {createSelector} from "reselect";
22

33
const WEIGHT_CLASSES = [
44
{name : "Light", weights : [20, 25, 30, 35]},
@@ -11,3 +11,11 @@ export function getWeightClass(weight) {
1111
const weightClass = WEIGHT_CLASSES.find(wc => wc.weights.includes(weight)) || {name : "Unknown"};
1212
return weightClass.name;
1313
}
14+
15+
16+
const selectMechs = state => state.mechs;
17+
18+
export const selectCurrentMech = createSelector(
19+
selectMechs,
20+
mechs => mechs.currentMech,
21+
);

src/features/mechs/mechsActions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {MECH_SELECT} from "./mechsConstants";
2+
3+
export function selectMech(mechID) {
4+
return {
5+
type : MECH_SELECT,
6+
payload : {currentMech : mechID},
7+
};
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MECH_SELECT = "MECH_SELECT";

src/features/mechs/mechsReducer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {createReducer} from "common/utils/reducerUtils";
2+
3+
import {MECH_SELECT} from "./mechsConstants";
4+
5+
const initialState = {
6+
currentMech : null
7+
};
8+
9+
export function selectMech(state, payload) {
10+
const prevSelectedMech = state.currentMech;
11+
const newSelectedMech = payload.currentMech;
12+
13+
const isSameMech = prevSelectedMech === newSelectedMech;
14+
15+
return {
16+
// Deselect entirely if it's a second click on the same mech,
17+
// otherwise go ahead and select the one that was clicked
18+
currentMech : isSameMech ? null : newSelectedMech,
19+
};
20+
}
21+
22+
23+
export default createReducer(initialState, {
24+
[MECH_SELECT] : selectMech,
25+
});

0 commit comments

Comments
 (0)