From 8a4396fb2063892601d9580efa428ede4e9c0f2c Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 26 Dec 2022 17:31:05 +0100 Subject: [PATCH] Still working on the account dialog. --- src/components/account/Account.tsx | 54 ++++++++++++++++++++-- src/db/account.ts | 3 +- src/db/index.ts | 72 +++++++++++++++--------------- src/db/settings.ts | 3 +- src/i18n/en.ts | 1 + src/i18n/fr.ts | 2 + 6 files changed, 94 insertions(+), 41 deletions(-) diff --git a/src/components/account/Account.tsx b/src/components/account/Account.tsx index 76ac338..95e7b91 100644 --- a/src/components/account/Account.tsx +++ b/src/components/account/Account.tsx @@ -5,6 +5,8 @@ import PersonIcon from '@suid/icons-material/Person'; import style from './Account.module.css'; import { Box, + Button, + DialogActions, IconButton, InputLabel, NativeSelect, @@ -13,8 +15,6 @@ import { import { useI18n } from '@solid-primitives/i18n'; import Dialog from '../dialog'; import dispatch from '../../workers/dispatcher-main'; -import allGpxes from '../all-gpxes'; -import gpx from '../gpx'; import { cloneDeep } from 'lodash'; import { getAccountById } from '../../db/account'; @@ -63,7 +63,7 @@ const Account: Component<{}> = (props) => { console.log({ caller: 'Account / changeAccountHandler', event, - selectedAccountName: selectedAccountId, + selectedAccountId: selectedAccountId, account: account(), }); }; @@ -81,6 +81,42 @@ const Account: Component<{}> = (props) => { }); }; + const saveHandler = async () => { + const updatedAccount = account(); + if (updatedAccount.id === '--new--') { + updatedAccount.id = updatedAccount.name; + } + await dispatch({ + action: 'putAccount', + params: { id: updatedAccount.id, account: updatedAccount }, + }); + const updatedSettings = { + ...settings(), + currentAccountId: updatedAccount.id, + }; + await dispatch({ + action: 'putSettings', + params: { settings: updatedSettings }, + }); + console.log({ + caller: 'Account / saveHandler', + account: account(), + updatedAccount, + settings: settings(), + updatedSettings, + }); + handleClose(); + location.reload(); + }; + + const isValid = () => { + if (!account().localDb) { + return false; + } + //TODO: add more tests + return true; + }; + return ( <>
@@ -156,6 +192,18 @@ const Account: Component<{}> = (props) => { onChange={changeHandlerFactory('remoteDbPassword')} /> + + + + ); diff --git a/src/db/account.ts b/src/db/account.ts index 7a98676..92878e8 100644 --- a/src/db/account.ts +++ b/src/db/account.ts @@ -12,7 +12,8 @@ export const getAccounts = async () => { return accountDocs.rows.map((row: any) => row.doc.doc); }; -export const putAccount = async (id: string, account: any) => { +export const putAccount = async (params: any) => { + const { id, account } = params; const uri = getUri('account', { account: id }); return await put(uri, 'account', (_: any) => account, {}, true); }; diff --git a/src/db/index.ts b/src/db/index.ts index 00d50c1..93323d8 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -39,21 +39,21 @@ export const initDb = async (params: any) => { if (accounts.length === 0) { accounts[0] = initialAccount; - await putAccount(initialAccount.id, initialAccount); + await putAccount({ id: initialAccount.id, account: initialAccount }); } const settings = await getSettings(); let currentAccount = getAccountById(accounts, settings.currentAccountId); if (currentAccount === undefined) { settings.currentAccountId = accounts[0].id; - await putSettings(settings); + await putSettings({ settings }); currentAccount = accounts[0]; } console.log({ caller: 'initDb', settings, currentAccount }); if (globalThis.db === undefined) { - globalThis.db = new PouchDB('dyomedea', { + globalThis.db = new PouchDB(currentAccount.localDb, { auto_compaction: false, }); } @@ -81,40 +81,40 @@ export const initDb = async (params: any) => { } //await await db.compact(); - const sync = PouchDB.sync( - 'dyomedea', - 'http://admin:password@localhost:5984/dyomedea', - { - live: true, - retry: true, - } - ) - .on('change', function (info) { - // handle change - console.log({ caller: 'Sync / change', info }); - }) - .on('paused', function (err) { - // replication paused (e.g. replication up to date, user went offline) - console.log({ caller: 'Sync / paused', err }); - }) - .on('active', function () { - // replicate resumed (e.g. new changes replicating, user went back online) - console.log({ caller: 'Sync / active' }); - }) - .on('denied', function (err) { - // a document failed to replicate (e.g. due to permissions) - console.error({ caller: 'Sync / denied', err }); - }) - .on('complete', function (info) { - // handle complete - console.log({ caller: 'Sync / complete', info }); - }) - .on('error', function (err) { - // handle error - console.error({ caller: 'Sync / error', err }); - }); + // const sync = PouchDB.sync( + // 'dyomedea', + // 'http://admin:password@localhost:5984/dyomedea', + // { + // live: true, + // retry: true, + // } + // ) + // .on('change', function (info) { + // // handle change + // console.log({ caller: 'Sync / change', info }); + // }) + // .on('paused', function (err) { + // // replication paused (e.g. replication up to date, user went offline) + // console.log({ caller: 'Sync / paused', err }); + // }) + // .on('active', function () { + // // replicate resumed (e.g. new changes replicating, user went back online) + // console.log({ caller: 'Sync / active' }); + // }) + // .on('denied', function (err) { + // // a document failed to replicate (e.g. due to permissions) + // console.error({ caller: 'Sync / denied', err }); + // }) + // .on('complete', function (info) { + // // handle complete + // console.log({ caller: 'Sync / complete', info }); + // }) + // .on('error', function (err) { + // // handle error + // console.error({ caller: 'Sync / error', err }); + // }); - console.log({ caller: 'initDb / before db.changes' }); + // console.log({ caller: 'initDb / before db.changes' }); const changes = db .changes({ since: 'now', live: true, include_docs: false }) diff --git a/src/db/settings.ts b/src/db/settings.ts index 7ee2c80..e1b1b30 100644 --- a/src/db/settings.ts +++ b/src/db/settings.ts @@ -11,6 +11,7 @@ export const getSettings = async () => { } }; -export const putSettings = async (settings: any) => { +export const putSettings = async (params: any) => { + const { settings } = params; return await put('settings', 'settings', (_: any) => settings, {}, true); }; diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 8f067c8..0abad74 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -61,6 +61,7 @@ const dict = { remoteDbServer: 'Remote database server URL', remoteDbUser: 'Remote database user', remoteDbPassword: 'Remote database password', + accountSave: 'Save', }; export default dict; diff --git a/src/i18n/fr.ts b/src/i18n/fr.ts index 0764d3e..8083bea 100644 --- a/src/i18n/fr.ts +++ b/src/i18n/fr.ts @@ -66,6 +66,8 @@ const dict = { remoteDbServer: 'Adresse du serveur de base de données distant', remoteDbUser: 'Utilisateur de la base de données distante', remoteDbPassword: 'Mot de passe de la base de données distante', + accountSave: 'Sauvegarder', + accountCancel: 'Annuler', }; export default dict;