From af137cc445907f8e6d68ff15754e841775f3a451 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 31 Aug 2022 21:45:39 +0200 Subject: [PATCH 1/6] Add `@rails/request.js` --- package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/package.json b/package.json index 202c6b1b..51d7b2f5 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dependencies": { "@babel/preset-typescript": "^7.18.6", "@melloware/coloris": "^0.16.0", + "@rails/request.js": "^0.0.6", "@rails/ujs": "^6.1.6", "bootstrap": "^4.6.2", "cheet.js": "^0.3.3", diff --git a/yarn.lock b/yarn.lock index a307439a..59ecac97 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1193,6 +1193,11 @@ resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-7.0.3.tgz#71f08e958883af64f6a20489318b5e95d2c6dc5b" integrity sha512-Iefl21FZD+ck1di6xSHMYzSzRiNJTHV4NrAzCfDfqc/wPz4xncrP8f2/fJ+2jzwKIaDn76UVMsALh7R5OzsF8Q== +"@rails/request.js@^0.0.6": + version "0.0.6" + resolved "https://registry.yarnpkg.com/@rails/request.js/-/request.js-0.0.6.tgz#5f0347a9f363e50ec45118c7134080490cda81d8" + integrity sha512-dfFWaQXitYJ4kxrgGJNhDNXX54/v10YgoJqBMVe6lhqs6a4N9WD7goZJEvwin82TtK8MqUNhwfyisgKwM6dMdg== + "@rails/ujs@^6.1.4-1", "@rails/ujs@^6.1.6": version "6.1.6" resolved "https://registry.yarnpkg.com/@rails/ujs/-/ujs-6.1.6.tgz#de486ae0a663e1bed637a012cbb2739bfcfa2031" From d521800a596f451c5bbdf8938d8d8e4743f9fde5 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 31 Aug 2022 21:46:10 +0200 Subject: [PATCH 2/6] Refactor removal of anon blocks to use request.js --- .../retrospring/features/settings/block.ts | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/app/javascript/retrospring/features/settings/block.ts b/app/javascript/retrospring/features/settings/block.ts index 93c3793b..a84136d7 100644 --- a/app/javascript/retrospring/features/settings/block.ts +++ b/app/javascript/retrospring/features/settings/block.ts @@ -1,27 +1,21 @@ -import Rails from '@rails/ujs'; +import { destroy } from '@rails/request.js'; import { showNotification, showErrorNotification } from 'utilities/notifications'; import I18n from 'retrospring/i18n'; export function unblockAnonymousHandler(event: Event): void { const button: HTMLButtonElement = event.currentTarget as HTMLButtonElement; const targetId = button.dataset.target; - let success = false; - Rails.ajax({ - url: `/ajax/block_anon/${targetId}`, - type: 'DELETE', - success: (data) => { - success = data.success; + destroy(`/ajax/block_anon/${targetId}`) + .then(async response => { + if (!response.ok) return; + + const data = await response.json; showNotification(data.message, data.success); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); - showErrorNotification(I18n.translate('frontend.error.message')); - }, - complete: () => { - if (!success) return; - button.closest('.list-group-item').remove(); - } - }); + }) + .catch(err => { + console.log(err); + showErrorNotification(I18n.translate('frontend.error.message')); + }); } \ No newline at end of file From 812c9617ff2d42ea882a880fd26d73e034f08b81 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 31 Aug 2022 21:46:47 +0200 Subject: [PATCH 3/6] Refactor mute rule handling to use request.js --- .../retrospring/features/settings/mute.ts | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/app/javascript/retrospring/features/settings/mute.ts b/app/javascript/retrospring/features/settings/mute.ts index e8580f8c..8456c49d 100644 --- a/app/javascript/retrospring/features/settings/mute.ts +++ b/app/javascript/retrospring/features/settings/mute.ts @@ -1,4 +1,5 @@ import Rails from '@rails/ujs'; +import { destroy, post } from '@rails/request.js'; function createSubmitEvent( submit: HTMLButtonElement, @@ -10,29 +11,31 @@ function createSubmitEvent( event.preventDefault(); submit.disabled = true; - Rails.ajax({ - url: '/ajax/mute', - type: 'POST', - dataType: 'json', - data: new URLSearchParams({muted_phrase: textEntry.value}).toString(), - success: (data) => { + 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') + + const newDeleteButton = newEntryFragment.querySelector('button'); newDeleteButton.dataset.id = String(data.id); newDeleteButton.onclick = createDeleteEvent( newEntryFragment.querySelector('.form-group'), newDeleteButton - ) + ); - rulesList.appendChild(newEntryFragment) - - textEntry.value = '' - } - }); + rulesList.appendChild(newEntryFragment); + textEntry.value = ''; + }); }; } @@ -43,18 +46,16 @@ function createDeleteEvent( return () => { deleteButton.disabled = true; - Rails.ajax({ - url: '/ajax/mute/' + deleteButton.dataset.id, - type: 'DELETE', - dataType: 'json', - success: (data) => { + 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; } - } - }) + }); } } From 8ad0cf24b87ec32616f76f7ea9be45cde17d20e8 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Fri, 2 Sep 2022 19:59:43 +0200 Subject: [PATCH 4/6] Fix bug preventing deletion of freshly created mute rules --- app/controllers/ajax/mute_rule_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ajax/mute_rule_controller.rb b/app/controllers/ajax/mute_rule_controller.rb index 0f52695d..96ec1e94 100644 --- a/app/controllers/ajax/mute_rule_controller.rb +++ b/app/controllers/ajax/mute_rule_controller.rb @@ -12,7 +12,7 @@ class Ajax::MuteRuleController < AjaxController @response[:status] = :okay @response[:success] = true @response[:message] = t(".success") - @response[:id] = rule.id + @response[:id] = rule.id.to_s # Returning this as a string as JavaScript defines all numbers as floats end def update From 13c275b18955d131867d33d4e49a135204a926f8 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Fri, 2 Sep 2022 20:02:03 +0200 Subject: [PATCH 5/6] Adjust tests to match controller adjustment --- spec/controllers/ajax/mute_rule_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/ajax/mute_rule_controller_spec.rb b/spec/controllers/ajax/mute_rule_controller_spec.rb index 603a6f97..096e2a74 100644 --- a/spec/controllers/ajax/mute_rule_controller_spec.rb +++ b/spec/controllers/ajax/mute_rule_controller_spec.rb @@ -13,7 +13,7 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do { "success" => true, "status" => "okay", - "id" => MuteRule.last.id, + "id" => MuteRule.last.id.to_s, "message" => "Rule added successfully.", } end From c979a7211d91f025abf102fb5a026382fe9e17e9 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Fri, 2 Sep 2022 20:08:11 +0200 Subject: [PATCH 6/6] Appease the robot dog --- .../ajax/mute_rule_controller_spec.rb | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/spec/controllers/ajax/mute_rule_controller_spec.rb b/spec/controllers/ajax/mute_rule_controller_spec.rb index 096e2a74..f5b504b5 100644 --- a/spec/controllers/ajax/mute_rule_controller_spec.rb +++ b/spec/controllers/ajax/mute_rule_controller_spec.rb @@ -1,20 +1,21 @@ -require 'rails_helper' +# frozen_string_literal: true + +require "rails_helper" describe Ajax::MuteRuleController, :ajax_controller, type: :controller do - describe "#create" do - subject { post(:create, params: params) } + subject { post(:create, params:) } context "when user is signed in" do before(:each) { sign_in(user) } - let(:params) { { muted_phrase: 'test' } } + let(:params) { { muted_phrase: "test" } } let(:expected_response) do { "success" => true, - "status" => "okay", - "id" => MuteRule.last.id.to_s, - "message" => "Rule added successfully.", + "status" => "okay", + "id" => MuteRule.last.id.to_s, + "message" => "Rule added successfully." } end @@ -24,7 +25,7 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do rule = MuteRule.first expect(rule.user_id).to eq(user.id) - expect(rule.muted_phrase).to eq('test') + expect(rule.muted_phrase).to eq("test") end include_examples "returns the expected response" @@ -32,17 +33,17 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do end describe "#update" do - subject { post(:update, params: params) } + subject { post(:update, params:) } context "when user is signed in" do before(:each) { sign_in(user) } - let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') } - let(:params) { { id: rule.id, muted_phrase: 'dogs' } } + let(:rule) { MuteRule.create(user:, muted_phrase: "test") } + let(:params) { { id: rule.id, muted_phrase: "dogs" } } let(:expected_response) do { "success" => true, - "status" => "okay", + "status" => "okay", "message" => "Rule updated successfully." } end @@ -51,7 +52,7 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do subject expect(response).to have_http_status(:success) - expect(rule.reload.muted_phrase).to eq('dogs') + expect(rule.reload.muted_phrase).to eq("dogs") end include_examples "returns the expected response" @@ -59,17 +60,17 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do end describe "#destroy" do - subject { delete(:destroy, params: params) } + subject { delete(:destroy, params:) } context "when user is signed in" do before(:each) { sign_in(user) } - let(:rule) { MuteRule.create(user: user, muted_phrase: 'test') } + let(:rule) { MuteRule.create(user:, muted_phrase: "test") } let(:params) { { id: rule.id } } let(:expected_response) do { "success" => true, - "status" => "okay", + "status" => "okay", "message" => "Rule deleted successfully." } end @@ -79,11 +80,9 @@ describe Ajax::MuteRuleController, :ajax_controller, type: :controller do 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