Skip to content

Commit

Permalink
Merge pull request #17267 from wordpress-mobile/feature/17200-approve…
Browse files Browse the repository at this point in the history
…_action

Comment Moderation Bar: update comment when Approved button tapped.
  • Loading branch information
ScoutHarris authored Oct 6, 2021
2 parents 6bbedf9 + 0e4ef85 commit db6a2ae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion WordPress/Classes/Services/CommentService.m
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,8 @@ - (void)moderateComment:(Comment *)comment
if (success) {
success();
}
} failure:^(NSError *error) {
} failure:^(NSError *error) {
DDLogError(@"Error moderating comment: %@", error);
[self.managedObjectContext performBlock:^{
// Note: The comment might have been deleted at this point
Comment *commentInContext = (Comment *)[self.managedObjectContext existingObjectWithID:commentID error:nil];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
isModerationEnabled = comment.canModerate

if isModerationEnabled {
moderationBar.comment = comment
moderationBar.commentStatus = CommentStatusType.typeForStatus(comment.status)
}

// Configure comment content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CommentDetailViewController: UITableViewController {

private var rows = [RowType]()

private var moderationBar: CommentModerationBar?

// MARK: Views

private var headerCell = CommentHeaderTableViewCell()
Expand Down Expand Up @@ -131,6 +133,7 @@ class CommentDetailViewController: UITableViewController {

configureContentCell(cell, comment: comment)
cell.moderationBar.delegate = self
moderationBar = cell.moderationBar
return cell

case .replyIndicator:
Expand Down Expand Up @@ -394,11 +397,33 @@ private extension String {
extension CommentDetailViewController: CommentModerationBarDelegate {
func statusChangedTo(_ commentStatus: CommentStatusType) {

// TODO: update Comment

switch commentStatus {
case .approved:
approveComment()
default:
break
}
}
}

// MARK: - Comment Moderation Actions

private extension CommentDetailViewController {

func approveComment() {
CommentAnalytics.trackCommentApproved(comment: comment)

commentService.approve(comment, success: { [weak self] in
self?.displayNotice(title: ModerationMessages.approveSuccess)
}, failure: { [weak self] error in
self?.displayNotice(title: ModerationMessages.approveFail)
self?.moderationBar?.commentStatus = CommentStatusType.typeForStatus(self?.comment.status)
})
}

struct ModerationMessages {
static let approveSuccess = NSLocalizedString("Comment approved.", comment: "Message displayed when approving a comment succeeds.")
static let approveFail = NSLocalizedString("Error approving comment.", comment: "Message displayed when approving a comment fails.")
}

}
19 changes: 7 additions & 12 deletions WordPress/Classes/ViewRelated/Comments/CommentModerationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ class CommentModerationBar: UIView {

weak var delegate: CommentModerationBarDelegate?

var comment: Comment? {
var commentStatus: CommentStatusType? {
didSet {
currentStatus = CommentStatusType.typeForStatus(comment?.status)
}
}

private var currentStatus: CommentStatusType? {
didSet {
if oldValue != currentStatus {
if oldValue != commentStatus {
toggleButtonForStatus(oldValue)
}
toggleButtonForStatus(currentStatus)
toggleButtonForStatus(commentStatus)
}
}

Expand Down Expand Up @@ -209,9 +203,10 @@ private extension CommentModerationBar {
}

func updateStatusTo(_ status: CommentStatusType) {
// Don't change the comment.status. It is needed to fallback to if the update fails.
currentStatus = status
delegate?.statusChangedTo(status)
ReachabilityUtils.onAvailableInternetConnectionDo {
commentStatus = status
delegate?.statusChangedTo(status)
}
}

}
Expand Down

0 comments on commit db6a2ae

Please sign in to comment.