Reload stories immediately

Reload the stories list immediately after successfully adding a new
story.
This commit is contained in:
Tom Willemse 2013-07-07 22:36:51 +02:00
parent eb32c34e1f
commit aece802c32
2 changed files with 34 additions and 25 deletions

View file

@ -97,12 +97,14 @@
parameters) parameters)
,@body)) ,@body))
(define-route stories-new ("stories/new" :method :post) (define-route stories-new ("stories/new" :method :post
:content-type "text/json")
(if (logged-in-p) (if (logged-in-p)
(with-post-parameters ("role" "necessity" "headline" "content") (with-post-parameters ("role" "necessity" "headline" "content")
(post-story role necessity headline content (post-story role necessity headline content
(hunchentoot:session-value :username)) (hunchentoot:session-value :username))
200) (with-output-to-string (out)
(encode-json '((status . "ok")) out)))
403)) 403))
(define-route tasks-new ("stories/tasks/new" :method :post) (define-route tasks-new ("stories/tasks/new" :method :post)

View file

@ -221,30 +221,11 @@ var StoryRow = React.createClass({
}); });
var StoryTable = React.createClass({ var StoryTable = React.createClass({
loadStoriesFromServer: function() {
$.ajax({
url: this.props.url,
mimeType: 'textPlain',
success: function(data) {
this.setState({data: eval(data)});
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentWillMount: function() {
this.loadStoriesFromServer();
setInterval(
this.loadStoriesFromServer.bind(this),
this.props.pollInterval
);
},
handleMoved: React.autoBind(function(direction) { handleMoved: React.autoBind(function(direction) {
this.loadStoriesFromServer(); this.loadStoriesFromServer();
}), }),
render: function() { render: function() {
var storyNodes = this.state.data.map(function (story) { var storyNodes = this.props.data.map(function (story) {
return <StoryRow story={story} onMoved={this.handleMoved} />; return <StoryRow story={story} onMoved={this.handleMoved} />;
}.bind(this)); }.bind(this));
return ( return (
@ -302,19 +283,45 @@ var StoryForm = React.createClass({
}); });
var StoryPage = React.createClass({ var StoryPage = React.createClass({
loadStoriesFromServer: function() {
$.ajax({
url: this.props.url,
mimeType: 'textPlain',
success: function(data) {
this.setState({data: eval(data)});
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentWillMount: function() {
this.loadStoriesFromServer();
setInterval(
this.loadStoriesFromServer.bind(this),
this.props.pollInterval
);
},
handleStorySubmit: React.autoBind(function (story) { handleStorySubmit: React.autoBind(function (story) {
$.ajax({ $.ajax({
url: "/stories/new", url: "/stories/new",
type: "POST", type: "POST",
data: story, data: story,
dataType: 'json', dataType: 'json',
mimeType: 'textPlain' mimeType: 'textPlain',
success: function (data, textStatus, jqXHR) {
if (data.status == "ok")
this.loadStoriesFromServer();
}.bind(this),
error: function (jqXHR, textStatus, errorThrown) {
alert("error: " + errorThrown);
}.bind(this)
}); });
}), }),
render: function() { render: function() {
return ( return (
<div> <div>
<StoryTable url="/stories" pollInterval={5000} /> <StoryTable data={this.state.data} />
<StoryForm onStorySubmit={this.handleStorySubmit} /> <StoryForm onStorySubmit={this.handleStorySubmit} />
</div> </div>
); );
@ -322,6 +329,6 @@ var StoryPage = React.createClass({
}); });
React.renderComponent( React.renderComponent(
<StoryPage />, <StoryPage url="/stories" pollInterval={5000} />,
document.getElementById('content') document.getElementById('content')
); );