import { createForm } from '@felte/solid'; import { Component, createSignal, Index, Match, onMount, Show, Switch, } from 'solid-js'; import { TextField, Button, Progress } from '@kobalte/core'; import reporter from '@felte/reporter-tippy'; import './style.css'; import { useNavigate } from 'solid-start'; import { adminCredentials } from '../credentials'; import { put } from '~/lib/put'; import { del } from '~/lib/del'; import { isFunction } from 'lodash'; import { userId } from '~/lib/user-id'; import { userExists } from '~/lib/user-exists'; interface Props { values?: () => any; } const User: Component = (props) => { const navigate = useNavigate(); const [progress, setProgress] = createSignal(-1); const credentials = adminCredentials(); const { database } = credentials || { database: null }; const isNew = () => !isFunction(props?.values); const getValues = () => { if (isNew()) { return { database, subscriptions: [] }; } return { subscriptions: [], ...props.values() }; }; const submitHandler = async (values: any, context: any) => { console.log({ caller: 'User / submitHandler', props, values, context, }); setProgress(0); const id = getValues()?._id ?? userId(values.username, values.database); await put(id, values, isNew()); const couchUserId = `org.couchdb.user:${values.username}`; const userDoc = { _id: couchUserId, name: values.username, password: values.password, type: 'user', roles: [], }; await put(couchUserId, userDoc, isNew(), '_users'); setProgress(1); const subscriptions = !!props?.values ? props.values().subscriptions ?? [] : []; const updatedSubscriptions = values.subscriptions ?? []; const isIn = (username: string, subs: any[]) => { for (let i = 0; i < subs.length; i++) { let sub = subs[i]; if (username === sub.username) { return true; } } return false; }; for (let i = 0; i < updatedSubscriptions.length; i++) { let subscription = updatedSubscriptions[i]; if (!isIn(subscription.username, subscriptions)) { console.log({ caller: 'User / submitHandler / new subscription', username: subscription.username, }); } } setProgress(2); for (let i = 0; i < subscriptions.length; i++) { let subscription = subscriptions[i]; if (!isIn(subscription.username, updatedSubscriptions)) { console.log({ caller: 'User / submitHandler / deleted subscription', username: subscription.username, }); } } setProgress(3); setInitialValues(values); setIsDirty(false); setProgress(-1); navigate(`/user/${id}`); }; const cancelHandler = () => { navigate(`/user/`); }; const deleteHandler = async () => { console.log({ caller: 'User / deleteHandler', id: getValues()?._id, props, }); await del(getValues()?._id); navigate(`/user/`); }; const validationHandler = async (values: any) => { let errors: any = {}; const credentials = adminCredentials(); if (!credentials) { return errors; } const { database, username, password } = credentials; try { const response = await fetch(database, { mode: 'cors' }); const dbStatus = await response.json(); if (!dbStatus.couchdb) { errors.database = 'The URL is not a couchdb instance'; } } catch (error) { errors.database = "Can't access the database"; } if (!!errors.database) { return errors; } const subscriptions = values.subscriptions ?? []; for (let i = 0; i < subscriptions.length; i++) { let subscription = subscriptions[i]; if ( !!subscription.username && !(await userExists(subscription.username)) ) { if (!errors.subscriptions) { errors.subscriptions = []; } errors.subscriptions[i] = { username: "User doesn't exist" }; } } if (isNew()) { if (await userExists(values.username)) { errors.username = 'The user is already existing'; } } console.log({ caller: 'Users / validationHandler', values, errors }); return errors; }; const { form, data, setData, setIsDirty, setInitialValues, reset, addField, unsetField, isDirty, isValid, } = createForm({ onSubmit: submitHandler, extend: reporter(), validate: validationHandler, initialValues: getValues(), }); const createUserHandler = async () => { console.log({ caller: 'User / createUserHandler', props, data: data(), }); // setOpenUserNamePasswordDialog(true); }; const subscriptions = () => data('subscriptions'); function removeSubscription(index: number) { return () => { unsetField(`subscriptions.${index}`); setIsDirty(true); }; } function addSubscription(index?: number) { return () => { addField(`subscriptions`, { username: '', direction: '' }, index); setIsDirty(true); }; } console.log({ caller: 'User ', props, values: getValues(), data: data(), }); return ( <> = 0}> `${value} of ${max} tasks completed` } class='progress' >
Processing...

{userId(data('username'), data('database'))}

Mail address Please provide a valid URL Database User name Password {(_, index) => ( )}
User name
Save Delete Create Back
); }; export default User;