mirror of https://github.com/logos-co/open-law.git
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
export function renameSubCollection() {
|
|
const renameSubCollectionBtns = document.querySelectorAll(
|
|
'[id^="rename-sub-collection-button-"]',
|
|
);
|
|
const subCollectionRenameForms: NodeListOf<HTMLFormElement> =
|
|
document.querySelectorAll('[id^="rename-sub-collection-label-form-"]');
|
|
if (
|
|
renameSubCollectionBtns.length > 0 &&
|
|
subCollectionRenameForms.length > 0
|
|
) {
|
|
renameSubCollectionBtns.forEach((btn, index) => {
|
|
btn.addEventListener('click', () => {
|
|
const inputsForRename: NodeListOf<HTMLInputElement> =
|
|
document.querySelectorAll(`[id^="edit-sub-collection-label-"]`);
|
|
const oldName = inputsForRename[index].value;
|
|
inputsForRename[index].removeAttribute('readonly');
|
|
inputsForRename[index].value = '';
|
|
inputsForRename[index].focus();
|
|
inputsForRename[index].addEventListener('blur', () => {
|
|
inputsForRename[index].value = oldName;
|
|
});
|
|
subCollectionRenameForms[index].addEventListener('submit', async e => {
|
|
e.preventDefault();
|
|
const bookId =
|
|
subCollectionRenameForms[index].getAttribute('data-book-id');
|
|
const subCollectionId = subCollectionRenameForms[index].getAttribute(
|
|
'data-sub-collection-id',
|
|
);
|
|
const newLabel = inputsForRename[index].value;
|
|
inputsForRename[index].readOnly = true;
|
|
|
|
let url = `/book/${bookId}/${subCollectionId}/edit`;
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
label: newLabel,
|
|
}),
|
|
});
|
|
if (response.status == 200) {
|
|
location.reload();
|
|
} else return;
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|