Skip to content

Commit bba6aa9

Browse files
committed
Update MechsList to be connected
Connected the MechsList and MechsList Row components MechsList now extracts a list of mech IDs instead of objects Disconnected Mechs, which is now effectively presentational again
1 parent 60c3f29 commit bba6aa9

File tree

3 files changed

+64
-55
lines changed

3 files changed

+64
-55
lines changed

src/features/mechs/Mechs/Mechs.jsx

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,23 @@
11
import React, {Component} from "react";
2-
import {connect} from "react-redux";
32

43
import {
54
Grid,
65
Segment,
76
Header,
87
} from "semantic-ui-react";
98

10-
import schema from "app/schema";
11-
129
import MechsList from "../MechsList";
1310
import MechDetails from "../MechDetails";
1411

15-
import {selectMech} from "../mechsActions";
16-
import {selectCurrentMech} from "../mechSelectors";
17-
18-
19-
const mapState = (state) => {
20-
const session = schema.from(state.entities);
21-
const {Mech} = session;
22-
23-
const mechs = Mech.all().withModels.map(mechModel => {
24-
const mech = {
25-
// Copy the data from the plain JS object
26-
...mechModel.ref,
27-
// Provide a default empty object for the relation
28-
mechType : {},
29-
};
30-
31-
if(mechModel.type) {
32-
// Replace the default object with a copy of the relation's data
33-
mech.mechType = {...mechModel.type.ref};
34-
}
35-
36-
return mech;
37-
});
38-
39-
const currentMech = selectCurrentMech(state);
4012

41-
return {mechs, currentMech}
42-
}
43-
44-
const actions = {
45-
selectMech,
46-
};
47-
48-
class Mechs extends Component {
13+
export default class Mechs extends Component {
4914
render() {
50-
const {mechs = [], selectMech, currentMech} = this.props;
51-
5215
return (
5316
<Segment>
5417
<Grid>
5518
<Grid.Column width={10}>
5619
<Header as="h3">Mechs List</Header>
57-
<MechsList
58-
mechs={mechs}
59-
onMechClicked={selectMech}
60-
currentMech={currentMech}
61-
/>
20+
<MechsList />
6221
</Grid.Column>
6322
<Grid.Column width={6}>
6423
<Header as="h3">Mech Details</Header>
@@ -71,6 +30,3 @@ class Mechs extends Component {
7130
);
7231
}
7332
}
74-
75-
76-
export default connect(mapState, actions)(Mechs);
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
import React, {Component} from "react";
2+
import {connect} from "react-redux";
23
import {Table} from "semantic-ui-react";
34

45
import MechsListHeader from "./MechsListHeader";
56
import MechsListRow from "./MechsListRow";
67

8+
import schema from "app/schema";
79

10+
import {selectMech} from "../mechsActions";
11+
import {selectCurrentMech} from "../mechSelectors";
812

9-
export default class MechsList extends Component {
13+
14+
const mapState = (state) => {
15+
const session = schema.from(state.entities);
16+
const {Mech} = session;
17+
18+
const mechs = Mech.all().withModels.map(mechModel => mechModel.getId());
19+
20+
const currentMech = selectCurrentMech(state);
21+
22+
return {mechs, currentMech}
23+
}
24+
25+
const actions = {
26+
selectMech,
27+
};
28+
29+
30+
31+
export class MechsList extends Component {
1032

1133
render() {
12-
const {mechs = [], onMechClicked, currentMech} = this.props;
34+
const {mechs = [], currentMech, selectMech} = this.props;
1335

14-
const mechRows = mechs.map(mech => (
36+
const mechRows = mechs.map(mechID => (
1537
<MechsListRow
16-
mech={mech}
17-
key={mech.id}
18-
onMechClicked={onMechClicked}
19-
selected={mech.id === currentMech}
38+
mechID={mechID}
39+
key={mechID}
40+
onMechClicked={selectMech}
41+
selected={mechID === currentMech}
2042
/>
2143
));
2244

@@ -31,3 +53,5 @@ export default class MechsList extends Component {
3153
);
3254
}
3355
}
56+
57+
export default connect(mapState, actions)(MechsList);

src/features/mechs/MechsList/MechsListRow.jsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
import React from "react";
2+
import {connect} from "react-redux";
23
import {Table} from "semantic-ui-react";
34

45
import {getWeightClass} from "../mechSelectors";
56

67

7-
const MechsListRow = ({mech, onMechClicked, selected}) => {
8+
import schema from "app/schema";
9+
10+
const mapState = (state, ownProps) => {
11+
const session = schema.from(state.entities);
12+
const {Mech} = session;
13+
14+
let mech;
15+
16+
if(Mech.hasId(ownProps.mechID)) {
17+
const mechModel = Mech.withId(ownProps.mechID);
18+
19+
mech = {
20+
// Copy the data from the plain JS object
21+
...mechModel.ref,
22+
// Provide a default empty object for the relation
23+
mechType : {},
24+
};
25+
26+
if(mechModel.type) {
27+
// Replace the default object with a copy of the relation's data
28+
mech.mechType = {...mechModel.type.ref};
29+
}
30+
}
31+
32+
return {mech};
33+
}
34+
35+
36+
const MechsListRow = ({mech = {}, onMechClicked, selected}) => {
837
const {
938
id = null,
1039
type = "",
@@ -40,4 +69,4 @@ const MechsListRow = ({mech, onMechClicked, selected}) => {
4069
}
4170

4271

43-
export default MechsListRow;
72+
export default connect(mapState)(MechsListRow);

0 commit comments

Comments
 (0)