Disable load previous buttons when there are no saves

This commit is contained in:
projectmoon 2023-01-04 12:22:06 +01:00
parent 0daf5a8e52
commit 017a624c66
2 changed files with 28 additions and 5 deletions

View File

@ -103,7 +103,8 @@ function processFragment() {
async function saveToDatabase(filename: string, xml: string) { async function saveToDatabase(filename: string, xml: string) {
db.entries db.entries
.put({ filename, backupData: xml }) .put({ filename, backupData: xml })
.catch(err => displayFatalError('Saving Failed', err)); .then(_ => toggleLoadPrevious(true))
.catch(err => console.error('Saving Failed', err));
} }
function displayBackup(xsltProcessor: XSLTProcessor, filename: string, xmlText: string): boolean { function displayBackup(xsltProcessor: XSLTProcessor, filename: string, xmlText: string): boolean {
@ -136,7 +137,29 @@ function displayBackup(xsltProcessor: XSLTProcessor, filename: string, xmlText:
} }
} }
function initEvents() { function toggleLoadPrevious(toggle: boolean) {
ui.loadPreviousButtons.forEach(btn => {
if (toggle) {
btn.classList.remove('disabled');
} else {
btn.classList.add('disabled');
}
});
}
async function initEvents() {
db.entries.count()
.then(count => {
if (count < 1) {
toggleLoadPrevious(false);
}
})
.catch(err => {
//Something wrong with db access, disable buttons anyway.
console.error(err);
toggleLoadPrevious(false);
});
ui.loadPreviousButtons.forEach(button => button.addEventListener('click', async () => { ui.loadPreviousButtons.forEach(button => button.addEventListener('click', async () => {
await populateSavedBackups(); await populateSavedBackups();
})); }));
@ -190,7 +213,7 @@ async function populateSavedBackups() {
} }
window.addEventListener('DOMContentLoaded', async () => { window.addEventListener('DOMContentLoaded', async () => {
initEvents(); await initEvents();
showLoadingIndicator(false); showLoadingIndicator(false);
showApplication(true); showApplication(true);
}); });

View File

@ -8,9 +8,9 @@ export const chatDisplay: HTMLElement = document.getElementById('chat-display')!
export const fileSelector: HTMLInputElement = document.getElementById('backup-file')! as HTMLInputElement; export const fileSelector: HTMLInputElement = document.getElementById('backup-file')! as HTMLInputElement;
export const savedBackups: HTMLSelectElement = document.getElementById('saved-backups')! as HTMLSelectElement; export const savedBackups: HTMLSelectElement = document.getElementById('saved-backups')! as HTMLSelectElement;
export const viewNewButtons = export const viewNewButtons =
document.querySelectorAll('#unloaded-view-new-button, #loaded-view-new-button'); <NodeListOf<HTMLElement>>document.querySelectorAll('#unloaded-view-new-button, #loaded-view-new-button');
export const loadPreviousButtons = export const loadPreviousButtons =
document.querySelectorAll('#unloaded-load-previous-button, #loaded-load-previous-button'); <NodeListOf<HTMLElement>>document.querySelectorAll('#unloaded-load-previous-button, #loaded-load-previous-button');
export const previousBackupsList: HTMLDivElement = document.getElementById('previous-backups-list')! as HTMLDivElement; export const previousBackupsList: HTMLDivElement = document.getElementById('previous-backups-list')! as HTMLDivElement;
export const noFileLoadedControls: HTMLDivElement = document.getElementById('no-file-loaded-controls')! as HTMLDivElement; export const noFileLoadedControls: HTMLDivElement = document.getElementById('no-file-loaded-controls')! as HTMLDivElement;
export const fileLoadedControls: HTMLDivElement = document.getElementById('file-loaded-controls')! as HTMLDivElement; export const fileLoadedControls: HTMLDivElement = document.getElementById('file-loaded-controls')! as HTMLDivElement;