Permalink
Browse files

Implement logic to add and edit a new pilot

  • Loading branch information...
markerikson committed Dec 29, 2017
1 parent 1c8e64b commit 0af9785c8e19ad7282956e550c83c6981c37d017
Showing with 44 additions and 1 deletion.
  1. +6 −1 src/features/entities/entitySelectors.js
  2. +17 −0 src/features/pilots/Pilot.js
  3. +21 −0 src/features/pilots/pilotsActions.js
@@ -7,4 +7,9 @@ export const selectEntities = state => state.entities;
export const getEntitiesSession = createSelector(
selectEntities,
entities => orm.session(entities)
);
);
export const getUnsharedEntitiesSession = (state) => {
const entities = selectEntities(state);
return orm.session(entities);
}
@@ -1,5 +1,13 @@
import {Model, fk, attr} from "redux-orm";
const defaultAttributes = {
name : "New Pilot",
rank : "Private",
gunnery : 4,
piloting : 5,
age : 25,
};
export default class Pilot extends Model {
static get fields() {
@@ -24,6 +32,15 @@ export default class Pilot extends Model {
return this.upsert(pilotData);
}
static generate(newAttributes = {}) {
const combinedAttributes = {
...defaultAttributes,
...newAttributes,
};
return this.create(combinedAttributes);
}
toJSON() {
return {...this.ref};
}
@@ -1,5 +1,8 @@
import cuid from "cuid";
import {
editExistingItem,
editNewItem,
applyItemEdits,
stopEditingItem
} from "features/editing/editingActions";
@@ -11,6 +14,7 @@ import {
} from "./pilotsConstants";
import {selectCurrentPilot, selectIsEditingPilot} from "./pilotsSelectors";
import {getUnsharedEntitiesSession} from "features/entities/entitySelectors";
export function selectPilot(pilotID) {
return (dispatch, getState) => {
@@ -56,3 +60,20 @@ export function cancelEditingPilot() {
dispatch(stopEditingItem("Pilot", currentPilot));
}
}
export function addNewPilot() {
return (dispatch, getState) => {
const session = getUnsharedEntitiesSession(getState());
const {Pilot} = session;
const id = cuid();
const newPilot = Pilot.generate({id});
const pilotContents = newPilot.toJSON();
dispatch(editNewItem("Pilot", id, pilotContents));
dispatch(selectPilot(id));
dispatch({type : PILOT_EDIT_START});
}
}

0 comments on commit 0af9785

Please sign in to comment.