Skip to content

Commit

Permalink
Allow only one unique note per all lanes when attaching
Browse files Browse the repository at this point in the history
Perhaps there's a neater way to guarantee this in Immutable.js. Now it's
pretty much the old code.
  • Loading branch information
bebraw committed Mar 3, 2016
1 parent 85a8149 commit a07d566
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions immutable_app/app/reducers/lanes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,34 @@ export default function lanes(state = initialState, action) {

case types.DELETE_LANE:
// XXX: this can crash if findIndex fails
return state.delete(state.findIndex(o => o.id === action.id));
return state.delete(state.findIndex(lane => lane.id === action.id));

case types.ATTACH_TO_LANE:
const laneId = action.laneId;
const noteId = action.noteId;

// XXX: this can crash if findIndex fails
return state.update(
state.findIndex(lane => lane.id === laneId),
lane => Object.assign({}, lane, {
notes: [...lane.notes, noteId]
})
return state.map(
lane => {
const noteIndex = lane.notes.indexOf(noteId);

// Delete notes if found
if(noteIndex >= 0) {
return Object.assign({}, lane, {
notes: lane.notes.slice(0, noteIndex).concat(
lane.notes.slice(noteIndex + 1)
)
});
}

// Attach note to the lane
if(lane.id === laneId) {
return Object.assign({}, lane, {
notes: [...lane.notes, noteId]
});
}

return lane;
}
);

case types.DETACH_FROM_LANE:
Expand Down

0 comments on commit a07d566

Please sign in to comment.