Still working on the account dialog.

This commit is contained in:
Eric van der Vlist 2022-12-26 17:31:05 +01:00
parent 1d6c3a858d
commit 8a4396fb20
6 changed files with 94 additions and 41 deletions

View File

@ -5,6 +5,8 @@ import PersonIcon from '@suid/icons-material/Person';
import style from './Account.module.css'; import style from './Account.module.css';
import { import {
Box, Box,
Button,
DialogActions,
IconButton, IconButton,
InputLabel, InputLabel,
NativeSelect, NativeSelect,
@ -13,8 +15,6 @@ import {
import { useI18n } from '@solid-primitives/i18n'; import { useI18n } from '@solid-primitives/i18n';
import Dialog from '../dialog'; import Dialog from '../dialog';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
import allGpxes from '../all-gpxes';
import gpx from '../gpx';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { getAccountById } from '../../db/account'; import { getAccountById } from '../../db/account';
@ -63,7 +63,7 @@ const Account: Component<{}> = (props) => {
console.log({ console.log({
caller: 'Account / changeAccountHandler', caller: 'Account / changeAccountHandler',
event, event,
selectedAccountName: selectedAccountId, selectedAccountId: selectedAccountId,
account: account(), 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 ( return (
<> <>
<div class={style.control}> <div class={style.control}>
@ -156,6 +192,18 @@ const Account: Component<{}> = (props) => {
onChange={changeHandlerFactory('remoteDbPassword')} onChange={changeHandlerFactory('remoteDbPassword')}
/> />
</Box> </Box>
<DialogActions>
<Button
variant='contained'
disabled={!isValid()}
onClick={saveHandler}
>
{t('accountSave')}
</Button>
<Button variant='outlined' color='secondary' onClick={handleClose}>
{t('accountCancel')}
</Button>
</DialogActions>
</Dialog> </Dialog>
</> </>
); );

View File

@ -12,7 +12,8 @@ export const getAccounts = async () => {
return accountDocs.rows.map((row: any) => row.doc.doc); 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 }); const uri = getUri('account', { account: id });
return await put(uri, 'account', (_: any) => account, {}, true); return await put(uri, 'account', (_: any) => account, {}, true);
}; };

View File

@ -39,21 +39,21 @@ export const initDb = async (params: any) => {
if (accounts.length === 0) { if (accounts.length === 0) {
accounts[0] = initialAccount; accounts[0] = initialAccount;
await putAccount(initialAccount.id, initialAccount); await putAccount({ id: initialAccount.id, account: initialAccount });
} }
const settings = await getSettings(); const settings = await getSettings();
let currentAccount = getAccountById(accounts, settings.currentAccountId); let currentAccount = getAccountById(accounts, settings.currentAccountId);
if (currentAccount === undefined) { if (currentAccount === undefined) {
settings.currentAccountId = accounts[0].id; settings.currentAccountId = accounts[0].id;
await putSettings(settings); await putSettings({ settings });
currentAccount = accounts[0]; currentAccount = accounts[0];
} }
console.log({ caller: 'initDb', settings, currentAccount }); console.log({ caller: 'initDb', settings, currentAccount });
if (globalThis.db === undefined) { if (globalThis.db === undefined) {
globalThis.db = new PouchDB('dyomedea', { globalThis.db = new PouchDB(currentAccount.localDb, {
auto_compaction: false, auto_compaction: false,
}); });
} }
@ -81,40 +81,40 @@ export const initDb = async (params: any) => {
} }
//await await db.compact(); //await await db.compact();
const sync = PouchDB.sync( // const sync = PouchDB.sync(
'dyomedea', // 'dyomedea',
'http://admin:password@localhost:5984/dyomedea', // 'http://admin:password@localhost:5984/dyomedea',
{ // {
live: true, // live: true,
retry: true, // retry: true,
} // }
) // )
.on('change', function (info) { // .on('change', function (info) {
// handle change // // handle change
console.log({ caller: 'Sync / change', info }); // console.log({ caller: 'Sync / change', info });
}) // })
.on('paused', function (err) { // .on('paused', function (err) {
// replication paused (e.g. replication up to date, user went offline) // // replication paused (e.g. replication up to date, user went offline)
console.log({ caller: 'Sync / paused', err }); // console.log({ caller: 'Sync / paused', err });
}) // })
.on('active', function () { // .on('active', function () {
// replicate resumed (e.g. new changes replicating, user went back online) // // replicate resumed (e.g. new changes replicating, user went back online)
console.log({ caller: 'Sync / active' }); // console.log({ caller: 'Sync / active' });
}) // })
.on('denied', function (err) { // .on('denied', function (err) {
// a document failed to replicate (e.g. due to permissions) // // a document failed to replicate (e.g. due to permissions)
console.error({ caller: 'Sync / denied', err }); // console.error({ caller: 'Sync / denied', err });
}) // })
.on('complete', function (info) { // .on('complete', function (info) {
// handle complete // // handle complete
console.log({ caller: 'Sync / complete', info }); // console.log({ caller: 'Sync / complete', info });
}) // })
.on('error', function (err) { // .on('error', function (err) {
// handle error // // handle error
console.error({ caller: 'Sync / error', err }); // console.error({ caller: 'Sync / error', err });
}); // });
console.log({ caller: 'initDb / before db.changes' }); // console.log({ caller: 'initDb / before db.changes' });
const changes = db const changes = db
.changes({ since: 'now', live: true, include_docs: false }) .changes({ since: 'now', live: true, include_docs: false })

View File

@ -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); return await put('settings', 'settings', (_: any) => settings, {}, true);
}; };

View File

@ -61,6 +61,7 @@ const dict = {
remoteDbServer: 'Remote database server URL', remoteDbServer: 'Remote database server URL',
remoteDbUser: 'Remote database user', remoteDbUser: 'Remote database user',
remoteDbPassword: 'Remote database password', remoteDbPassword: 'Remote database password',
accountSave: 'Save',
}; };
export default dict; export default dict;

View File

@ -66,6 +66,8 @@ const dict = {
remoteDbServer: 'Adresse du serveur de base de données distant', remoteDbServer: 'Adresse du serveur de base de données distant',
remoteDbUser: 'Utilisateur de la base de données distante', remoteDbUser: 'Utilisateur de la base de données distante',
remoteDbPassword: 'Mot de passe 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; export default dict;