Skip to content

Commit

Permalink
UX: Tweak 'Solution' button design (#232)
Browse files Browse the repository at this point in the history
Hide the accept solution button if a solution has been accepted already.
  • Loading branch information
nbianca committed Mar 17, 2023
1 parent ee55e6d commit 4ae1841
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Expand Up @@ -19,6 +19,7 @@ function clearAccepted(topic) {
accepted_answer: false,
can_accept_answer: true,
can_unaccept_answer: false,
topic_accepted_answer: false,
});
}
});
Expand Down Expand Up @@ -78,24 +79,25 @@ function initializeWithApi(api) {
api.includePostAttributes(
"can_accept_answer",
"can_unaccept_answer",
"accepted_answer"
"accepted_answer",
"topic_accepted_answer"
);

if (api.addDiscoveryQueryParam) {
api.addDiscoveryQueryParam("solved", { replace: true, refreshModel: true });
}

api.addPostMenuButton("solved", (attrs) => {
const isOp = currentUser?.id === attrs.topicCreatedById;

if (attrs.can_accept_answer) {
const isOp = currentUser?.id === attrs.topicCreatedById;

return {
action: "acceptAnswer",
icon: "far-check-square",
className: "unaccepted",
title: "solved.accept_answer",
label: isOp ? "solved.solution" : null,
position: isOp ? "first" : "second",
position: attrs.topic_accepted_answer ? "second-last-hidden" : "first",
};
} else if (attrs.accepted_answer) {
if (attrs.can_unaccept_answer) {
Expand All @@ -104,8 +106,8 @@ function initializeWithApi(api) {
icon: "check-square",
title: "solved.unaccept_answer",
className: "accepted fade-out",
position: isOp ? "first" : "second",
label: isOp ? "solved.solution" : null,
position: "first",
label: "solved.solution",
};
} else {
return {
Expand Down Expand Up @@ -168,23 +170,22 @@ function initializeWithApi(api) {

api.attachWidgetAction("post", "acceptAnswer", function () {
const post = this.model;
const current = post.get("topic.postStream.posts").filter((p) => {
return p.post_number === 1 || p.accepted_answer;
});
acceptPost(post);

current.forEach((p) =>
this.appEvents.trigger("post-stream:refresh", { id: p.id })
);
post.get("topic.postStream.posts").forEach((p) => {
p.set("topic_accepted_answer", true);
this.appEvents.trigger("post-stream:refresh", { id: p.id });
});
});

api.attachWidgetAction("post", "unacceptAnswer", function () {
const post = this.model;
const op = post
.get("topic.postStream.posts")
.find((p) => p.post_number === 1);
unacceptPost(post);
this.appEvents.trigger("post-stream:refresh", { id: op.id });

post.get("topic.postStream.posts").forEach((p) => {
p.set("topic_accepted_answer", false);
this.appEvents.trigger("post-stream:refresh", { id: p.id });
});
});

if (api.registerConnectorClass) {
Expand Down
8 changes: 7 additions & 1 deletion plugin.rb
Expand Up @@ -504,7 +504,7 @@ def can_accept_answer?(topic, post)

require_dependency "post_serializer"
class ::PostSerializer
attributes :can_accept_answer, :can_unaccept_answer, :accepted_answer
attributes :can_accept_answer, :can_unaccept_answer, :accepted_answer, :topic_accepted_answer

def can_accept_answer
if topic = (topic_view && topic_view.topic) || object.topic
Expand All @@ -524,6 +524,12 @@ def can_unaccept_answer
def accepted_answer
post_custom_fields["is_accepted_answer"] == "true"
end

def topic_accepted_answer
if topic = (topic_view && topic_view.topic) || object.topic
topic.custom_fields["accepted_answer_post_id"].present?
end
end
end

require_dependency "search"
Expand Down

0 comments on commit 4ae1841

Please sign in to comment.