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 all commits
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
<body>
<div class="container"></div>
</body>
<script src="/bundle.js"></script>
<script rel="text/babel" src="/bundle.js"></script>
</html>
46 changes: 46 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import axios from "axios";

export const FETCH_POSTS = "fetch_posts";
export const CREATE_POST = "create_posts";
export const FETCH_POST = "fetch_post";
export const DELETE_POST = "delete_post";

const ROOT_URL = "https://reduxblog.herokuapp.com/api";
const API_KEY = "?key=mykey4445";
const url = `${ROOT_URL}/posts${API_KEY}`;

export function fetchPosts() {
const request = axios.get(url);

return {
type: FETCH_POSTS,
payload: request
};
}

export function createPost(values, callback) {
const request = axios.post(url, values).then(() => callback());
return {
type: CREATE_POST,
payload: request
};
}

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

return {
type: FETCH_POST,
payload: request
};
}

export function deletePost(id, callback) {
const request = axios
.delete(`${ROOT_URL}/posts/${id}${API_KEY}`)
.then(() => callback());
return {
type: DELETE_POST,
payload: id
};
}
9 changes: 0 additions & 9 deletions src/components/app.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/posts_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import _ from "lodash";
import React, { Component } from "react";

import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { fetchPosts } from "../actions";

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

renderPosts() {
return _.map(this.props.posts, post => {
return (
<li className="list-group-item" key={post.id}>
<Link to={`/posts/${post.id}`}>{post.title}</Link>
</li>
);
});
}

render() {
return (
<div>
<div className="text-xs-right">
<Link className="btn btn-primary" to="/posts/new">
add a Post
</Link>
</div>

<h3>Posts:</h3>
<ul className="list-group">{this.renderPosts()}</ul>
</div>
);
}
}

function mapStateToProps(state) {
return { posts: state.posts };
}

export default connect(mapStateToProps, { fetchPosts })(PostIndex);
91 changes: 91 additions & 0 deletions src/components/posts_new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { Component } from "react";
import { Link } from "react-router-dom";
import { Field, reduxForm } from "redux-form";
import {connect} from 'react-redux';
import {createPost} from '../actions';

class PostsNew extends Component {
renderField(field) {
const { meta: { touched, error} } =field;
const className=`form-group ${touched && error ? 'has-danger': ''}`;
return (
<div className={className}>
<label>{field.label}</label>
<input className="form-control" type="text" {...field.input} />
<div className="text-help">
{touched ? error: ''}
</div>

</div>
);
}

onSubmit(values){

this.props.createPost(values, () => {
this.props.history.push('/');

});

}

render() {
const { handleSubmit } = this.props
return (
<div>


<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<Field
label="Title of your post"
name="title"
component={this.renderField}
/>
<Field
label="Categories"
name="categories"
component={this.renderField}
/>
<Field
label="Post Content"
name="content"
component={this.renderField}
/>
<button type="submit" className="btn btn-primary">
Submit
</button>
<Link to="/" className="btn btn-danger margin">
Cancel
</Link>
</form>
</div>
);
}
}

function validate(values) {
const errors = {};

//Validate the inputs from 'values'
if (!values.title) {
errors.title = "Enter a title!";
}
if (!values.categories) {
errors.categories = "Enter a categories!";
}
if (!values.content) {
errors.content = "Enter some content!";
}

// If errors is empty the form is fine to submit
//If errors has *any* properties, redux form assumes form is valid
return errors;
}

export default reduxForm({
validate,
form: "PostsNewForm"
})(
connect(null,{ createPost })(PostsNew)
);

52 changes: 52 additions & 0 deletions src/components/posts_show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { fetchPost, deletePost } from "../actions";
import { Link } from "react-router-dom";

class PostsShow extends Component {
componentDidMount() {
//To fetch info just once
// if (!this.props.post) {
const { id } = this.props.match.params;
this.props.fetchPost(id);
}
// }

onDeleteClick(){
const { id } = this.props.match.params;
this.props.deletePost(id, () => {
this.props.history.push('/');
});
}

render() {
const { post } = this.props;

if (!post) {
return <div>Loading...</div>;
}

return (
<div>
<Link className="btn btn-info" to="/">
Back to index
</Link>
<button
className="btn btn-danger pull-xs-right"
onClick={this.onDeleteClick.bind(this)}


>Delete Post</button>
<h3>{post.title}</h3>
<h6>Categories:{post.categories} </h6>
<p>{post.content}</p>
</div>
);
}
}

function mapStateToProps({ posts }, ownProps) {
return { post: posts[ownProps.match.params.id] };
}

export default connect(mapStateToProps, { fetchPost, deletePost })(PostsShow);
34 changes: 24 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
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, Switch } from "react-router-dom";
import promise from "redux-promise";
import '/style/style.css';

import App from './components/app';
import reducers from './reducers';
import reducers from "./reducers";
import PostsIndex from "./components/posts_index";
import PostsNew from "./components/posts_new";
import PostsShow from "./components/posts_show";

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

ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.querySelector('.container'));
<BrowserRouter>
<div>
<Switch>
<Route path="/posts/new" component={PostsNew} />
<Route path="/posts/:id" component={PostsShow} />
<Route path="/" component={PostsIndex} />
</Switch>
</div>
</BrowserRouter>
</Provider>,
document.querySelector(".container")
);
5 changes: 4 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { combineReducers } from 'redux';
import { reducer as formReducer} from 'redux-form';
import PostReducer from './reducer_posts'

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

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

export default function(state = {}, action) {
switch (action.type) {
case DELETE_POST:
return _.omit(state, action.payload);

case FETCH_POST:
// const post = action.payload.data;
// const newState = { ...state };
// newState[post.id] = post;
// return newState;

return { ...state, [action.payload.data.id]: action.payload.data };

case FETCH_POSTS:
return _.mapKeys(action.payload.data, "id");

default:
return state;
}
}
4 changes: 4 additions & 0 deletions style/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
form a{
margin-left:10px;

}