Progressing into the implementation of the Account dialog.

This commit is contained in:
Eric van der Vlist 2022-12-25 22:09:52 +01:00
parent 5ca12b6780
commit 5dbf080190
4 changed files with 27 additions and 10 deletions

View File

@ -33,10 +33,7 @@ const Account: Component<{}> = (props) => {
const newAccounts: any = await dispatch({ const newAccounts: any = await dispatch({
action: 'getAccounts', action: 'getAccounts',
}); });
setAccounts([ setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]);
{ id: '--new--', name: t('newAccount') },
...newAccounts.rows,
]);
console.log({ console.log({
caller: 'Account / handleClickOpen', caller: 'Account / handleClickOpen',

View File

@ -8,10 +8,16 @@ export const initialAccount = {
}; };
export const getAccounts = async () => { export const getAccounts = async () => {
return await getFamily('account', {}, true); const accountDocs = await getFamily('account', { include_docs: true }, true);
return accountDocs.rows.map((row: any) => row.doc.doc);
}; };
export const putAccount = async (id: string, account: any) => { export const putAccount = async (id: string, account: any) => {
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);
}; };
export const getAccountById = (accounts: any, id: string) => {
const targetAccounts: any[] = accounts.filter((acc: any) => acc.id === id);
return targetAccounts[0];
};

View File

@ -1,8 +1,14 @@
import _ from 'lodash'; import _ from 'lodash';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import uri from '../lib/ids'; import uri from '../lib/ids';
import { getAccounts, initialAccount, putAccount } from './account'; import {
getAccountById,
getAccounts,
initialAccount,
putAccount,
} from './account';
import changeHandler from './change-handler'; import changeHandler from './change-handler';
import { getSettings, putSettings } from './settings';
const dbDefinitionId = uri('dbdef', {}); const dbDefinitionId = uri('dbdef', {});
@ -28,7 +34,7 @@ export const initDb = async (params: any) => {
}); });
} }
const accounts = (await getAccounts()).rows; const accounts = await getAccounts();
console.log({ caller: 'initDb', accounts }); console.log({ caller: 'initDb', accounts });
if (accounts.length === 0) { if (accounts.length === 0) {
@ -36,6 +42,16 @@ export const initDb = async (params: any) => {
await putAccount(initialAccount.id, initialAccount); await putAccount(initialAccount.id, initialAccount);
} }
const settings = await getSettings();
let currentAccount = getAccountById(accounts, settings.currentAccountId);
if (currentAccount === undefined) {
settings.currentAccountId = accounts[0].id;
await putSettings(settings);
currentAccount = accounts[0];
}
console.log({ caller: 'initDb', settings, currentAccount });
if (globalThis.db === undefined) { if (globalThis.db === undefined) {
globalThis.db = new PouchDB('dyomedea', { globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false, auto_compaction: false,
@ -65,7 +81,6 @@ 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',
@ -116,5 +131,4 @@ export const initDb = async (params: any) => {
// changes.cancel(); // changes.cancel();
globalThis.dbReady = true; globalThis.dbReady = true;
}; };

View File

@ -4,7 +4,7 @@ const emptySettings = {};
export const getSettings = async () => { export const getSettings = async () => {
try { try {
return await get('settings', true); return (await get('settings', true)).doc;
} catch (err) { } catch (err) {
console.error({ caller: 'getSettings', err }); console.error({ caller: 'getSettings', err });
return emptySettings; return emptySettings;