UX: Tweak 'Solution' button design (#232)

Hide the accept solution button if a solution has been accepted already.
This commit is contained in:
Bianca Nenciu 2023-03-17 17:35:58 +02:00 committed by GitHub
parent ee55e6d160
commit 4ae1841479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View File

@ -19,6 +19,7 @@ function clearAccepted(topic) {
accepted_answer: false, accepted_answer: false,
can_accept_answer: true, can_accept_answer: true,
can_unaccept_answer: false, can_unaccept_answer: false,
topic_accepted_answer: false,
}); });
} }
}); });
@ -78,7 +79,8 @@ function initializeWithApi(api) {
api.includePostAttributes( api.includePostAttributes(
"can_accept_answer", "can_accept_answer",
"can_unaccept_answer", "can_unaccept_answer",
"accepted_answer" "accepted_answer",
"topic_accepted_answer"
); );
if (api.addDiscoveryQueryParam) { if (api.addDiscoveryQueryParam) {
@ -86,16 +88,16 @@ function initializeWithApi(api) {
} }
api.addPostMenuButton("solved", (attrs) => { api.addPostMenuButton("solved", (attrs) => {
if (attrs.can_accept_answer) {
const isOp = currentUser?.id === attrs.topicCreatedById; const isOp = currentUser?.id === attrs.topicCreatedById;
if (attrs.can_accept_answer) {
return { return {
action: "acceptAnswer", action: "acceptAnswer",
icon: "far-check-square", icon: "far-check-square",
className: "unaccepted", className: "unaccepted",
title: "solved.accept_answer", title: "solved.accept_answer",
label: isOp ? "solved.solution" : null, label: isOp ? "solved.solution" : null,
position: isOp ? "first" : "second", position: attrs.topic_accepted_answer ? "second-last-hidden" : "first",
}; };
} else if (attrs.accepted_answer) { } else if (attrs.accepted_answer) {
if (attrs.can_unaccept_answer) { if (attrs.can_unaccept_answer) {
@ -104,8 +106,8 @@ function initializeWithApi(api) {
icon: "check-square", icon: "check-square",
title: "solved.unaccept_answer", title: "solved.unaccept_answer",
className: "accepted fade-out", className: "accepted fade-out",
position: isOp ? "first" : "second", position: "first",
label: isOp ? "solved.solution" : null, label: "solved.solution",
}; };
} else { } else {
return { return {
@ -168,23 +170,22 @@ function initializeWithApi(api) {
api.attachWidgetAction("post", "acceptAnswer", function () { api.attachWidgetAction("post", "acceptAnswer", function () {
const post = this.model; const post = this.model;
const current = post.get("topic.postStream.posts").filter((p) => {
return p.post_number === 1 || p.accepted_answer;
});
acceptPost(post); acceptPost(post);
current.forEach((p) => post.get("topic.postStream.posts").forEach((p) => {
this.appEvents.trigger("post-stream:refresh", { id: p.id }) p.set("topic_accepted_answer", true);
); this.appEvents.trigger("post-stream:refresh", { id: p.id });
});
}); });
api.attachWidgetAction("post", "unacceptAnswer", function () { api.attachWidgetAction("post", "unacceptAnswer", function () {
const post = this.model; const post = this.model;
const op = post
.get("topic.postStream.posts")
.find((p) => p.post_number === 1);
unacceptPost(post); 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) { if (api.registerConnectorClass) {

View File

@ -504,7 +504,7 @@ SQL
require_dependency "post_serializer" require_dependency "post_serializer"
class ::PostSerializer 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 def can_accept_answer
if topic = (topic_view && topic_view.topic) || object.topic if topic = (topic_view && topic_view.topic) || object.topic
@ -524,6 +524,12 @@ SQL
def accepted_answer def accepted_answer
post_custom_fields["is_accepted_answer"] == "true" post_custom_fields["is_accepted_answer"] == "true"
end 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 end
require_dependency "search" require_dependency "search"