From f37984561576e40594302acdcf10b0f70c0680bd Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 14 Jun 2022 23:16:55 +0200 Subject: [PATCH] Add frontend for blocking anonymous users --- .../ajax/anonymous_block_controller.rb | 24 +++++++++++++++ .../features/inbox/entry/blockAnon.ts | 30 +++++++++++++++++++ .../retrospring/features/inbox/entry/index.ts | 2 ++ app/views/inbox/_entry.haml | 4 +++ config/routes.rb | 1 + 5 files changed, 61 insertions(+) create mode 100644 app/controllers/ajax/anonymous_block_controller.rb create mode 100644 app/javascript/retrospring/features/inbox/entry/blockAnon.ts diff --git a/app/controllers/ajax/anonymous_block_controller.rb b/app/controllers/ajax/anonymous_block_controller.rb new file mode 100644 index 00000000..04f06171 --- /dev/null +++ b/app/controllers/ajax/anonymous_block_controller.rb @@ -0,0 +1,24 @@ +class Ajax::AnonymousBlockController < AjaxController + def create + params.require :question + + question = Question.find(params[:question]) + + AnonymousBlock.create!( + user: current_user, + identifier: AnonymousBlock.get_identifier(question.author_identifier), + question: question, + ) + + question.inboxes.first.destroy + + @response[:status] = :okay + @response[:message] = I18n.t('messages.block.create.okay') + @response[:success] = true + + rescue Errors::Base => e + @response[:status] = e.code + @response[:message] = I18n.t(e.locale_tag) + @response[:success] = false + end +end diff --git a/app/javascript/retrospring/features/inbox/entry/blockAnon.ts b/app/javascript/retrospring/features/inbox/entry/blockAnon.ts new file mode 100644 index 00000000..185489a7 --- /dev/null +++ b/app/javascript/retrospring/features/inbox/entry/blockAnon.ts @@ -0,0 +1,30 @@ +import Rails from '@rails/ujs'; + +import { showErrorNotification, showNotification } from "utilities/notifications"; +import I18n from "retrospring/i18n"; + +export function blockAnonEventHandler(event: Event): void { + const element: HTMLAnchorElement = event.target as HTMLAnchorElement; + + const data = { + question: element.getAttribute('data-q-id'), + }; + + Rails.ajax({ + url: '/ajax/block_anon', + type: 'POST', + data: new URLSearchParams(data).toString(), + success: (data) => { + if (!data.success) return false; + const inboxEntry: Node = element.closest('.inbox-entry'); + + showNotification(data.message); + + (inboxEntry as HTMLElement).remove(); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + showErrorNotification(I18n.translate('frontend.error.message')); + } + }); +} \ No newline at end of file diff --git a/app/javascript/retrospring/features/inbox/entry/index.ts b/app/javascript/retrospring/features/inbox/entry/index.ts index c17405c1..27bbc5ac 100644 --- a/app/javascript/retrospring/features/inbox/entry/index.ts +++ b/app/javascript/retrospring/features/inbox/entry/index.ts @@ -3,12 +3,14 @@ import { answerEntryHandler, answerEntryInputHandler } from './answer'; import { deleteEntryHandler } from './delete'; import optionsEntryHandler from './options'; import { reportEventHandler } from './report'; +import { blockAnonEventHandler } from "retrospring/features/inbox/entry/blockAnon"; export default (): void => { registerEvents([ { type: 'click', target: 'button[name="ib-answer"]', handler: answerEntryHandler, global: true }, { type: 'click', target: '[name="ib-destroy"]', handler: deleteEntryHandler, global: true }, { type: 'click', target: '[name=ib-report]', handler: reportEventHandler, global: true }, + { type: 'click', target: '[name=ib-block-anon]', handler: blockAnonEventHandler, global: true }, { type: 'click', target: 'button[name=ib-options]', handler: optionsEntryHandler, global: true }, { type: 'keydown', target: 'textarea[name=ib-answer]', handler: answerEntryInputHandler, global: true } ]); diff --git a/app/views/inbox/_entry.haml b/app/views/inbox/_entry.haml index b3cb71f3..ff42217b 100644 --- a/app/views/inbox/_entry.haml +++ b/app/views/inbox/_entry.haml @@ -22,6 +22,10 @@ %a.dropdown-item{ name: "ib-report", data: { q_id: i.question.id } } %i.fa.fa-warning = t("voc.report") + - if i.question.author_is_anonymous && i.question.author_identifier.present? + %a.dropdown-item{ name: "ib-block-anon", data: { q_id: i.question.id } } + %i.fa.fa-minus-circle + = t("views.actions.block") - if current_user.has_role? :administrator %a.dropdown-item{ href: rails_admin_path_for_resource(i) } %i.fa.fa-gears diff --git a/config/routes.rb b/config/routes.rb index 03120c1e..6e116e95 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -118,6 +118,7 @@ Rails.application.routes.draw do match '/mute', to: 'mute_rule#create', via: :post, as: :create_mute_rule match '/mute/:id', to: 'mute_rule#update', via: :post, as: :update_mute_rule match '/mute/:id', to: 'mute_rule#destroy', via: :delete, as: :delete_mute_rule + match '/block_anon', to: 'anonymous_block#create', via: :post, as: :block_anon end match '/discover', to: 'discover#index', via: :get, as: :discover