import { createForm } from '@felte/solid'; import { Component, createSignal, Match, Switch } from 'solid-js'; import { TextField, Button, Dialog } from '@kobalte/core'; import './style.css'; import { createServerAction$ } from 'solid-start/server'; 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; } const User: Component = (props) => { const navigate = useNavigate(); const [openDialog, setOpenDialog] = createSignal(false); const [openUsernamePasswordDialog, setOpenUserNamePasswordDialog] = createSignal(false); const [status, setStatus] = createSignal(); const [saving, save] = createServerAction$(async (args: any) => { const { props, values } = args; console.log({ caller: 'User / save', props, values, args, id: props.values?._id, }); const db = new PouchDb('.db'); const id = props.values?._id ?? `user/${uuid()}`; await put({ db, doc: { _id: id, date: new Date().toISOString(), type: 'user', ...values, }, }); return id; }); const [dbChecking, dbCheck] = createServerAction$(async (args: any) => { console.log({ caller: 'User / dbCheck', args, }); let baseDbInfoAuth; try { const baseDb = new PouchDb(cloneDeep(args)); baseDbInfoAuth = await baseDb.info(); } catch (error) { baseDbInfoAuth = { error }; } let baseDbInfoAnon; try { const baseDb = new PouchDb(args.name); baseDbInfoAnon = await baseDb.info(); } catch (error) { baseDbInfoAnon = { 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 }; } let userDbInfoAnon; try { const userDb = new PouchDb(name); userDbInfoAnon = await userDb.info(); } catch (error) { userDbInfoAnon = { error }; } return { baseDbInfoAuth, baseDbInfoAnon, userDbInfo, userDbInfoAnon }; }); const submitHandler = async (values: any, context: any) => { console.log({ caller: 'User / submitHandler', props, values, context, }); const id = await save({ values, props }); if (!props.values) { navigate(`/user/${encodeURIComponent(id)}`); } }; const cancelHandler = () => { navigate(`/user/`); }; const deleteHandler = async () => { console.log({ caller: 'User / deleteHandler', props, }); await save({ props, values: { _deleted: true } }); navigate(`/user/`); }; 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: 'User / dbCheckHandler', props, data: data(), result, }); if (result.baseDbInfoAnon.error) { setStatus('baseError'); } else if (!result.userDbInfo.error) { setStatus('OK'); } else if ( result.userDbInfoAnon.error.reason === 'You are not authorized to access this db.' ) { setStatus('passwordError'); } else { setStatus('notFound'); } setOpenDialog(true); }; const createUserHandler = async () => { console.log({ caller: 'User / createUserHandler', props, data: data(), }); setOpenUserNamePasswordDialog(true); }; console.log({ caller: 'User ', props, }); return ( <>
Mail address Please provide a valid URL Database Please provide a valid URL User name Password Please provide a valid password Save Delete Create DB check Cancel
Database check X
The database address seems to be wrong !! The user is already existing.
The user must be created.
Create user
The user seems to exist but the password is wrong.
Update password
); }; export default User;