demarches-normaliennes/app/javascript/new_design/messagerie.js
Paul Chavard 44c64669e9 Revert "Merge pull request #6787 from tchak/use-vite"
This reverts commit 5d572727b5, reversing
changes made to 43be4482ee.
2022-03-31 12:07:52 +02:00

58 lines
1.4 KiB
JavaScript

import { scrollTo, scrollToBottom } from '@utils';
function scrollMessagerie() {
const ul = document.querySelector('.messagerie ul');
if (ul) {
const elementToScroll = document.querySelector('.date.highlighted');
if (elementToScroll) {
scrollTo(ul, elementToScroll);
} else {
scrollToBottom(ul);
}
}
}
function saveMessageContent() {
const commentaireForms = Array.from(
document.querySelectorAll('form[data-persisted-content-id]')
);
if (commentaireForms.length) {
const commentaireInputs = Array.from(
document.querySelectorAll('.persisted-input')
);
const persistedContentIds = commentaireForms.map(
(form) => form.dataset.persistedContentId
);
const keys = persistedContentIds.map((key) => `persisted-value-${key}`);
const object = commentaireInputs.map((input, index) => {
return {
input: input,
form: commentaireForms[index],
key: keys[index]
};
});
for (const el of object) {
if (localStorage.getItem(el.key)) {
el.input.value = localStorage.getItem(el.key);
}
el.input.addEventListener('change', (event) => {
localStorage.setItem(el.key, event.target.value);
});
el.form.addEventListener('submit', () => {
localStorage.removeItem(el.key);
});
}
}
}
addEventListener('ds:page:update', scrollMessagerie);
addEventListener('ds:page:update', saveMessageContent);