From 05ed2143678acfd2ad4a9f2ab7e7e60e5b032f52 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 23 Dec 2021 01:29:48 +0100 Subject: [PATCH] Implement frontend for deleting mute rules --- .../retrospring/features/settings/index.ts | 6 +++- .../retrospring/features/settings/mute.ts | 29 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/javascript/retrospring/features/settings/index.ts b/app/javascript/retrospring/features/settings/index.ts index 507031a0..e885ca11 100644 --- a/app/javascript/retrospring/features/settings/index.ts +++ b/app/javascript/retrospring/features/settings/index.ts @@ -1,10 +1,14 @@ -import {createSubmitEvent} from "retrospring/features/settings/mute"; +import {createDeleteEvent, createSubmitEvent} from "retrospring/features/settings/mute"; export default (): void => { const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement; if (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; diff --git a/app/javascript/retrospring/features/settings/mute.ts b/app/javascript/retrospring/features/settings/mute.ts index e9ef07fb..a402538d 100644 --- a/app/javascript/retrospring/features/settings/mute.ts +++ b/app/javascript/retrospring/features/settings/mute.ts @@ -21,7 +21,12 @@ export function createSubmitEvent( const newEntryFragment = template.content.cloneNode(true) as Element; newEntryFragment.querySelector('input').value = textEntry.value; - newEntryFragment.querySelector('button').dataset.id = String(data.id); + const newDeleteButton = newEntryFragment.querySelector('button') + newDeleteButton.dataset.id = String(data.id); + newDeleteButton.onclick = createDeleteEvent( + newEntryFragment.querySelector('.form-group'), + newDeleteButton + ) rulesList.appendChild(newEntryFragment) @@ -29,4 +34,26 @@ export function createSubmitEvent( } }); }; +} + +export function createDeleteEvent( + entry: HTMLDivElement, + deleteButton: HTMLButtonElement +): () => void { + return () => { + deleteButton.disabled = true; + + Rails.ajax({ + url: '/ajax/mute/' + deleteButton.dataset.id, + type: 'DELETE', + dataType: 'json', + success: (data) => { + if (data.success) { + entry.parentElement.removeChild(entry) + } else { + deleteButton.disabled = false; + } + } + }) + } } \ No newline at end of file