Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Disable needs-love button on save #2

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions app/controllers/discourse_needs_love/needs_love_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ class NeedsLoveController < ApplicationController
before_action :ensure_logged_in

def needs_love
tag_name = "needs-love" # Load from plugin site setting
tag_name = SiteSetting.needs_love_tag

topic = Topic.find_by(id: params[:topic_id])
tag = Tag.find_by(name: tag_name)
unless tag
tag = Tag.create(name: name)
tag = Tag.create(name: tag_name)
end

old_tag_names = topic.tags.pluck(:name) || []
tag_names = ([tag_name] + old_tag_names).uniq
tags = Tag.where(name: tag_names)

topic.tags = tags

topic.tags_changed = true
DiscourseEvent.trigger(
:topic_tags_changed,
topic, old_tag_names: old_tag_names, new_tag_names: [tag_name]
)
PostRevisor.new(topic.first_post, topic).revise!(current_user, { tags: tag_names }, validate_post: false)

render json: success_json
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";

function TagTopic(user, target) {
return ajax(`/needs_love/needs_love/${target.id}`, {
type: "PUT",
data: {},
});
function tagTopic(user, target) {
try {
let result = ajax(`/needs_love/needs_love/${target.id}`, {
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
type: "PUT",
data: {},
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
});
return result;
} catch (error) {
popupAjaxError(error);
}
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
}

function disableNeedsLoveButton(topic, tagName) {
const tags = topic.tags || [];
return tags.includes(tagName);
}

function registerTopicFooterButtons(api) {
function registerTopicFooterButtons(api, tagName) {
api.registerTopicFooterButton({
id: "needs-love",
icon() {
Expand All @@ -26,15 +37,47 @@ function registerTopicFooterButtons(api) {
},
action() {
// Add Tag
TagTopic(this.currentUser, this.topic);
tagTopic(this.currentUser, this.topic).then(() => {
this.appEvents.trigger("post-stream:refresh", {
id: this.topic.postStream.firstPostId,
});
});
},
dropdown() {
return this.site.mobileView;
},
classNames: ["needs-love"],
dependentKeys: [],
dependentKeys: ["topic.tags"],
displayed() {
return !disableNeedsLoveButton(this.topic, tagName);
},
});

api.registerTopicFooterButton({
id: "needs-love-disabled",
icon() {
return "band-aid";
},
priority: 250,
translatedTitle() {
return "Needs Love";
},
translatedAriaLabel() {
return "Needs Love";
},
translatedLabel() {
return "Needs Love";
},
action() {
// No action. Button is disabled.
},
dropdown() {
return this.site.mobileView;
},
classNames: ["needs-love", "disabled"],
dependentKeys: ["topic.tags"],
displayed() {
return true;
return disableNeedsLoveButton(this.topic, tagName);
},
});
}
Expand All @@ -47,7 +90,8 @@ export default {
if (!siteSettings.needs_love_enabled) {
return;
}
const tagName = siteSettings.needs_love_tag;

withPluginApi("0.8.28", (api) => registerTopicFooterButtons(api));
withPluginApi("0.8.28", (api) => registerTopicFooterButtons(api, tagName));
},
};
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
en:
site_settings:
needs_love_enabled: "Enable the Needs Love plugin"
needs_love_tag: "Tag name for the Needs Love plugin"
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ plugins:
needs_love_enabled:
default: false
client: true
needs_love_tag:
default: "needs-love"
client: true
1 change: 1 addition & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
end

after_initialize do
require_relative "app/controllers/discourse_needs_love/needs_love_controller.rb"
end
30 changes: 30 additions & 0 deletions spec/requests/discourse_needs_love/needs_love_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rails_helper'

module DiscourseNeedsLove
describe NeedsLoveController do
let(:topic) { Fabricate(:topic) }
let(:post) { Fabricate(:post, topic: topic) }
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
before_all do
end
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved

context "signed in as an admin" do
fab!(:signed_in_user) { Fabricate(:admin) }
fab!(:another_admin) { Fabricate(:admin) }

before do
SiteSetting.needs_love_enabled = true
SiteSetting.tagging_enabled = true

sign_in signed_in_user
end

it 'adds the needs-love tag' do
put "/needs_love/needs_love/#{post.topic.id}.json"
expect(response.status).to eq(200)
expect(topic.tags.pluck(:name).include?("needs-love")).to eq(true)
end
end
end
end