Skip to content

Commit 7a5b756

Browse files
committed
Connect the Mechs component to display a list of mechs from the store
1 parent 3b25f24 commit 7a5b756

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/features/mechs/MechDetails/MechDetails.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import {getWeightClass} from "../mechSelectors";
66
const MechDetails = ({mech={}}) => {
77
const {
88
id = "",
9-
name = "",
109
type = "",
11-
weight = "",
10+
mechType = {},
1211
} = mech;
1312

13+
const {
14+
name = "",
15+
weight = "",
16+
} = mechType;
17+
18+
1419
const weightClass = getWeightClass(weight);
1520

1621
return (

src/features/mechs/Mechs/Mechs.jsx

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

34
import {
45
Grid,
56
Segment,
67
Header,
78
} from "semantic-ui-react";
89

10+
import schema from "app/schema";
11+
912
import MechsList from "../MechsList";
1013
import MechDetails from "../MechDetails";
1114

12-
const mechs = [
13-
{
14-
id : 1,
15-
name : "Warhammer",
16-
type : "WHM-6R",
17-
weight : 70,
18-
}
19-
];
2015

21-
class Mechs extends Component {
22-
state = {
23-
mechs : mechs,
24-
}
16+
const mapState = (state) => {
17+
const session = schema.from(state.entities);
18+
const {Mech} = session;
19+
20+
const mechs = Mech.all().withModels.map(mechModel => {
21+
const mech = {
22+
// Copy the data from the plain JS object
23+
...mechModel.ref,
24+
// Provide a default empty object for the relation
25+
mechType : {},
26+
};
27+
28+
if(mechModel.type) {
29+
// Replace the default object with a copy of the relation's data
30+
mech.mechType = {...mechModel.type.ref};
31+
}
2532

33+
return mech;
34+
});
35+
36+
return {mechs}
37+
}
38+
39+
class Mechs extends Component {
2640
render() {
27-
const {mechs} = this.state;
41+
const {mechs = []} = this.props;
2842

2943
const currentMech = mechs[0] || {};
3044

@@ -46,4 +60,6 @@ class Mechs extends Component {
4660
);
4761
}
4862
}
49-
export default Mechs;
63+
64+
65+
export default connect(mapState)(Mechs);

src/features/mechs/MechsList/MechsListRow.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import {getWeightClass} from "../mechSelectors";
77
const MechsListRow = ({mech={}}) => {
88
const {
99
id = "",
10-
name = "",
1110
type = "",
12-
weight = "",
11+
mechType = {},
1312
} = mech;
13+
14+
const {
15+
name = "",
16+
weight = "",
17+
} = mechType;
18+
1419
const weightClass = getWeightClass(weight);
1520

1621
return (
@@ -35,4 +40,4 @@ const MechsListRow = ({mech={}}) => {
3540
}
3641

3742

38-
export default MechsListRow;
43+
export default MechsListRow;

0 commit comments

Comments
 (0)