Navigation

This commit is contained in:
Eric van der Vlist 2023-02-22 11:19:10 +01:00
parent a8c88d1303
commit d64eca488f
4 changed files with 94 additions and 14 deletions

View File

@ -1,38 +1,65 @@
import { createForm } from '@felte/solid'; import { createForm } from '@felte/solid';
import { Component, createUniqueId } from 'solid-js'; import { Component, createEffect, Match, Show, Switch } from 'solid-js';
import { TextField, Button } from '@kobalte/core'; import { TextField, Button } from '@kobalte/core';
import './style.css'; import './style.css';
import { createServerAction$ } from 'solid-start/server'; import { createServerAction$ } from 'solid-start/server';
import PouchDb from 'pouchdb'; import PouchDb from 'pouchdb';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import { put } from '~/lib/db';
import { useNavigate } from 'solid-start';
interface Props {} interface Props {
values?: any;
}
const Invitation: Component<Props> = (props) => { const Invitation: Component<Props> = (props) => {
const [saving, save] = createServerAction$(async (values: any) => { const navigate = useNavigate();
const [saving, save] = createServerAction$(async (args: any) => {
const { props, values } = args;
console.log({
caller: 'Invitation / save',
props,
values,
args,
});
const db = new PouchDb('.db'); const db = new PouchDb('.db');
const id = `invitation/${uuid()}`; const id = props.values?.id ?? `invitation/${uuid()}`;
await db.put({ await put({
db,
doc: {
_id: id, _id: id,
date: new Date().toISOString(),
type: 'invitation', type: 'invitation',
...values, ...values,
date: new Date().toISOString(), },
}); });
return id; return id;
}); });
const submitHandler = (values: any, context: any) => { const submitHandler = async (values: any, context: any) => {
console.log({ console.log({
caller: 'Invitation / submitHandler', caller: 'Invitation / submitHandler',
props, props,
values, values,
context, context,
}); });
save(values); const id = await save({ values, props });
if (!props.values) {
navigate(`/invitations/${encodeURIComponent(id)}`);
}
}; };
const { form } = createForm({ onSubmit: submitHandler }); let { form } = createForm({
onSubmit: submitHandler,
initialValues: props?.values,
});
console.log({
caller: 'Invitation ',
props,
});
return ( return (
<form use:form> <form use:form>
@ -73,7 +100,11 @@ 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'>Create</Button.Root> <Button.Root type='submit'>
<Switch fallback={<>Create</>}>
<Match when={!!props.values}>Save</Match>
</Switch>
</Button.Root>
</form> </form>
); );
}; };

View File

@ -1,3 +1,4 @@
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';
@ -35,7 +36,11 @@ const Invitations: Component<Props> = (props) => {
caller: 'Invitations / loop', caller: 'Invitations / loop',
invitations: invitation, invitations: invitation,
}); });
return <li>{invitation.doc.mail}</li>; return (
<li>
<A href={encodeURIComponent(invitation.id)}>{invitation.doc.mail}</A>
</li>
);
}} }}
</For> </For>
</ul> </ul>

8
src/lib/db.ts Normal file
View File

@ -0,0 +1,8 @@
export const put = async (params: { db: any; doc: any }) => {
const { db, doc } = params;
try {
const previous = await db.get(doc._id);
doc._rev = previous._rev;
} catch (error) {}
db.put(doc);
};

View File

@ -0,0 +1,36 @@
import { useParams } from 'solid-start';
import { createServerAction$ } from 'solid-start/server';
import PouchDb from 'pouchdb';
import Invitation from '~/components/invitation';
import { createEffect, Show } from 'solid-js';
export default () => {
const params = useParams();
const [invitation, getInvitation] = createServerAction$(
async (id: string) => {
const db = new PouchDb('.db');
const result = await db.get(id);
console.log({ caller: 'Invitations / serverAction', result });
return result;
}
);
getInvitation(decodeURIComponent(params.id));
createEffect(() => {
console.log({
caller: 'Invitations/[id]',
params,
invitation: invitation.result,
});
});
return (
<main>
<h1>Invitation: </h1>
<Show when={!invitation.pending} fallback={<>Loading...</>}>
<Invitation values={invitation.result} />
</Show>
</main>
);
};