Permalink
Browse files

Load Mechs and MechDesigns from sample data and use in display

Added Mech and MechDesign entries to the sample data
Added reducer logic to load them into the store
Updated Pilots mapState to use relations to look up mech type
  • Loading branch information...
markerikson committed Dec 8, 2016
1 parent f28fee9 commit 3b25f24b5b7b74dd11d27d58134b11b35802a527
Showing with 154 additions and 19 deletions.
  1. +7 −4 src/app/reducers/entitiesReducer.js
  2. +121 −12 src/data/sampleData.js
  3. +26 −3 src/features/pilots/Pilots/Pilots.jsx
@@ -9,12 +9,15 @@ const initialState = schema.getDefaultState();
export function loadData(state, payload) {
// Create a Redux-ORM session from our entities "tables"
const session = schema.from(state);
// Get a reference to the correct version of the Pilots class for this Session
const {Pilot} = session;
// Get a reference to the correct version of model classes for this Session
const {Pilot, MechDesign, Mech} = session;
const {pilots} = payload;
// Queue up creation commands for each pilot entry
const {pilots, designs, mechs} = payload;
// Queue up creation commands for each entry
pilots.forEach(pilot => Pilot.parse(pilot));
designs.forEach(design => MechDesign.parse(design));
mechs.forEach(mech => Mech.parse(mech));
// Apply the queued updates and return the updated "tables"
return session.reduce();
View
@@ -11,7 +11,7 @@ const sampleData = {
gunnery : 2,
piloting : 2,
age : 52,
mechType : "WHM-6R",
mech : 1,
},
{
id : 2,
@@ -20,7 +20,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 43,
mechType : "MAD-3R",
mech : 2,
},
{
id : 3,
@@ -29,7 +29,7 @@ const sampleData = {
gunnery : 4,
piloting : 5,
age : 27,
mechType : "CRD-3R",
mech : 3,
},
{
id : 4,
@@ -38,7 +38,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 34,
mechType : "GRF-1N",
mech : 4,
},
{
id : 5,
@@ -47,7 +47,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 41,
mechType : "ARC-2R",
mech : 5,
},
{
id : 6,
@@ -56,7 +56,7 @@ const sampleData = {
gunnery : 4,
piloting : 4,
age : 31,
mechType : "ARC-2R",
mech : 6,
},
{
id : 7,
@@ -65,7 +65,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 39,
mechType : "WSP-1A",
mech : 7,
},
{
id : 8,
@@ -74,7 +74,7 @@ const sampleData = {
gunnery : 4,
piloting : 5,
age : 36,
mechType : "WSP-1A",
mech : 8,
},
{
id : 9,
@@ -83,7 +83,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 40,
mechType : "RFL-3N",
mech : 9,
},
{
id : 10,
@@ -92,7 +92,7 @@ const sampleData = {
gunnery : 4,
piloting : 5,
age : 37,
mechType : "PXH-1K",
mech : 10,
},
{
id : 11,
@@ -101,7 +101,7 @@ const sampleData = {
gunnery : 3,
piloting : 4,
age : 32,
mechType : "STG-3R",
mech : 11,
},
{
id : 12,
@@ -110,8 +110,117 @@ const sampleData = {
gunnery : 3,
piloting : 5,
age : 29,
mechType : "STG-3R",
mech : 12,
},
],
designs : [
{
id : "STG-3R",
name : "Stinger",
weight : 20,
},
{
id : "WSP-1A",
name : "Wasp",
weight : 20,
},
{
id : "PXH-1K",
name : "Phoenix Hawk",
weight : 45,
},
{
id : "GRF-1N",
name : "Griffin",
weight : 55,
},
{
id : "RFL-3N",
name : "Rifleman",
weight : 60,
},
{
id : "CRD-3R",
name : "Crusader",
weight : 65,
},
{
id : "ARC-2R",
name : "Archer",
weight : 70,
},
{
id : "WHM-6R",
name : "Warhammer",
weight : 70,
},
{
id : "MAD-3R",
name : "Marauder",
weight : 75,
},
],
mechs : [
{
id : 1,
type : "WHM-6R",
pilot : 1,
},
{
id : 2,
type : "MAD-3R",
pilot : 2,
},
{
id : 3,
type : "CRD-3R",
pilot : 3,
},
{
id : 4,
type : "GRF-1N",
pilot : 4,
},
{
id : 5,
type : "ARC-2R",
pilot : 5,
},
{
id : 6,
type : "ARC-2R",
pilot : 6,
},
{
id : 7,
type : "WSP-1A",
pilot : 7,
},
{
id : 8,
type : "STG-3R",
pilot : 8,
},
{
id : 9,
type : "RFL-3N",
pilot : 9,
},
{
id : 10,
type : "PXH-1K",
pilot : 10,
},
{
id : 11,
type : "STG-3R",
pilot : 11,
},
{
id : 12,
type : "STG-3R",
pilot : 12,
}
]
};
@@ -30,9 +30,32 @@ const mapState = (state) => {
// The QuerySet that is returned from all() can be used to
// retrieve instances of the Pilot class, or retrieve the
// plain JS objects that are actually in the store.
// The toRefArray() method will give us an array of the
// plain JS objects for each item in the QuerySet.
const pilots = Pilot.all().toRefArray();
// The withModels modifier will let us map over Model instances
// for each entry, rather than the plain JS objects.
const pilots = Pilot.all().withModels.map(pilotModel => {
// Access the underlying plain JS object using the "ref" field,
// and make a shallow copy of it
const pilot = {
...pilotModel.ref
};
// We want to look up pilotModel.mech.mechType. Just in case the
// relational fields are null, we'll do a couple safety checks as we go.
// Look up the associated Mech instance using the foreign-key
// field that we defined in the Pilot Model class
const {mech} = pilotModel;
// If there actually is an associated mech, include the
// mech type's ID as a field in the data passed to the component
if(mech && mech.type) {
pilot.mechType = mech.type.id;
}
return pilot;
});
// Now that we have an array of all pilot objects, return it as a prop
return {pilots};

0 comments on commit 3b25f24

Please sign in to comment.