Store the current gpx id in account docs

This commit is contained in:
Eric van der Vlist 2023-02-13 18:30:01 +01:00
parent 8d32fa253a
commit 51ab7c38e0
3 changed files with 41 additions and 3 deletions

View File

@ -19,6 +19,7 @@ const GpxDialog: Component<{}> = (props) => {
const [selectedGpxId, setSelectedGpxId] = createSignal<string>('');
const [gpx, setGpx] = createSignal<Gpx>();
const [currentAccount, setCurrentAccount] = createSignal<any>();
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 (

View File

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

View File

@ -1,6 +1,11 @@
/// <reference lib="webworker" />
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 });