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

Introduce blog app with derived data for TopAuthor component #1

Open
wants to merge 2 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
27 changes: 27 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Original Code Copyright:

The MIT License (MIT)

Copyright (c) 2015 Dan Abramov
Expand All @@ -19,3 +21,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Modifications:

The MIT License (MIT)

Copyright (c) 2015 Nathan Wenneker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Derived Data Experiment ##

This branch is intended to explore some solutions to the question posed here:
htps://github.com/rackt/redux/issues/291#issuecomment-125047678

Do ActionCreators need to know how to call several API's to update various components?

vladar suggested a couple of approaches:

1. Move the API calls into the reducers
2. Allow reducers to emit side effects for the API calls.

My general approach is going to be:

1. topAuthor is actually derived data, so it doesn't belong in the client state (except possibly
as cached data)

2. if all state is on the client (i.e. no API's), this is obvious --- update the state with the raw data, and the component itself (or something like reselect) is responsible for computing the derived data

3. if the state is stored on the server: Will address in future commit.


Empty file added actions/index.js
Empty file.
5 changes: 5 additions & 0 deletions actions/postActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const ADD_POST = 'ADD_POST';

export function addPost(title, authorName) {
return { type: ADD_POST, title: title, authorName: authorName };
}
25 changes: 0 additions & 25 deletions actions/todos.js

This file was deleted.

Empty file added components/.keep
Empty file.
41 changes: 41 additions & 0 deletions components/AddPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { addPost } from '../actions/postActions'

class AddPost extends Component {

constructor(props, context) {
super(props, context);
this.state = { title: '', author: '' };
}

setTitle(newTitle) {
this.setState({ title: newTitle });
}

setAuthor(newAuthor) {
this.setState({ author: newAuthor });
}

add(e) {
e.preventDefault();
this.props.dispatch(addPost(this.state.title, this.state.author));
this.setState({ title: '', author: '' });
}

render() {

return (
<div>
<h1>Add Post</h1>
<form onSubmit={(e) => this.add(e)}>
Title: <input value={this.state.title} onChange={(e) => this.setTitle(e.target.value)} type="text"/><br/>
Author: <input value={this.state.author} onChange={(e) => this.setAuthor(e.target.value)} type="text"/><br/>
<button type="submit">Submit</button>
</form>
</div>
)
}
}

export default connect()(AddPost)
28 changes: 28 additions & 0 deletions components/AllAuthors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'

class AllAuthors extends Component {

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

const authorItems = authors.map((author) => <li>{author.name}</li>);

return (
<div>
<h1>Authors</h1>
<ul>
{authorItems}
</ul>
</div>
)
}
}

const mapStateToProps = function(state) {
return {
authors: state.authors
}
}

export default connect(mapStateToProps)(AllAuthors)
73 changes: 0 additions & 73 deletions components/Footer.js

This file was deleted.

27 changes: 0 additions & 27 deletions components/Header.js

This file was deleted.

86 changes: 0 additions & 86 deletions components/MainSection.js

This file was deleted.

36 changes: 36 additions & 0 deletions components/Posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'

class Posts extends Component {

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

const postRows = posts.map((post) => <tr><td>{post.title}</td><td>{post.authorName}</td></tr>);

return (
<div>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
</tr>
</thead>
<tbody>
{postRows}
</tbody>
</table>
</div>
)
}
}

const mapStateToProps = function(state) {
return {
posts: state.posts
}
}

export default connect(mapStateToProps)(Posts)
Loading