First working version with the account dialog.

This commit is contained in:
Eric van der Vlist 2022-12-26 18:04:01 +01:00
parent 8a4396fb20
commit ad9b70173f
2 changed files with 59 additions and 32 deletions

View File

@ -1,6 +1,7 @@
import _ from 'lodash';
import PouchDB from 'pouchdb';
import uri from '../lib/ids';
import { toHex } from '../lib/to-hex';
import {
getAccountById,
getAccounts,
@ -81,38 +82,47 @@ 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 });
// });
if (currentAccount.remoteDbServer) {
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
currentAccount.remoteDbUser
)}`;
const remoteDb = new PouchDB(url, {
auth: {
username: currentAccount.remoteDbUser,
password: currentAccount.remoteDbPassword,
},
skip_setup: true,
});
const sync = PouchDB.sync(db, remoteDb, {
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' });

17
src/lib/to-hex.ts Normal file
View File

@ -0,0 +1,17 @@
// see https://9to5answer.com/javascript-unicode-string-to-hex
export const toHex = (str: string) => {
let hex;
try {
hex = unescape(encodeURIComponent(str))
.split('')
.map(function (v) {
return v.charCodeAt(0).toString(16);
})
.join('');
} catch (e) {
hex = str;
console.log('invalid text input: ' + str);
}
return hex;
};