Refactor mute rule functionality into seperate file
This commit is contained in:
parent
b18c68449e
commit
e4485cb5cd
|
@ -1,18 +1,17 @@
|
||||||
import {createDeleteEvent, createSubmitEvent} from "retrospring/features/settings/mute";
|
import registerEvents from "utilities/registerEvents";
|
||||||
|
import { muteDocumentHandler } from "./mute";
|
||||||
|
import { profileHeaderChangeHandler, profilePictureChangeHandler } from "./crop";
|
||||||
|
import { themeDocumentHandler, themeSubmitHandler } from "./theme";
|
||||||
|
import { userSubmitHandler } from "./password";
|
||||||
|
|
||||||
export default (): void => {
|
export default (): void => {
|
||||||
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement;
|
muteDocumentHandler();
|
||||||
if (!submit || submit.classList.contains('js-initialized')) return;
|
themeDocumentHandler();
|
||||||
|
|
||||||
const rulesList = document.querySelector<HTMLDivElement>('.js-rules-list');
|
registerEvents([
|
||||||
rulesList.querySelectorAll<HTMLDivElement>('.form-group:not(.js-initalized)').forEach(entry => {
|
{ type: 'submit', target: document.querySelector('#update_theme'), handler: themeSubmitHandler },
|
||||||
const button = entry.querySelector('button')
|
{ type: 'submit', target: document.querySelector('#edit_user'), handler: userSubmitHandler },
|
||||||
button.onclick = createDeleteEvent(entry, button)
|
{ type: 'change', target: document.querySelector('#user_profile_picture[type=file]'), handler: profilePictureChangeHandler },
|
||||||
});
|
{ type: 'change', target: document.querySelector('#user_profile_header[type=file]'), handler: profileHeaderChangeHandler }
|
||||||
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')
|
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import Rails from '@rails/ujs';
|
import Rails from '@rails/ujs';
|
||||||
|
|
||||||
export function createSubmitEvent(
|
function createSubmitEvent(
|
||||||
submit: HTMLButtonElement,
|
submit: HTMLButtonElement,
|
||||||
rulesList: HTMLDivElement,
|
rulesList: HTMLDivElement,
|
||||||
textEntry: HTMLButtonElement,
|
textEntry: HTMLButtonElement,
|
||||||
|
@ -36,7 +36,7 @@ export function createSubmitEvent(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createDeleteEvent(
|
function createDeleteEvent(
|
||||||
entry: HTMLDivElement,
|
entry: HTMLDivElement,
|
||||||
deleteButton: HTMLButtonElement
|
deleteButton: HTMLButtonElement
|
||||||
): () => void {
|
): () => void {
|
||||||
|
@ -56,4 +56,21 @@ export function createDeleteEvent(
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function muteDocumentHandler() {
|
||||||
|
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement;
|
||||||
|
if (!submit || submit.classList.contains('js-initialized')) return;
|
||||||
|
|
||||||
|
const rulesList = document.querySelector<HTMLDivElement>('.js-rules-list');
|
||||||
|
rulesList.querySelectorAll<HTMLDivElement>('.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');
|
||||||
}
|
}
|
Loading…
Reference in New Issue