open-law/src/renameSubCollection.ts

52 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-05-17 12:32:52 +00:00
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;
2023-05-29 15:07:40 +00:00
let url = `/book/${bookId}/${subCollectionId}/edit`;
2023-05-17 12:32:52 +00:00
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;
});
});
});
}
}