Navigation (for invitations)

This commit is contained in:
Eric van der Vlist 2023-02-22 11:58:56 +01:00
parent d64eca488f
commit ce873a72e5
3 changed files with 59 additions and 23 deletions

View File

@ -23,9 +23,10 @@ const Invitation: Component<Props> = (props) => {
props, props,
values, values,
args, args,
id: props.values?._id,
}); });
const db = new PouchDb('.db'); const db = new PouchDb('.db');
const id = props.values?.id ?? `invitation/${uuid()}`; const id = props.values?._id ?? `invitation/${uuid()}`;
await put({ await put({
db, db,
doc: { doc: {
@ -51,7 +52,20 @@ const Invitation: Component<Props> = (props) => {
} }
}; };
let { form } = createForm({ const cancelHandler = () => {
navigate(`/invitations/`);
};
const deleteHandler = async () => {
console.log({
caller: 'Invitation / deleteHandler',
props,
});
await save({ props, values: { _deleted: true } });
navigate(`/invitations/`);
};
const { form } = createForm({
onSubmit: submitHandler, onSubmit: submitHandler,
initialValues: props?.values, initialValues: props?.values,
}); });
@ -100,11 +114,17 @@ const Invitation: Component<Props> = (props) => {
Please provide a valid password Please provide a valid password
</TextField.ErrorMessage> </TextField.ErrorMessage>
</TextField.Root> </TextField.Root>
<Button.Root type='submit'> <Switch>
<Switch fallback={<>Create</>}> <Match when={!!props.values}>
<Match when={!!props.values}>Save</Match> <Button.Root type='submit'>Save</Button.Root>
</Switch> <Button.Root onclick={deleteHandler}>Delete</Button.Root>
</Button.Root> <Button.Root onclick={cancelHandler}>Cancel</Button.Root>
</Match>
<Match when={!props.values}>
<Button.Root type='submit'>Create</Button.Root>
<Button.Root onclick={cancelHandler}>Cancel</Button.Root>
</Match>
</Switch>
</form> </form>
); );
}; };

View File

@ -1,11 +1,15 @@
import { Button } from '@kobalte/core';
import { A } from '@solidjs/router'; import { A } from '@solidjs/router';
import PouchDb from 'pouchdb'; import PouchDb from 'pouchdb';
import { Component, createEffect, For } from 'solid-js'; import { Component, createEffect, For } from 'solid-js';
import { createServerAction$ } from 'solid-start/server'; import { createServerAction$ } from 'solid-start/server';
import { useNavigate } from 'solid-start';
interface Props {} interface Props {}
const Invitations: Component<Props> = (props) => { const Invitations: Component<Props> = (props) => {
const navigate = useNavigate();
const [invitations, getInvitations] = createServerAction$( const [invitations, getInvitations] = createServerAction$(
async (values: any) => { async (values: any) => {
const db = new PouchDb('.db'); const db = new PouchDb('.db');
@ -28,22 +32,31 @@ const Invitations: Component<Props> = (props) => {
}); });
}); });
const newHandler = () => {
navigate('/invitations/new');
};
return ( return (
<ul> <>
<For each={invitations.result}> <Button.Root onclick={newHandler}>New</Button.Root>
{(invitation: any) => { <ul>
console.log({ <For each={invitations.result}>
caller: 'Invitations / loop', {(invitation: any) => {
invitations: invitation, console.log({
}); caller: 'Invitations / loop',
return ( invitations: invitation,
<li> });
<A href={encodeURIComponent(invitation.id)}>{invitation.doc.mail}</A> return (
</li> <li>
); <A href={encodeURIComponent(invitation.id)}>
}} {invitation.doc.mail}
</For> </A>
</ul> </li>
);
}}
</For>
</ul>
</>
); );
}; };

View File

@ -3,6 +3,9 @@ export const put = async (params: { db: any; doc: any }) => {
try { try {
const previous = await db.get(doc._id); const previous = await db.get(doc._id);
doc._rev = previous._rev; doc._rev = previous._rev;
} catch (error) {} } catch (error) {
console.error({ caller: 'put', doc, error });
}
console.log({ caller: 'put', doc });
db.put(doc); db.put(doc);
}; };