mirror of
https://github.com/Casvt/MIND.git
synced 2026-02-19 11:54:46 -05:00
Added option for default notification service (#56)
This commit is contained in:
@@ -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}/`;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -252,11 +252,6 @@
|
||||
<div id="settings">
|
||||
<h2>Settings</h2>
|
||||
<div class="settings-container">
|
||||
<h3>Change Password</h3>
|
||||
<form id="change-password-form">
|
||||
<input type="password" id="password-input" autocomplete="new-password" required>
|
||||
<button type="submit">Change</button>
|
||||
</form>
|
||||
<h3>Locale</h3>
|
||||
<select id="locale-input">
|
||||
<option value="ar-SA">ar-SA</option>
|
||||
@@ -313,6 +308,16 @@
|
||||
<option value="zh-HK">zh-HK</option>
|
||||
<option value="zh-TW">zh-TW</option>
|
||||
</select>
|
||||
|
||||
<h3>Default Notification Service</h3>
|
||||
<select id="default-service-input"></select>
|
||||
|
||||
<h3>Change Password</h3>
|
||||
<form id="change-password-form">
|
||||
<input type="password" id="password-input" autocomplete="new-password" required>
|
||||
<button type="submit">Change</button>
|
||||
</form>
|
||||
|
||||
<h3>Delete Account</h3>
|
||||
<button id="delete-account-button">Delete Account</button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user