Skip to content

Commit 4ae1841

Browse files
authored
UX: Tweak 'Solution' button design (#232)
Hide the accept solution button if a solution has been accepted already.
1 parent ee55e6d commit 4ae1841

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

assets/javascripts/discourse/initializers/extend-for-solved-button.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function clearAccepted(topic) {
1919
accepted_answer: false,
2020
can_accept_answer: true,
2121
can_unaccept_answer: false,
22+
topic_accepted_answer: false,
2223
});
2324
}
2425
});
@@ -78,24 +79,25 @@ function initializeWithApi(api) {
7879
api.includePostAttributes(
7980
"can_accept_answer",
8081
"can_unaccept_answer",
81-
"accepted_answer"
82+
"accepted_answer",
83+
"topic_accepted_answer"
8284
);
8385

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

8890
api.addPostMenuButton("solved", (attrs) => {
89-
const isOp = currentUser?.id === attrs.topicCreatedById;
90-
9191
if (attrs.can_accept_answer) {
92+
const isOp = currentUser?.id === attrs.topicCreatedById;
93+
9294
return {
9395
action: "acceptAnswer",
9496
icon: "far-check-square",
9597
className: "unaccepted",
9698
title: "solved.accept_answer",
9799
label: isOp ? "solved.solution" : null,
98-
position: isOp ? "first" : "second",
100+
position: attrs.topic_accepted_answer ? "second-last-hidden" : "first",
99101
};
100102
} else if (attrs.accepted_answer) {
101103
if (attrs.can_unaccept_answer) {
@@ -104,8 +106,8 @@ function initializeWithApi(api) {
104106
icon: "check-square",
105107
title: "solved.unaccept_answer",
106108
className: "accepted fade-out",
107-
position: isOp ? "first" : "second",
108-
label: isOp ? "solved.solution" : null,
109+
position: "first",
110+
label: "solved.solution",
109111
};
110112
} else {
111113
return {
@@ -168,23 +170,22 @@ function initializeWithApi(api) {
168170

169171
api.attachWidgetAction("post", "acceptAnswer", function () {
170172
const post = this.model;
171-
const current = post.get("topic.postStream.posts").filter((p) => {
172-
return p.post_number === 1 || p.accepted_answer;
173-
});
174173
acceptPost(post);
175174

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

181181
api.attachWidgetAction("post", "unacceptAnswer", function () {
182182
const post = this.model;
183-
const op = post
184-
.get("topic.postStream.posts")
185-
.find((p) => p.post_number === 1);
186183
unacceptPost(post);
187-
this.appEvents.trigger("post-stream:refresh", { id: op.id });
184+
185+
post.get("topic.postStream.posts").forEach((p) => {
186+
p.set("topic_accepted_answer", false);
187+
this.appEvents.trigger("post-stream:refresh", { id: p.id });
188+
});
188189
});
189190

190191
if (api.registerConnectorClass) {

plugin.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def can_accept_answer?(topic, post)
504504

505505
require_dependency "post_serializer"
506506
class ::PostSerializer
507-
attributes :can_accept_answer, :can_unaccept_answer, :accepted_answer
507+
attributes :can_accept_answer, :can_unaccept_answer, :accepted_answer, :topic_accepted_answer
508508

509509
def can_accept_answer
510510
if topic = (topic_view && topic_view.topic) || object.topic
@@ -524,6 +524,12 @@ def can_unaccept_answer
524524
def accepted_answer
525525
post_custom_fields["is_accepted_answer"] == "true"
526526
end
527+
528+
def topic_accepted_answer
529+
if topic = (topic_view && topic_view.topic) || object.topic
530+
topic.custom_fields["accepted_answer_post_id"].present?
531+
end
532+
end
527533
end
528534

529535
require_dependency "search"

0 commit comments

Comments
 (0)