diff --git a/app/controllers/ajax/mute_rule_controller.rb b/app/controllers/ajax/mute_rule_controller.rb deleted file mode 100644 index 96ec1e94..00000000 --- a/app/controllers/ajax/mute_rule_controller.rb +++ /dev/null @@ -1,67 +0,0 @@ -class Ajax::MuteRuleController < AjaxController - def create - params.require :muted_phrase - - unless user_signed_in? - @response[:status] = :noauth - @response[:message] = t(".noauth") - return - end - - rule = MuteRule.create!(user: current_user, muted_phrase: params[:muted_phrase]) - @response[:status] = :okay - @response[:success] = true - @response[:message] = t(".success") - @response[:id] = rule.id.to_s # Returning this as a string as JavaScript defines all numbers as floats - end - - def update - params.require :id - params.require :muted_phrase - - unless user_signed_in? - @response[:status] = :noauth - @response[:message] = t(".noauth") - return - end - - rule = MuteRule.find(params[:id]) - - if rule.user_id != current_user.id - @response[:status] = :nopriv - @response[:message] = t(".nopriv") - return - end - - rule.muted_phrase = params[:muted_phrase] - rule.save! - - @response[:status] = :okay - @response[:message] = t(".success") - @response[:success] = true - end - - def destroy - params.require :id - - unless user_signed_in? - @response[:status] = :noauth - @response[:message] = t(".noauth") - return - end - - rule = MuteRule.find(params[:id]) - - if rule.user_id != current_user.id - @response[:status] = :nopriv - @response[:message] = t(".nopriv") - return - end - - rule.destroy! - - @response[:status] = :okay - @response[:message] = t(".success") - @response[:success] = true - end -end diff --git a/app/javascript/retrospring/features/settings/index.ts b/app/javascript/retrospring/features/settings/index.ts index 39024af8..0b525372 100644 --- a/app/javascript/retrospring/features/settings/index.ts +++ b/app/javascript/retrospring/features/settings/index.ts @@ -1,12 +1,10 @@ import registerEvents from "utilities/registerEvents"; -import { muteDocumentHandler } from "./mute"; import { profileHeaderChangeHandler, profilePictureChangeHandler } from "./crop"; import { themeDocumentHandler, themeSubmitHandler } from "./theme"; import { userSubmitHandler } from "./password"; import { unblockAnonymousHandler } from "./block"; export default (): void => { - muteDocumentHandler(); themeDocumentHandler(); registerEvents([ @@ -16,4 +14,4 @@ export default (): void => { { type: 'change', target: document.querySelector('#user_profile_header[type=file]'), handler: profileHeaderChangeHandler }, { type: 'click', target: document.querySelectorAll('[data-action="anon-unblock"]'), handler: unblockAnonymousHandler } ]); -} \ No newline at end of file +} diff --git a/app/javascript/retrospring/features/settings/mute.ts b/app/javascript/retrospring/features/settings/mute.ts deleted file mode 100644 index 91fbfe5e..00000000 --- a/app/javascript/retrospring/features/settings/mute.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { destroy, post } from '@rails/request.js'; - -function createSubmitEvent( - submit: HTMLButtonElement, - rulesList: HTMLDivElement, - textEntry: HTMLButtonElement, - template: HTMLTemplateElement -): (event: Event) => void { - return (event) => { - event.preventDefault(); - submit.disabled = true; - - post('/ajax/mute', { - body: { - muted_phrase: textEntry.value - }, - contentType: 'application/json' - }) - .then(async response => { - const data = await response.json; - - submit.disabled = false; - if (!data.success) return; - - const newEntryFragment = template.content.cloneNode(true) as Element; - newEntryFragment.querySelector('input').value = textEntry.value; - - const newDeleteButton = newEntryFragment.querySelector('button'); - newDeleteButton.dataset.id = String(data.id); - newDeleteButton.onclick = createDeleteEvent( - newEntryFragment.querySelector('.form-group'), - newDeleteButton - ); - - rulesList.appendChild(newEntryFragment); - textEntry.value = ''; - }); - }; -} - -function createDeleteEvent( - entry: HTMLDivElement, - deleteButton: HTMLButtonElement -): () => void { - return () => { - deleteButton.disabled = true; - - destroy(`/ajax/mute/${deleteButton.dataset.id}`) - .then(async response => { - const data = await response.json; - - if (data.success) { - entry.parentElement.removeChild(entry) - } else { - deleteButton.disabled = false; - } - }); - } -} - -export function muteDocumentHandler(): void { - const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement; - if (!submit || submit.classList.contains('js-initialized')) return; - - const rulesList = document.querySelector('.js-rules-list'); - rulesList.querySelectorAll('.form-group:not(.js-initalized)').forEach(entry => { - const button = entry.querySelector('button') - button.onclick = createDeleteEvent(entry, button) - }); - const textEntry: HTMLButtonElement = document.getElementById('new-rule-text') as HTMLButtonElement; - const template: HTMLTemplateElement = document.getElementById('rule-template') as HTMLTemplateElement; - - submit.form.onsubmit = createSubmitEvent(submit, rulesList, textEntry, template) - - submit.classList.add('js-initialized'); -} \ No newline at end of file diff --git a/spec/controllers/ajax/mute_rule_controller_spec.rb b/spec/controllers/ajax/mute_rule_controller_spec.rb deleted file mode 100644 index f5b504b5..00000000 --- a/spec/controllers/ajax/mute_rule_controller_spec.rb +++ /dev/null @@ -1,88 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -describe Ajax::MuteRuleController, :ajax_controller, type: :controller do - describe "#create" do - subject { post(:create, params:) } - - context "when user is signed in" do - before(:each) { sign_in(user) } - - let(:params) { { muted_phrase: "test" } } - let(:expected_response) do - { - "success" => true, - "status" => "okay", - "id" => MuteRule.last.id.to_s, - "message" => "Rule added successfully." - } - end - - it "creates a mute rule" do - expect { subject }.to change { MuteRule.count }.by(1) - expect(response).to have_http_status(:success) - - rule = MuteRule.first - expect(rule.user_id).to eq(user.id) - expect(rule.muted_phrase).to eq("test") - end - - include_examples "returns the expected response" - end - end - - describe "#update" do - subject { post(:update, params:) } - - context "when user is signed in" do - before(:each) { sign_in(user) } - - let(:rule) { MuteRule.create(user:, muted_phrase: "test") } - let(:params) { { id: rule.id, muted_phrase: "dogs" } } - let(:expected_response) do - { - "success" => true, - "status" => "okay", - "message" => "Rule updated successfully." - } - end - - it "updates a mute rule" do - subject - expect(response).to have_http_status(:success) - - expect(rule.reload.muted_phrase).to eq("dogs") - end - - include_examples "returns the expected response" - end - end - - describe "#destroy" do - subject { delete(:destroy, params:) } - - context "when user is signed in" do - before(:each) { sign_in(user) } - - let(:rule) { MuteRule.create(user:, muted_phrase: "test") } - let(:params) { { id: rule.id } } - let(:expected_response) do - { - "success" => true, - "status" => "okay", - "message" => "Rule deleted successfully." - } - end - - it "deletes a mute rule" do - subject - expect(response).to have_http_status(:success) - - expect(MuteRule.exists?(rule.id)).to eq(false) - end - - include_examples "returns the expected response" - end - end -end