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 {
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 (
<>
<div class={style.control}>
@ -156,6 +192,18 @@ const Account: Component<{}> = (props) => {
onChange={changeHandlerFactory('remoteDbPassword')}
/>
</Box>
<DialogActions>
<Button
variant='contained'
disabled={!isValid()}
onClick={saveHandler}
>
{t('accountSave')}
</Button>
<Button variant='outlined' color='secondary' onClick={handleClose}>
{t('accountCancel')}
</Button>
</DialogActions>
</Dialog>
</>
);

View File

@ -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);
};

View File

@ -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 })

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);
};

View File

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

View File

@ -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;