Book Image

Build Applications with Meteor

Book Image

Build Applications with Meteor

Overview of this book

This book starts with the basic installation and overview of the main components in Meteor. You’ll get hands-on multiple versatile applications covering a wide range of topics from adding a front-end views with the hottest rendering technology React to implementing a microservices oriented architecture.All the code is written with ES6/7 which is the latest significantly improved JavaScript language. We’ll also look at real-time data streaming, server to server data exchange, responsive styles on the front-end, full-text search functionality, and integration of many third-party libraries and APIs using npm. By the end of the book, you’ll have the skills to quickly prototype and even launch your next app idea in a matter of days.
Table of Contents (16 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
8
Build a Chatbot with Facebook’s Messenger Platform

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...