Fix to submit form when creating issue comment.

This commit is contained in:
shimamoto
2013-06-25 14:11:01 +09:00
parent 1a4510201f
commit 317e2b6ea3
3 changed files with 29 additions and 55 deletions

View File

@@ -14,11 +14,16 @@ trait IssuesControllerBase extends ControllerBase {
with UsersOnlyAuthenticator => with UsersOnlyAuthenticator =>
case class IssueForm(title: String, content: Option[String]) case class IssueForm(title: String, content: Option[String])
case class CommentForm(issueId: Int, content: String)
val form = mapping( val form = mapping(
"title" -> trim(label("Title", text(required))), "title" -> trim(label("Title", text(required))),
"content" -> trim(optional(text())) "content" -> trim(optional(text()))
)(IssueForm.apply) )(IssueForm.apply)
val commentForm = mapping(
"issueId" -> label("Issue Id", number()),
"content" -> trim(label("Comment", text(required)))
)(CommentForm.apply)
get("/:owner/:repository/issues"){ get("/:owner/:repository/issues"){
val owner = params("owner") val owner = params("owner")
@@ -68,21 +73,12 @@ trait IssuesControllerBase extends ControllerBase {
}) })
// TODO requires users only and redable repository checking // TODO requires users only and redable repository checking
post("/:owner/:repository/issue_comments")( usersOnly { post("/:owner/:repository/issue_comments", commentForm)( usersOnly { form =>
val owner = params("owner") val owner = params("owner")
val repository = params("repository") val repository = params("repository")
val issueId = params("issueId").toInt
val content = params("content") // TODO input check
contentType = formats("json") redirect("/%s/%s/issues/%d#comment-%d".format(owner, repository, form.issueId,
saveComment(owner, repository, context.loginAccount.get.userName, issueId, content) map { saveComment(owner, repository, context.loginAccount.get.userName, form.issueId, form.content)))
model => org.json4s.jackson.Serialization.write(
Map("commentedUserName" -> model.commentedUserName,
"registeredDate" -> view.helpers.datetime(model.registeredDate),
"content" -> view.Markdown.toHtml(
model.content, getRepository(owner, repository, baseUrl).get, false, true, true)
))
} getOrElse ""
}) })
} }

View File

@@ -60,15 +60,13 @@ trait IssuesService {
def saveComment(owner: String, repository: String, loginUser: String, def saveComment(owner: String, repository: String, loginUser: String,
issueId: Int, content: String) = issueId: Int, content: String) =
Query(IssueComments) filter { IssueComments.autoInc insert (
_.commentId is ( IssueComments.autoInc insert ( owner,
owner, repository,
repository, issueId,
issueId, loginUser,
loginUser, content,
content, currentDate,
currentDate, currentDate)
currentDate) ).bind
} firstOption
} }

View File

@@ -19,25 +19,26 @@
@markdown(issue.content getOrElse "No description given.", repository, false, true, true) @markdown(issue.content getOrElse "No description given.", repository, false, true, true)
</div> </div>
</div> </div>
<span id="comment-area">
@comments.map { comment => @comments.map { comment =>
<div class="box"> <div class="box" id="comment-@comment.commentId">
<div class="box-header-small"> <div class="box-header-small">
<a href="@path/@comment.commentedUserName">@comment.commentedUserName</a> commented <a href="@path/@comment.commentedUserName">@comment.commentedUserName</a> commented
<span class="pull-right">@datetime(comment.registeredDate)</span> <span class="pull-right">@datetime(comment.registeredDate)</span>
</div>
<div class="box-content" style="background-color: #f5f5f5;">
@markdown(comment.content, repository, false, true, true)
</div>
</div> </div>
<div class="box-content" style="background-color: #f5f5f5;">
@markdown(comment.content, repository, false, true, true)
</div>
</div>
} }
</span> <form action="@path/@repository.owner/@repository.name/issue_comments" method="POST" validate="true">
<div class="box"> <div class="box">
<div class="box-content"> <div class="box-content">
@html.preview(repository, "", false, true, true, "width: 730px; height: 100px;") @html.preview(repository, "", false, true, true, "width: 730px; height: 100px;")
</div> </div>
</div> </div>
<input type="button" class="btn btn-success" value="Comment" id="comment"/> <input type="hidden" name="issueId" value="@issue.issueId"/>
<input type="submit" class="btn btn-success" value="Comment"/>
</form>
</div> </div>
<div class="span2"> <div class="span2">
@if(issue.closed) { @if(issue.closed) {
@@ -50,25 +51,4 @@
<strong>Labels</strong> <strong>Labels</strong>
</div> </div>
</div> </div>
} }
<script>
$(function(){
$('#comment').click(function(){
$.post('@path/@repository.owner/@repository.name/issue_comments',
{
issueId : @issue.issueId,
content : $('#content').val()
},
function(data){
var div = $('<div>').addClass('box')
.append($('<div>').addClass('box-header-small')
.append($('<a>').attr('href', '@path/@repository.owner').text(data.commentedUserName))
.append(' commented')
.append($('<span>').addClass('pull-right').text(data.registeredDate)))
.append($('<div>').addClass('box-content').attr('style', 'background-color: #f5f5f5;').html(data.content));
$('#comment-area').append(div);
$('#content').val('');
});
});
});
</script>