Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Build Applications with Meteor

You're reading from   Build Applications with Meteor Isomorphic JavaScript web development

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781787129887
Length 388 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dobrin Ganev Dobrin Ganev
Author Profile Icon Dobrin Ganev
Dobrin Ganev
Arrow right icon
View More author details
Toc

The reducer function


We start with the action type INITIAL_LOAD:

export default function cards(state = [], action) {
    switch (action.type) {
    case INITIAL_LOAD:
       return action.data;

Here, we return the new data; notice we do not mutate our state. The second one is the ADD_CARD action:

case ADD_CARD:
if (state.filter(card => card._id === action.id).length > 0) {
      return state;
 }
   return [
    ...state, {
    _id: action.id,
    title: action.title,
    task: action.task,
    status: action.status
   }
 ]

In our app, when we create a new card, we dispatch the ADD_CARD action, and at the same time, we listen for collection changes. Since we are changing the collection (adding a new card), we will dispatch the RECEIVE_CARD action. To prevent adding one item twice, we return the original state if the card is already added. We can add a filter condition in both actions; this way, we won't have to worry about race conditions, especially since the data on the client MongoDB...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime