Use composer to toggle `is_category_expert_question` on existing first posts (#13)

Use composer to toggle is_category_expert_question on existing first posts (#13)

diff --git a/app/controllers/category_experts_controller.rb b/app/controllers/category_experts_controller.rb
index 51c30cb..83c0926 100644
--- a/app/controllers/category_experts_controller.rb
+++ b/app/controllers/category_experts_controller.rb
@@ -2,7 +2,6 @@
 
 class CategoryExpertsController < ApplicationController
   before_action :find_post, :ensure_staff_and_enabled, only: [:approve_post, :unapprove_post]
-  before_action :find_topic, only: [:mark_topic_as_question, :unmark_topic_as_question]
 
   def endorse
     raise Discourse::NotFound unless current_user
@@ -40,25 +39,6 @@ class CategoryExpertsController < ApplicationController
     render json: topic_custom_fields
   end
 
-  def mark_topic_as_question
-    guardian.ensure_can_edit!(@topic)
-    raise Discourse::InvalidParameters unless @topic.category.accepting_category_expert_questions?
-
-    @topic.custom_fields[CategoryExperts::TOPIC_IS_CATEGORY_EXPERT_QUESTION] = true
-    @topic.save!
-
-    render json: success_json
-  end
-
-  def unmark_topic_as_question
-    guardian.ensure_can_edit!(@topic)
-
-    @topic.custom_fields[CategoryExperts::TOPIC_IS_CATEGORY_EXPERT_QUESTION] = false
-    @topic.save!
-
-    render json: success_json
-  end
-
   private
 
   def topic_custom_fields
@@ -80,11 +60,4 @@ class CategoryExpertsController < ApplicationController
 
     raise Discourse::NotFound unless @post
   end
-
-  def find_topic
-    topic_id = params.require(:topic_id)
-    @topic = Topic.find_by(id: topic_id)
-
-    raise Discourse::NotFound unless @topic
-  end
 end
diff --git a/assets/javascripts/discourse/components/is-question-checkbox.js b/assets/javascripts/discourse/components/is-question-checkbox.js
index 87d5ddb..e9afbea 100644
--- a/assets/javascripts/discourse/components/is-question-checkbox.js
+++ b/assets/javascripts/discourse/components/is-question-checkbox.js
@@ -1,3 +1,19 @@
 import Component from "@ember/component";
+import discourseComputed from "discourse-common/utils/decorators";
 
-export default Component.extend({});
+export default Component.extend({
+  init() {
+    this._super(...arguments);
+
+    if (this.model.topic && this.model.topic.is_category_expert_question) {
+      this.set("model.is_category_expert_question", true);
+    }
+  },
+
+  @discourseComputed("model", "model.category")
+  show(model, category) {
+    if (!category || !category.allowingCategoryExpertQuestions) return false;
+
+    return model.editingFirstPost || model.creatingTopic;
+  },
+});
diff --git a/assets/javascripts/discourse/components/topic-view-is-question-indicator.js b/assets/javascripts/discourse/components/topic-view-is-question-indicator.js
deleted file mode 100644
index b0088f1..0000000
--- a/assets/javascripts/discourse/components/topic-view-is-question-indicator.js
+++ /dev/null
@@ -1,50 +0,0 @@
-import Component from "@ember/component";
-import bootbox from "bootbox";
-import { action } from "@ember/object";
-import { ajax } from "discourse/lib/ajax";
-import { popupAjaxError } from "discourse/lib/ajax-error";
-import { readOnly } from "@ember/object/computed";
-
-export default Component.extend({
-  canEdit: readOnly("topic.details.can_edit"),
-  questionsEnabled: false,
-
-  init() {
-    this._super(...arguments);
-    this.set(
-      "questionsEnabled",
-      this.siteSettings.enable_category_experts &&
-        this.topic.category &&
-        this.topic.category.allowingCategoryExpertQuestions
-    );
-  },
-
-  @action
-  unmarkAsQuestion() {
-    bootbox.confirm(
-      I18n.t("category_experts.confirm_unmark_as_question"),
-      (result) => {
-        if (!result) return;
-
-        ajax(`/category-experts/unmark-topic-as-question/${this.topic.id}`, {
-          type: "DELETE",
-        })
-          .then(() => {
-            this.topic.set("is_category_expert_question", false);
-          })
-          .catch(popupAjaxError);
-      }
-    );
-  },
-
-  @action
-  markAsQuestion() {
-    ajax(`/category-experts/mark-topic-as-question/${this.topic.id}`, {
-      type: "POST",
-    })
-      .then(() => {
-        this.topic.set("is_category_expert_question", true);
-      })
-      .catch(popupAjaxError);
-  },
-});
diff --git a/assets/javascripts/discourse/connectors/topic-category/is-question-indicator.hbs b/assets/javascripts/discourse/connectors/topic-category/is-question-indicator.hbs
index e1f7aaf..d0894b6 100644
--- a/assets/javascripts/discourse/connectors/topic-category/is-question-indicator.hbs
+++ b/assets/javascripts/discourse/connectors/topic-category/is-question-indicator.hbs
@@ -1 +1,3 @@
-{{topic-view-is-question-indicator topic=topic}}
+{{#if topic.is_category_expert_question}}
+  {{category-expert-question-indicator topic currentUser}}
+{{/if}}
diff --git a/assets/javascripts/discourse/pre-initializers/extend-for-category-experts.js b/assets/javascripts/discourse/pre-initializers/extend-for-category-experts.js
index 159f2af..d429104 100644
--- a/assets/javascripts/discourse/pre-initializers/extend-for-category-experts.js
+++ b/assets/javascripts/discourse/pre-initializers/extend-for-category-experts.js
@@ -14,6 +14,11 @@ export default {
       "is_category_expert_question"
     );
 
+    Composer.serializeOnUpdate(
+      "is_category_expert_question",
+      "is_category_expert_question"
+    );
+
     Category.reopen({
       allowingCategoryExpertEndorsements: and(
         "custom_fields.category_expert_group_ids",
diff --git a/assets/javascripts/discourse/templates/components/is-question-checkbox.hbs b/assets/javascripts/discourse/templates/components/is-question-checkbox.hbs
index f30fab0..c2b607e 100644
--- a/assets/javascripts/discourse/templates/components/is-question-checkbox.hbs
+++ b/assets/javascripts/discourse/templates/components/is-question-checkbox.hbs
@@ -1,11 +1,9 @@
-{{#if model.creatingTopic}}
-  {{#if model.category.allowingCategoryExpertQuestions}}
-    <label class="checkbox-label is-category-expert-question">
-      {{input
-        type="checkbox"
-        checked=model.is_category_expert_question
-      }}
-      {{i18n "category_experts.ask_category_expert"}}
-    </label>
-  {{/if}}
+{{#if show}}
+  <label class="checkbox-label is-category-expert-question">
+    {{input
+      type="checkbox"
+      checked=model.is_category_expert_question
+    }}
+    {{i18n "category_experts.ask_category_expert"}}
+  </label>
 {{/if}}
diff --git a/assets/javascripts/discourse/templates/components/topic-view-is-question-indicator.hbs b/assets/javascripts/discourse/templates/components/topic-view-is-question-indicator.hbs
deleted file mode 100644
index fcbd5ad..0000000
--- a/assets/javascripts/discourse/templates/components/topic-view-is-question-indicator.hbs
+++ /dev/null
@@ -1,19 +0,0 @@
-{{#if questionsEnabled}}
-  {{#unless topic.expert_post_group_names}}
-    {{#if topic.is_category_expert_question}}
-      <span onclick={{action "unmarkAsQuestion"}} class='topic-view-category-expert-question'>
-        {{i18n "category_experts.topic_list.question"}}
-        {{#if canEdit}}
-          <span class="remove-category-expert-question"> {{d-icon "times"}}
-          </span>
-        {{/if}}
-      </span>
-    {{else}}
-      {{#if canEdit}}
-        <a href {{action "markAsQuestion"}} class="topic-view-category-expert-mark-as-question">
-          {{i18n "category_experts.mark_as_question"}}
-        </a>
-      {{/if}}
-    {{/if}}
-  {{/unless}}
-{{/if}}
diff --git a/assets/stylesheets/common.scss b/assets/stylesheets/common.scss
index 9fde684..4001653 100644
--- a/assets/stylesheets/common.scss
+++ b/assets/stylesheets/common.scss
@@ -55,8 +55,7 @@ article.category-expert-post {
 
 .topic-list-category-expert-tags > a,
 .topic-list-category-expert-needs-approval,
-.topic-list-category-expert-question,
-.topic-view-category-expert-question {
+.topic-list-category-expert-question {
   font-size: $font-down-2;
   padding: 0.3em 0.5em;
   background: $success-low;
@@ -65,37 +64,9 @@ article.category-expert-post {

[... diff too long, it was truncated ...]

GitHub sha: 2d31b2ea

This commit appears in #13 which was approved by davidtaylorhq. It was merged by markvanlan.