Skip to content

Commit ddba948

Browse files
committed
adding author component
1 parent daa2bfc commit ddba948

File tree

8 files changed

+5411
-174
lines changed

8 files changed

+5411
-174
lines changed

dist/scripts/bundle.js

Lines changed: 5275 additions & 163 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"bootstrap": "^3.3.5",
1313
"browserify": "^11.0.1",
1414
"flux": "^2.0.3",
15-
"gulp": "^3.9.0",
15+
"gulp": "^3.9.1",
1616
"gulp-concat": "^2.6.0",
1717
"gulp-connect": "^2.2.0",
1818
"gulp-eslint": "^0.15.0",
1919
"gulp-open": "^1.0.0",
2020
"jquery": "^2.1.4",
21+
"loadash": "^1.0.0",
2122
"react": "^0.13.3",
2223
"react-router": "^0.13.3",
2324
"reactify": "^1.1.1",

src/api/authorApi.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
//This file is mocking a web API by hitting hard coded data.
4+
var authors = require('./authorData').authors;
5+
var _ = require('lodash');
6+
7+
//This would be performed on the server in a real app. Just stubbing in.
8+
var _generateId = function(author) {
9+
return author.firstName.toLowerCase() + '-' + author.lastName.toLowerCase();
10+
};
11+
12+
var _clone = function(item) {
13+
return JSON.parse(JSON.stringify(item)); //return cloned copy so that the item is passed by value instead of by reference
14+
};
15+
16+
var AuthorApi = {
17+
getAllAuthors: function() {
18+
return _clone(authors);
19+
},
20+
21+
getAuthorById: function(id) {
22+
var author = _.find(authors, {id: id});
23+
return _clone(author);
24+
},
25+
26+
saveAuthor: function(author) {
27+
//pretend an ajax call to web api is made here
28+
console.log('Pretend this just saved the author to the DB via AJAX call...');
29+
30+
if (author.id) {
31+
var existingAuthorIndex = _.indexOf(authors, _.find(authors, {id: author.id}));
32+
authors.splice(existingAuthorIndex, 1, author);
33+
} else {
34+
//Just simulating creation here.
35+
//The server would generate ids for new authors in a real app.
36+
//Cloning so copy returned is passed by value rather than by reference.
37+
author.id = _generateId(author);
38+
authors.push(author);
39+
}
40+
41+
return _clone(author);
42+
},
43+
44+
deleteAuthor: function(id) {
45+
console.log('Pretend this just deleted the author from the DB via an AJAX call...');
46+
_.remove(authors, { id: id});
47+
}
48+
};
49+
50+
module.exports = AuthorApi;

src/api/authorData.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
authors:
3+
[
4+
{
5+
id: 'cory-house',
6+
firstName: 'Cory',
7+
lastName: 'House'
8+
},
9+
{
10+
id: 'scott-allen',
11+
firstName: 'Scott',
12+
lastName: 'Allen'
13+
},
14+
{
15+
id: 'dan-wahlin',
16+
firstName: 'Dan',
17+
lastName: 'Wahlin'
18+
}
19+
]
20+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict"
2+
3+
var React = require('react');
4+
5+
var AuthorApi = require('../../api/authorApi');
6+
7+
var Authors = React.createClass({
8+
getInitialState: function(){
9+
return {
10+
authors: []
11+
}
12+
},
13+
14+
componentWillMount: function(){
15+
this.setState({ authors: AuthorApi.getAllAuthors()});
16+
},
17+
18+
render: function(){
19+
var createAuthorRow = function(author){
20+
return (
21+
<tr key={author.id}>
22+
<td><a href={"/#authors" + author.id}>{author.id}</a></td>
23+
<td>{author.firstName} {author.lastName}</td>
24+
</tr>
25+
);
26+
}
27+
return (
28+
<div>
29+
<h1>Author</h1>
30+
31+
<table className="table">
32+
<thead>
33+
<tr>
34+
<th>ID</th>
35+
<th>Name</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
{this.state.authors.map(createAuthorRow, this)}
40+
</tbody>
41+
</table>
42+
</div>
43+
);
44+
}
45+
});
46+
47+
module.exports = Authors;

src/components/common/header.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ var Header = React.createClass({
1111
<img src="images/pluralsight-logo.png"/>
1212
</a>
1313

14-
<ul className="nav nvarbar-nav">
14+
<ul className="nav navbar-nav">
1515
<li><a href="/">Home</a></li>
16+
<li><a href="/#authors">Authors</a></li>
1617
<li><a href="/#about">About</a></li>
1718
</ul>
1819
</div>

src/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$ = JQuery = require('jquery');
22
var React = require('react');
33
var Home = require('./components/homePage');
4+
var Author = require('./components/author/authorPage');
45
var About = require('./components/about/aboutPage');
56
var Header = require('./components/common/header');
67
(function(win){
@@ -11,8 +12,8 @@ var Header = require('./components/common/header');
1112
var Child;
1213

1314
switch (this.props.route){
14-
case 'about': Child = About;
15-
break;
15+
case 'about': Child = About; break;
16+
case 'authors': Child= Author; break;
1617
default: Child = Home;
1718
}
1819

0 commit comments

Comments
 (0)