diff --git a/frontend/static/js/general.js b/frontend/static/js/general.js index 26d3c43..cc249ee 100644 --- a/frontend/static/js/general.js +++ b/frontend/static/js/general.js @@ -48,20 +48,71 @@ function logout() { 'method': 'POST' }) .then(response => { - const new_stor = JSON.parse(localStorage.getItem('MIND')) - new_stor.api_key = null - localStorage.setItem('MIND', JSON.stringify(new_stor)); + setLocalStorage({'api_key': null}); window.location.href = `${url_prefix}/`; }); }; +// +// LocalStorage +// +const default_values = { + 'api_key': null, + 'locale': 'en-GB', + 'default_service': null +}; + +function setupLocalStorage() { + if (!localStorage.getItem('MIND')) + localStorage.setItem('MIND', JSON.stringify(default_values)); + + const missing_keys = [ + ...Object.keys(default_values) + ].filter(e => + ![...Object.keys(JSON.parse(localStorage.getItem('MIND')))].includes(e) + ) + + if (missing_keys.length) { + const storage = JSON.parse(localStorage.getItem('MIND')); + + missing_keys.forEach(missing_key => { + storage[missing_key] = default_values[missing_key] + }) + + localStorage.setItem('MIND', JSON.stringify(storage)); + }; + return; +}; + +function getLocalStorage(keys) { + const storage = JSON.parse(localStorage.getItem('MIND')); + const result = {}; + if (typeof keys === 'string') + result[keys] = storage[keys]; + + else if (typeof keys === 'object') + for (const key in keys) + result[key] = storage[key]; + + return result; +}; + +function setLocalStorage(keys_values) { + const storage = JSON.parse(localStorage.getItem('MIND')); + + for (const [key, value] of Object.entries(keys_values)) + storage[key] = value; + + localStorage.setItem('MIND', JSON.stringify(storage)); + return; +}; + // code run on load -if (localStorage.getItem('MIND') === null) - localStorage.setItem('MIND', JSON.stringify({'api_key': null, 'locale': 'en-GB'})) +setupLocalStorage(); const url_prefix = document.getElementById('url_prefix').dataset.value; -const api_key = JSON.parse(localStorage.getItem('MIND')).api_key; +const api_key = getLocalStorage('api_key')['api_key']; if (api_key === null) { window.location.href = `${url_prefix}/`; }; diff --git a/frontend/static/js/library.js b/frontend/static/js/library.js index 215d006..20f92af 100644 --- a/frontend/static/js/library.js +++ b/frontend/static/js/library.js @@ -58,7 +58,7 @@ function fillTable(table, results) { const time = document.createElement('p'); var offset = new Date(r.time * 1000).getTimezoneOffset() * -60; var d = new Date((r.time + offset) * 1000); - var formatted_date = d.toLocaleString(JSON.parse(localStorage.getItem('MIND')).locale); + var formatted_date = d.toLocaleString(getLocalStorage('locale')['locale']); if (r.repeat_interval !== null) { if (r.repeat_interval === 1) { var quantity = r.repeat_quantity.endsWith('s') ? r.repeat_quantity.slice(0, -1) : r.repeat_quantity; diff --git a/frontend/static/js/notification.js b/frontend/static/js/notification.js index 7cd871c..e3d7e94 100644 --- a/frontend/static/js/notification.js +++ b/frontend/static/js/notification.js @@ -8,6 +8,24 @@ function fillNotificationSelection() { if (json.result.length) { document.getElementById('add-reminder').classList.remove('error', 'error-icon'); + const default_select = document.querySelector('#default-service-input'); + default_select.innerHTML = ''; + let default_service = getLocalStorage('default_service')['default_service']; + json.result.forEach(service => { + const entry = document.createElement('option'); + entry.value = service.id; + entry.innerText = service.title; + if (default_service === service.id) + entry.setAttribute('selected', ''); + default_select.appendChild(entry); + }); + if (!document.querySelector(`#default-service-input > option[value="${default_service}"]`)) + setLocalStorage({'default_service': + parseInt(document.querySelector('#default-service-input > option')?.value) + || null + }); + default_service = getLocalStorage('default_service')['default_service']; + inputs.notification_service.innerHTML = ''; json.result.forEach(service => { const entry = document.createElement('div'); @@ -85,6 +103,18 @@ function fillNotificationSelection() { }); } else { document.getElementById('add-reminder').classList.add('error', 'error-icon'); + + inputs.notification_service.innerHTML = ''; + + const default_select = document.querySelector('#default-service-input'); + default_select.innerHTML = ''; + + const default_service = getLocalStorage('default_service')['default_service']; + if (!document.querySelector(`#default-service-input > option[value="${default_service}"]`)) + setLocalStorage({'default_service': + parseInt(document.querySelector('#default-service-input > option')?.value) + || null + }); }; }) .catch(e => { @@ -139,8 +169,9 @@ function deleteService(id) { if (json.error !== null) return Promise.reject(json); row.remove(); - if (document.querySelectorAll('#services-list > tr:not(#add-row)').length === 0) - document.getElementById('add-entry').classList.add('error', 'error-icon'); + fillNotificationSelection(); + if (document.querySelectorAll('#services-list > tr').length === 0) + document.getElementById('add-reminder').classList.add('error', 'error-icon'); }) .catch(e => { if (e.error === 'ApiKeyExpired' || e.error === 'ApiKeyInvalid') diff --git a/frontend/static/js/settings.js b/frontend/static/js/settings.js index b422e51..e73559d 100644 --- a/frontend/static/js/settings.js +++ b/frontend/static/js/settings.js @@ -1,5 +1,14 @@ function loadSettings() { - document.getElementById('locale-input').value = JSON.parse(localStorage.getItem('MIND')).locale; + document.getElementById('locale-input').value = getLocalStorage('locale')['locale']; +}; + +function updateLocale(e) { + setLocalStorage({'locale': e.target.value}); + window.location.reload(); +}; + +function updateDefaultService(e) { + setLocalStorage({'default_service': parseInt(e.target.value)}); }; function changePassword() { @@ -23,13 +32,6 @@ function changePassword() { }); }; -function updateLocale(e) { - const new_stor = JSON.parse(localStorage.getItem('MIND')); - new_stor.locale = e.target.value; - localStorage.setItem('MIND', JSON.stringify(new_stor)); - window.location.reload(); -}; - function deleteAccount() { fetch(`${url_prefix}/api/user?api_key=${api_key}`, { 'method': 'DELETE' @@ -43,6 +45,7 @@ function deleteAccount() { loadSettings(); -document.getElementById('change-password-form').setAttribute('action', 'javascript:changePassword()'); document.getElementById('locale-input').addEventListener('change', updateLocale); +document.querySelector('#default-service-input').addEventListener('change', updateDefaultService); +document.getElementById('change-password-form').setAttribute('action', 'javascript:changePassword()'); document.getElementById('delete-account-button').addEventListener('click', e => deleteAccount()); diff --git a/frontend/static/js/show.js b/frontend/static/js/show.js index 672f000..afd5076 100644 --- a/frontend/static/js/show.js +++ b/frontend/static/js/show.js @@ -1,10 +1,11 @@ function showAdd(type) { + const default_service = getLocalStorage('default_service')['default_service']; inputs.template.value = '0'; inputs.title.value = ''; inputs.text.value = ''; inputs.time.value = ''; inputs.notification_service.querySelectorAll('input[type="checkbox"]').forEach(c => c.checked = false); - inputs.notification_service.querySelector('input[type="checkbox"]:first-child').checked = true; + inputs.notification_service.querySelector(`input[type="checkbox"][data-id="${default_service}"]`).checked = true; toggleNormal(); toggleColor(true); document.getElementById('test-reminder').classList.remove('show-sent'); diff --git a/frontend/templates/reminders.html b/frontend/templates/reminders.html index 208d05f..a186765 100644 --- a/frontend/templates/reminders.html +++ b/frontend/templates/reminders.html @@ -252,11 +252,6 @@

Settings

-

Change Password

-
- - -

Locale

+ +

Default Notification Service

+ + +

Change Password

+
+ + +
+

Delete Account