From 0434b9c626aacaba0a86114a72ca46f2e42640b6 Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 22 Feb 2023 14:54:41 +0100 Subject: [PATCH] dbCheck action --- package-lock.json | 19 ++++++---- package.json | 4 ++- src/components/invitation/Invitation.tsx | 44 ++++++++++++++++++++++-- src/lib/to-hex.ts | 18 ++++++++++ src/routes/invitations/[id].tsx | 2 +- 5 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/lib/to-hex.ts diff --git a/package-lock.json b/package-lock.json index 2fbbb26..2f7164d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "@felte/solid": "^1.2.7", "@kobalte/core": "^0.6.1", "@types/pouchdb": "^6.4.0", + "lodash": "^4.17.21", "pouchdb": "^8.0.1", "pouchdb-server": "^4.2.0", "uuid": "^9.0.0" @@ -19,8 +20,9 @@ "@solidjs/router": "^0.7.0", "@solidjs/testing-library": "^0.6.1", "@testing-library/jest-dom": "^5.16.5", + "@types/lodash": "^4.14.191", "@types/testing-library__jest-dom": "^5.14.5", - "@types/uuid": "^9.0.0", + "@types/uuid": "^9.0.1", "@vitest/coverage-c8": "^0.28.5", "@vitest/ui": "^0.28.5", "jsdom": "^21.1.0", @@ -2987,6 +2989,12 @@ "integrity": "sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==", "dev": true }, + "node_modules/@types/lodash": { + "version": "4.14.191", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz", + "integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==", + "dev": true + }, "node_modules/@types/ms": { "version": "0.7.31", "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz", @@ -3186,9 +3194,9 @@ } }, "node_modules/@types/uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-kr90f+ERiQtKWMz5rP32ltJ/BtULDI5RVO0uavn1HQUOwjx0R1h0rnDYNL0CepF1zL5bSY6FISAfd9tOdDhU5Q==", + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-rFT3ak0/2trgvp4yYZo5iKFEPsET7vKydKF+VRCxlQ9bpheehyAJH89dAkaLEq/j/RZXJIqcgsmPJKUP1Z28HA==", "dev": true }, "node_modules/@types/yargs": { @@ -7258,8 +7266,7 @@ "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "node_modules/lodash.debounce": { "version": "4.0.8", diff --git a/package.json b/package.json index be725cd..f3cfc6b 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,9 @@ "@solidjs/router": "^0.7.0", "@solidjs/testing-library": "^0.6.1", "@testing-library/jest-dom": "^5.16.5", + "@types/lodash": "^4.14.191", "@types/testing-library__jest-dom": "^5.14.5", - "@types/uuid": "^9.0.0", + "@types/uuid": "^9.0.1", "@vitest/coverage-c8": "^0.28.5", "@vitest/ui": "^0.28.5", "jsdom": "^21.1.0", @@ -33,6 +34,7 @@ "@felte/solid": "^1.2.7", "@kobalte/core": "^0.6.1", "@types/pouchdb": "^6.4.0", + "lodash": "^4.17.21", "pouchdb": "^8.0.1", "pouchdb-server": "^4.2.0", "uuid": "^9.0.0" diff --git a/src/components/invitation/Invitation.tsx b/src/components/invitation/Invitation.tsx index 7e3c0ca..212d752 100644 --- a/src/components/invitation/Invitation.tsx +++ b/src/components/invitation/Invitation.tsx @@ -8,6 +8,8 @@ import PouchDb from 'pouchdb'; import { v4 as uuid } from 'uuid'; import { put } from '~/lib/db'; import { useNavigate } from 'solid-start'; +import { toHex } from '~/lib/to-hex'; +import { cloneDeep } from 'lodash'; interface Props { values?: any; @@ -39,6 +41,29 @@ const Invitation: Component = (props) => { return id; }); + const [dbChecking, dbCheck] = createServerAction$(async (args: any) => { + console.log({ + caller: 'Invitation / dbCheck', + args, + }); + let baseDbInfo; + try { + const baseDb = new PouchDb(cloneDeep(args)); + baseDbInfo = await baseDb.info(); + } catch (error) { + baseDbInfo = error; + } + const name = `${args.name}/userdb-${toHex(args.auth.username)}`; + let userDbInfo; + try { + const userDb = new PouchDb({ ...args, name }); + userDbInfo = await userDb.info(); + } catch (error) { + userDbInfo = error; + } + return { baseDbInfo, userDbInfo, args, name }; + }); + const submitHandler = async (values: any, context: any) => { console.log({ caller: 'Invitation / submitHandler', @@ -65,11 +90,24 @@ const Invitation: Component = (props) => { navigate(`/invitations/`); }; - const { form } = createForm({ + const { form, data } = createForm({ onSubmit: submitHandler, initialValues: props?.values, }); + const dbCheckHandler = async () => { + const result = await dbCheck({ + name: data('database'), + auth: { username: data('username'), password: data('password') }, + }); + console.log({ + caller: 'Invitation / dbCheckHandler', + props, + data: data(), + result, + }); + }; + console.log({ caller: 'Invitation ', props, @@ -127,13 +165,13 @@ const Invitation: Component = (props) => { Save Delete - Cancel Create - Cancel + DB check + Cancel ); }; diff --git a/src/lib/to-hex.ts b/src/lib/to-hex.ts new file mode 100644 index 0000000..f829233 --- /dev/null +++ b/src/lib/to-hex.ts @@ -0,0 +1,18 @@ +// 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; + }; + \ No newline at end of file diff --git a/src/routes/invitations/[id].tsx b/src/routes/invitations/[id].tsx index ce136a9..5d3b02f 100644 --- a/src/routes/invitations/[id].tsx +++ b/src/routes/invitations/[id].tsx @@ -27,7 +27,7 @@ export default () => { return (
-

Invitation:

+

Invitation

Loading...}>