diff --git a/js/components/commentsForm.js b/js/components/commentsForm.js index 30d899b..97dedf8 100644 --- a/js/components/commentsForm.js +++ b/js/components/commentsForm.js @@ -21,7 +21,21 @@ class CommentsForm extends React.Component { const mapDispatchToProps = function(dispatch){ return { handleSubmit: function(body){ - dispatch({type:'ADD', comment: { body: body }}); + fetch( + 'http://localhost:3000/comments', + { + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + method: "POST", + body: JSON.stringify({ body:body }) + } + ).then(function(response){ + response.json().then(function(data){ + dispatch({type:'ADD', comment: { body: body }}); + }); + }); } } } diff --git a/js/index.js b/js/index.js index e21b9d1..ee05e78 100644 --- a/js/index.js +++ b/js/index.js @@ -28,6 +28,13 @@ import store from './store.js' // }); // }); +fetch('http://localhost:3000/comments').then(function(response){ + response.json().then(function(data){ + store.dispatch({type:'SET', comments:data}); + }); +}); + + ReactDOM.render( diff --git a/js/store.js b/js/store.js index 747588c..879e994 100644 --- a/js/store.js +++ b/js/store.js @@ -4,6 +4,8 @@ let comments = function(state = [], action){ switch(action.type){ case 'ADD': return [...state, action.comment]; + case 'SET': + return action.comments default: return state }