Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Last active September 12, 2017 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/4778abb70b0458ed5aa8849a9f90613d to your computer and use it in GitHub Desktop.
Save deanebarker/4778abb70b0458ed5aa8849a9f90613d to your computer and use it in GitHub Desktop.
The simplest comment implementation I could come up with using Episerver Social with the "Alloy" demo site
<form method="post" action="/social/comments/add" id="commentForm">
<input type="hidden" name="pageId" value="@Model.CurrentPage.ContentGuid"/>
<input type="text" name="author" placeholder="Your name..."/>
<textarea name="body" id="commentBody" placeholder="Your comment..."></textarea>
<br/>
<input type="submit" id="commentSubmitButton" disabled="disabled" style="opacity: 0.5;" value="Submit" />
</form>
<div id="commentList">
Loading comments...
</div>
<script>
$(function () {
var commentForm = $('#commentForm');
var commentBody = $('#commentBody');
var commentSubmitButton = $('#commentSubmitButton');
commentBody.keyup(function () {
bodyEmpty = commentBody.val().length == 0;
commentSubmitButton.attr('style', 'opacity: ' + (bodyEmpty ? '0.5' : '1.0'));
commentSubmitButton.prop('disabled', bodyEmpty);
});
commentForm.submit(function (e) {
e.preventDefault();
$.ajax({
type: commentForm.attr('method'),
url: commentForm.attr('action'),
data: commentForm.serialize(),
success: function (html) {
refreshComments(html);
document.getElementById("commentForm").reset();
}
});
});
// This happens on page load...
$.ajax({
type: "POST",
url: '/social/comments/get?id=@Model.CurrentPage.ContentGuid',
success: function (html) {
refreshComments(html);
}
});
});
function refreshComments(html) {
$('#commentList').html(html);
}
</script>
@using EPiServer.Social.Comments.Core
@using EPiServer.Social.Common
@model ResultPage<Comment>
@foreach (var comment in Model.Results)
{
<div class="comment">
<blockquote>@comment.Body</blockquote>
<div class="commentMeta">
by @comment.Author
on @comment.Created.ToLocalTime().ToString("MMM dd, yyyy 'at' h:mm tt")
</div>
</div>
}
<style>
.comment {
margin-bottom: 2em;
}
.comment blockquote {
background-color: rgb(250,250,250);
padding: 1em;
margin-bottom: 2px;
}
.comment .commentMeta {
font-style: italic;
}
</style>
using EPiServer.ServiceLocation;
using EPiServer.Social.Comments.Core;
using EPiServer.Social.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Demo1.Controllers
{
public class SocialController : Controller
{
private const string ANONYMOUS_AUTHOR_NAME = "Anonymous Coward";
readonly ICommentService commentService;
public SocialController(ICommentService commentService)
{
this.commentService = commentService;
}
[Route("social/comments/add")]
[HttpPost]
public ActionResult AddComments(CommentSubmission commentSubmission)
{
if(ModelState.IsValid)
{
var comment = new Comment(
parent: Reference.Create(GetContentReference(commentSubmission.PageId)),
author: Reference.Create(commentSubmission.Author ?? ANONYMOUS_AUTHOR_NAME), // This should technically be a reference to the author, but a simple string works too
body: commentSubmission.Body,
isVisible: true
);
commentService.Add(comment);
}
return GetComments(commentSubmission.PageId);
}
[Route("social/comments/get")]
[HttpPost]
public ActionResult GetComments(Guid id)
{
var criteria = new Criteria<CommentFilter>
{
Filter = new CommentFilter
{
Parent = Reference.Create(GetContentReference(id))
},
OrderBy = new List<SortInfo>
{
new SortInfo(CommentSortFields.Created, false)
}
};
var comments = commentService.Get(criteria);
return PartialView("/Views/Shared/PagePartials/CommentList.cshtml", comments);
}
private string GetContentReference(Guid id)
{
return string.Concat("content://", id.ToString());
}
}
public class CommentSubmission
{
[Required]
public string Body { get; set; }
public Guid PageId { get; set; }
public string Author { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment