From 5dbf0801907669c5956b405108b6357b2aeb0132 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 25 Dec 2022 22:09:52 +0100 Subject: [PATCH] Progressing into the implementation of the Account dialog. --- src/components/account/Account.tsx | 5 +---- src/db/account.ts | 8 +++++++- src/db/index.ts | 22 ++++++++++++++++++---- src/db/settings.ts | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/components/account/Account.tsx b/src/components/account/Account.tsx index 296087b..a63a846 100644 --- a/src/components/account/Account.tsx +++ b/src/components/account/Account.tsx @@ -33,10 +33,7 @@ const Account: Component<{}> = (props) => { const newAccounts: any = await dispatch({ action: 'getAccounts', }); - setAccounts([ - { id: '--new--', name: t('newAccount') }, - ...newAccounts.rows, - ]); + setAccounts([{ id: '--new--', name: t('newAccount') }, ...newAccounts]); console.log({ caller: 'Account / handleClickOpen', diff --git a/src/db/account.ts b/src/db/account.ts index b4b8a2d..7a98676 100644 --- a/src/db/account.ts +++ b/src/db/account.ts @@ -8,10 +8,16 @@ export const initialAccount = { }; 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) => { const uri = getUri('account', { account: id }); 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]; +}; diff --git a/src/db/index.ts b/src/db/index.ts index 677af6a..00d50c1 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,8 +1,14 @@ import _ from 'lodash'; import PouchDB from 'pouchdb'; import uri from '../lib/ids'; -import { getAccounts, initialAccount, putAccount } from './account'; +import { + getAccountById, + getAccounts, + initialAccount, + putAccount, +} from './account'; import changeHandler from './change-handler'; +import { getSettings, putSettings } from './settings'; 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 }); if (accounts.length === 0) { @@ -36,6 +42,16 @@ export const initDb = async (params: any) => { 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) { globalThis.db = new PouchDB('dyomedea', { auto_compaction: false, @@ -65,7 +81,6 @@ export const initDb = async (params: any) => { } //await await db.compact(); - const sync = PouchDB.sync( 'dyomedea', 'http://admin:password@localhost:5984/dyomedea', @@ -116,5 +131,4 @@ export const initDb = async (params: any) => { // changes.cancel(); globalThis.dbReady = true; - }; diff --git a/src/db/settings.ts b/src/db/settings.ts index c64a700..7ee2c80 100644 --- a/src/db/settings.ts +++ b/src/db/settings.ts @@ -4,7 +4,7 @@ const emptySettings = {}; export const getSettings = async () => { try { - return await get('settings', true); + return (await get('settings', true)).doc; } catch (err) { console.error({ caller: 'getSettings', err }); return emptySettings;