From ac3ae5455f5cac306d0a3c5f992e1facb043b2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Tue, 25 Jul 2023 22:26:29 +0200 Subject: [PATCH] Refactor the account sync --- pwa/app.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/pwa/app.js b/pwa/app.js index 104445f..94db75f 100644 --- a/pwa/app.js +++ b/pwa/app.js @@ -70,21 +70,31 @@ document.addEventListener('DOMContentLoaded', () => { }); } + // Function to get an account by its name + function getAccountByName(name) { + const account_string = localStorage.getItem(name); + const account_raw = JSON.parse(account_string); + return new Account( + account_raw.local_part, + account_raw.separator, + account_raw.domain, + account_raw.key, + ); + } + // Function to synchronize the account list function syncAccountList() { var acc_list = document.querySelector('#account-name'); while (acc_list.lastElementChild) { acc_list.removeChild(acc_list.lastElementChild); } + var account_names = []; for (var i = 0, len = localStorage.length; i < len; ++i) { - const account_string = localStorage.getItem(localStorage.key(i)); - const account_raw = JSON.parse(account_string); - const account = new Account( - account_raw.local_part, - account_raw.separator, - account_raw.domain, - account_raw.key, - ); + account_names.push(localStorage.key(i)); + } + account_names.sort(); + for (const name of account_names) { + const account = getAccountByName(name); const new_elem = new Option(account.getName(), account.getName()); acc_list.appendChild(new_elem); }