Skip to content

Commit

Permalink
add items working
Browse files Browse the repository at this point in the history
  • Loading branch information
jbkly committed Feb 25, 2016
1 parent afe0a9e commit d9c7aca
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 128 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A simple API layer to model an inventory",
"main": "server.js",
"scripts": {
"server": "nodemon server --ignore public/ --ignore store/",
"test": " "
},
"author": "jbkly",
Expand All @@ -14,5 +15,8 @@
"inert": "^3.2.0",
"joi": "^8.0.2",
"vision": "^4.0.1"
},
"devDependencies": {
"nodemon": "^1.9.0"
}
}
Binary file added public/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Inventory Demo</title>
<!-- <link rel="stylesheet" href="css/base.css" /> -->
<link rel="stylesheet" href="css/base.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
Expand Down
228 changes: 113 additions & 115 deletions public/js/app.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,118 @@
// const CommentBox = React.createClass({
// loadCommentsFromServer: function() {
// $.ajax({
// url: this.props.url,
// dataType: 'json',
// cache: false,
// success: data => this.setState({data}),
// error: (xhr, status, err) => console.error(this.props.url, status, err.toString())
// });
// },
// handleCommentSubmit: function(comment) {
// // optimistically update UI before hearing back from server
// var comments = this.state.data;
// comment.id = Date.now(); // create a temporary id, will be replaced by the server-gen id
// var newComments = comments.concat([comment]);
// this.setState({data: newComments});
const InventorySystem = React.createClass({
loadInventory: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: reply => this.setState({data: reply.data}),
error: (xhr, status, err) => console.error(this.props.url, status, err.toString())
});
},
handleAddToInventory: function(item) {
// optimistically update UI before hearing back from server
let items = this.state.data;
let updatedItems = Object.assign(items);
updatedItems[item.label] = item;
this.setState({data: updatedItems});

// $.ajax({
// url: this.props.url,
// dataType: 'json',
// type: 'POST',
// data: comment,
// success: data => this.setState({data}),
// error: (xhr, status, err) => {r
// this.setState({data: comments});
// console.errer(this.props.url, status, err.toString())
// }
// });
// },
// getInitialState: function() {
// return {data: []};
// },
// componentDidMount: function() {
// this.loadCommentsFromServer();
// setInterval(this.loadCommentsFromServer, this.props.pollInterval);
// },
// render: function() {
// return (
// <div className='CommentBox'>
// <h1>Comments</h1>
// <CommentList data={this.state.data} />
// <CommentForm onCommentSubmit={this.handleCommentSubmit} />
// </div>
// );
// }
// });
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: item,
success: reply => this.setState({data: reply.data}),
error: (xhr, status, err) => {
this.setState({data: items});
console.errer(this.props.url, status, err.toString())
}
});
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadInventory();
setInterval(this.loadInventory, this.props.pollInterval);
},
render: function() {
return (
<div className='inventory'>
<h1>Inventory System</h1>
<ItemList data={this.state.data} />
<AddItemForm onAddItem={this.handleAddToInventory} />
</div>
);
}
});

// const CommentList = React.createClass({
// render: function() {
// var commentNodes = this.props.data.map(function(comment) {
// return (
// <Comment author={comment.author} key={comment.id}>{comment.text}</Comment>
// );
// });
// return (
// <div className='commentList'>
// {commentNodes}
// </div>
// );
// }
// });
const ItemList = React.createClass({
render: function() {
let itemList = Array.from(Object.keys(this.props.data), key => {
let item = this.props.data[key];
return (
<Item label={item.label} type={item.type} expiration={item.expiration} key={item.label}>{item.label}</Item>
);
});
return (
<div className='itemList'>
{itemList}
</div>
);
}
});

// const CommentForm = React.createClass({
// getInitialState: function() {
// return {author: '', text: ''};
// },
// handleAuthorChange: function(e) {
// this.setState({author: e.target.value});
// },
// handleTextChange: function(e) {
// this.setState({text: e.target.value});
// },
// handleSubmit: function(e) {
// e.preventDefault();
// let author = this.state.author.trim();
// let text = this.state.text.trim();
// if (!text || !author) return;
// this.props.onCommentSubmit({author, text});
// this.setState({author: '', text: ''});
// },
// render: function() {
// return (
// <form className='commentForm' onSubmit={this.handleSubmit}>
// <input
// type='text'
// placeholder='Your Name'
// value={this.state.author}
// onChange={this.handleAuthorChange}
// />
// <input
// type='text'
// placeholder='Say something...'
// value={this.state.text}
// onChange={this.handleTextChange}
// />
// <input type='submit' value='Post' />
// </form>
// );
// }
// });
const AddItemForm = React.createClass({
getInitialState: function() {
return {label: '', type: '', expiration: ''};
},
handleLabelChange: function(e) {
this.setState({label: e.target.value});
},
handleTypeChange: function(e) {
this.setState({type: e.target.value});
},
handleSubmit: function(e) {
e.preventDefault();
let label = this.state.label.trim();
let type = this.state.type.trim();
if (!type || !label) return;
this.props.onAddItem({label, type});
this.setState({label: '', type: '', expiration: ''});
},
render: function() {
return (
<form className='addItemForm' onSubmit={this.handleSubmit}>
<input
type='text'
placeholder='Item Label'
value={this.state.label}
onChange={this.handleLabelChange}
/>
<input
type='text'
placeholder='Type'
value={this.state.type}
onChange={this.handleTypeChange}
/>
<input type='submit' value='Post' />
</form>
);
}
});

// const Comment = React.createClass({
// rawMarkup: function() {
// var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
// return { __html: rawMarkup };
// },
// render: function() {
// return (
// <div className='comment'>
// <h2 className='commentAuthor'>{this.props.author}</h2>
// <span dangerouslySetInnerHTML={this.rawMarkup()} />
// </div>
// );
// }
// });
const Item = React.createClass({
render: function() {
return (
<div className='item'>
<h2 className='itemLabel'>{this.props.label}</h2>
<p className='itemType'>Type: {this.props.type}</p>
<p className='itemExpiration'>Expires: {this.props.expiration}</p>
</div>
);
}
});

// ReactDOM.render(
// <CommentBox url='/api/comments' pollInterval={2000} />,
// document.getElementById('content')
// );
ReactDOM.render(
<InventorySystem url='/api/items' pollInterval={2000} />,
document.getElementById('content')
);
17 changes: 5 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ const routes = [

server.route(routes);

server.start((err) => {
if (err) {
console.log(err);
} else {
console.log('server running at: ', server.info.uri);
}
});

function getItems(request, reply) {
fs.readFile(INVENTORY, (err, data) => {
let response = err || {
Expand All @@ -100,10 +92,11 @@ function addItem(request, reply) {
console.error(err);
return reply(err).code(500);
}

let items = JSON.parse(data),
label = encodeURIComponent(request.params.label),
type = encodeURIComponent(request.params.type),
expiration = encodeURIComponent(request.params.expiration) || Date.now() + 300000; // 5min
label = request.payload.label,
type = request.payload.type,
expiration = parseInt(request.payload.expiration, 10) || Date.now() + 300000; // 5min

if (items[label]) {
return reply('Item by that label is already in inventory').code(409);
Expand All @@ -124,7 +117,7 @@ function addItem(request, reply) {

function removeItem(request, reply) {
fs.readFile(INVENTORY, (err, data) => {
let label = encodeURIComponent(request.params.label);
let label = decodeURIComponent(request.params.label);
if (err) {
console.error(err);
return reply(err).code(500);
Expand Down
10 changes: 10 additions & 0 deletions store/items.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@
"label": "Pizza",
"type": "Italian",
"expiration": 1456109060070
},
"Croissant-wich": {
"label": "Croissant-wich",
"type": "French-murican",
"expiration": 9283374098374099000
},
"Bison Burger": {
"label": "Bison Burger",
"type": "native murican",
"expiration": 1456375061762
}
}

0 comments on commit d9c7aca

Please sign in to comment.