Starting to implement subscriptions.

This commit is contained in:
Eric van der Vlist 2023-02-27 19:15:43 +01:00
parent 2c6107d761
commit 0dde189741
1 changed files with 75 additions and 18 deletions

View File

@ -1,8 +1,7 @@
import { createForm } from '@felte/solid'; import { createForm } from '@felte/solid';
import { Component, createEffect, Match, Switch } from 'solid-js'; import { Component, Index, Match, onMount, Switch } from 'solid-js';
import { TextField, Button } from '@kobalte/core'; import { TextField, Button } from '@kobalte/core';
import reporter from '@felte/reporter-tippy'; import reporter from '@felte/reporter-tippy';
import { v4 as uuid } from 'uuid';
import './style.css'; import './style.css';
import { useNavigate } from 'solid-start'; import { useNavigate } from 'solid-start';
import { adminCredentials } from '../credentials'; import { adminCredentials } from '../credentials';
@ -18,14 +17,17 @@ interface Props {
const User: Component<Props> = (props) => { const User: Component<Props> = (props) => {
const navigate = useNavigate(); const navigate = useNavigate();
const getValues = () => { const credentials = adminCredentials();
if (!isFunction(props?.values)) { const { database } = credentials || { database: null };
return null;
}
return props.values();
};
const isNew = () => !getValues(); const isNew = () => !isFunction(props?.values);
const getValues = () => {
if (isNew()) {
return { database, subscriptions: [] };
}
return { subscriptions: [], ...props.values() };
};
const submitHandler = async (values: any, context: any) => { const submitHandler = async (values: any, context: any) => {
console.log({ console.log({
@ -104,15 +106,13 @@ const User: Component<Props> = (props) => {
return errors; return errors;
}; };
const credentials = adminCredentials(); const { form, data, setData, setInitialValues, reset, addField, unsetField } =
const { database } = credentials || { database: null }; createForm({
onSubmit: submitHandler,
const { form, data, setData, setInitialValues, reset } = createForm({ extend: reporter(),
onSubmit: submitHandler, validate: validationHandler,
extend: reporter(), initialValues: getValues(),
validate: validationHandler, });
initialValues: getValues() || { database },
});
const createUserHandler = async () => { const createUserHandler = async () => {
console.log({ console.log({
@ -123,10 +123,21 @@ const User: Component<Props> = (props) => {
// setOpenUserNamePasswordDialog(true); // setOpenUserNamePasswordDialog(true);
}; };
const subscriptions = () => data('subscriptions');
function removeSubscription(index: number) {
return () => unsetField(`subscriptions.${index}`);
}
function addSubscription(index?: number) {
return () => addField(`subscriptions`, { user: '', direction: '' }, index);
}
console.log({ console.log({
caller: 'User ', caller: 'User ',
props, props,
values: getValues(), values: getValues(),
data: data(),
}); });
return ( return (
@ -173,6 +184,52 @@ const User: Component<Props> = (props) => {
autocomplete='off' autocomplete='off'
/> />
</TextField.Root> </TextField.Root>
<table>
<thead>
<tr>
<th>
<button type='button' onClick={addSubscription()}>
+
</button>
</th>
<th>User</th>
<th>Direction</th>
</tr>
</thead>
<tbody>
<Index each={subscriptions()}>
{(_, index) => (
<tr>
<td>
<button type='button' onClick={removeSubscription(index)}>
-
</button>
</td>
<td>
<TextField.Root>
<TextField.Input
type='text'
name={`subscriptions.${index}.user`}
required={true}
placeholder='user'
/>
</TextField.Root>
</td>
<td>
<TextField.Root>
<TextField.Input
type='text'
name={`subscriptions.${index}.direction`}
required={true}
placeholder='direction'
/>
</TextField.Root>
</td>
</tr>
)}
</Index>
</tbody>
</table>
<Switch> <Switch>
<Match when={!isNew()}> <Match when={!isNew()}>
<Button.Root type='submit'>Save</Button.Root> <Button.Root type='submit'>Save</Button.Root>