From 51ab7c38e04011b90742d32d0a64e27d2401be1a Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 13 Feb 2023 18:30:01 +0100 Subject: [PATCH] Store the current gpx id in account docs --- src/components/gpx-dialog/GpxDialog.tsx | 15 ++++++++++++++- src/db/account.ts | 20 +++++++++++++++++++- src/workers/dispatcher-worker.ts | 9 ++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/components/gpx-dialog/GpxDialog.tsx b/src/components/gpx-dialog/GpxDialog.tsx index 66a8e03..5ef8398 100644 --- a/src/components/gpx-dialog/GpxDialog.tsx +++ b/src/components/gpx-dialog/GpxDialog.tsx @@ -19,6 +19,7 @@ const GpxDialog: Component<{}> = (props) => { const [selectedGpxId, setSelectedGpxId] = createSignal(''); const [gpx, setGpx] = createSignal(); + const [currentAccount, setCurrentAccount] = createSignal(); console.log({ caller: 'GpxDialog', currentGpxId: currentGpxId() }); @@ -33,9 +34,15 @@ const GpxDialog: Component<{}> = (props) => { return newGpx(); }; + dispatch({ action: 'getCurrentAccount' }).then((currentAccount: any) => { + console.log({ caller: 'GpxDialog / currentAccount', currentAccount }); + setCurrentAccount(currentAccount); + setCurrentGpxId(currentAccount.currentGpxId); + setSelectedGpxId(currentAccount.currentGpxId); + }); + const handleClickOpen = async () => { setOpen(true); - setSelectedGpxId(currentGpxId()); if (currentGpxId() !== '') { setGpx(fetchGpx(currentGpxId()) as Gpx); } @@ -131,6 +138,12 @@ const GpxDialog: Component<{}> = (props) => { })) as string; setCurrentGpxId(id); setOpen(false); + await dispatch({ + action: 'putCurrentAccount', + params: { + account: { ...currentAccount(), currentGpxId: currentGpxId() }, + }, + }); }; return ( diff --git a/src/db/account.ts b/src/db/account.ts index c402499..bed34d3 100644 --- a/src/db/account.ts +++ b/src/db/account.ts @@ -1,5 +1,6 @@ -import { getFamily, put } from './lib'; +import { get, getFamily, put } from './lib'; import getUri from '../lib/ids'; +import { getSettings, putSettings } from './settings'; export const initialAccount = { id: 'initial', @@ -12,6 +13,12 @@ export const getAccounts = async () => { return accountDocs.rows.map((row: any) => row.doc.doc); }; +export const getAccount = async (params: any) => { + const { id } = params; + const uri = getUri('account', { account: id }); + return (await get(uri, true)).doc; +}; + export const putAccount = async (params: any) => { const { id, account } = params; const uri = getUri('account', { account: id }); @@ -22,3 +29,14 @@ export const getAccountById = (accounts: any, id: string) => { const targetAccounts: any[] = accounts.filter((acc: any) => acc.id === id); return targetAccounts[0]; }; + +export const getCurrentAccount = async (params: any) => { + const settings = await getSettings(); + return await getAccount({ id: settings.currentAccountId }); +}; + +export const putCurrentAccount = async (params: any) => { + const { account } = params; + const settings = await getSettings(); + await putAccount({ id: settings.currentAccountId, account }); +}; diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 1043097..69857f0 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -1,6 +1,11 @@ /// import { initDb } from '../db'; -import { getAccounts, putAccount } from '../db/account'; +import { + getAccounts, + getCurrentAccount, + putAccount, + putCurrentAccount, +} from '../db/account'; import { cancelWatch, getAndWatch } from '../db/change-handler'; import { putNewGpx, @@ -65,6 +70,8 @@ onmessage = async function (e) { getAccounts, putAccount, + getCurrentAccount, + putCurrentAccount, }; // console.log({ caller: 'dispatcher-worker / onmessage', e });