Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

full working #195

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
actions
  • Loading branch information
serjt12 committed Jan 24, 2018
commit 9d72a23d5f20698561b9976a40d621fd5c532b78
15 changes: 15 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from "axios";

export const FETCH_POSTS = "fetch_posts";
const ROOT_URL = "https://reduxblog.herokuapp.com/api";
const API_KEY = "?key=mykey4445";

export function fetchPosts() {
const url = `${ROOT_URL}/posts${API_KEY}`;
const request = axios.get(url);

return {
type: FETCH_POSTS,
payload: request
};
}
9 changes: 0 additions & 9 deletions src/components/app.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/components/post_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { fetchPosts } from "../actions";

class PostIndex extends Component {
componentDidMount() {
this.props.fetchPosts();
}

render() {

return <div>Post Index</div>;
}
}

export default connect(null, { fetchPosts })(PostIndex);
27 changes: 17 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import { BrowserRouter, Route } from "react-router-dom";
import promise from "redux-promise";

import App from './components/app';
import reducers from './reducers';
import reducers from "./reducers";
import PostIndex from "./components/post_index";

const createStoreWithMiddleware = applyMiddleware()(createStore);
const createStoreWithMiddleware = applyMiddleware(promise)(createStore);

ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.querySelector('.container'));
<BrowserRouter>
<div>
<Route path="/" component={PostIndex} />
</div>
</BrowserRouter>
</Provider>,
document.querySelector(".container")
);
3 changes: 2 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { combineReducers } from 'redux';
import PostReducer from './reducer_posts'

const rootReducer = combineReducers({
state: (state = {}) => state
posts: PostReducer
});

export default rootReducer;
12 changes: 12 additions & 0 deletions src/reducers/reducer_posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import _ from "lodash";
import { FETCH_POSTS } from "../actions";

export default function(state = {}, action) {
switch (action.type) {
case FETCH_POSTS:
return _.mapKeys(action.payload.data, 'id');

default:
return state;
}
}