From d64eca488f87ec04431a93e375e2eea13eea846b Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 22 Feb 2023 11:19:10 +0100 Subject: [PATCH] Navigation --- src/components/invitation/Invitation.tsx | 57 +++++++++++++++++----- src/components/invitations/Invitations.tsx | 7 ++- src/lib/db.ts | 8 +++ src/routes/invitations/[id].tsx | 36 ++++++++++++++ 4 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 src/lib/db.ts create mode 100644 src/routes/invitations/[id].tsx diff --git a/src/components/invitation/Invitation.tsx b/src/components/invitation/Invitation.tsx index 39bb62d..506b2f7 100644 --- a/src/components/invitation/Invitation.tsx +++ b/src/components/invitation/Invitation.tsx @@ -1,38 +1,65 @@ 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 './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'; -interface Props {} +interface Props { + values?: any; +} const Invitation: Component = (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 id = `invitation/${uuid()}`; - await db.put({ - _id: id, - type: 'invitation', - ...values, - date: new Date().toISOString(), + const id = props.values?.id ?? `invitation/${uuid()}`; + await put({ + db, + doc: { + _id: id, + date: new Date().toISOString(), + type: 'invitation', + ...values, + }, }); return id; }); - const submitHandler = (values: any, context: any) => { + const submitHandler = async (values: any, context: any) => { console.log({ caller: 'Invitation / submitHandler', props, values, 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 (
@@ -73,7 +100,11 @@ const Invitation: Component = (props) => { Please provide a valid password - Create + + Create}> + Save + + ); }; diff --git a/src/components/invitations/Invitations.tsx b/src/components/invitations/Invitations.tsx index 90a25ba..491fe2c 100644 --- a/src/components/invitations/Invitations.tsx +++ b/src/components/invitations/Invitations.tsx @@ -1,3 +1,4 @@ +import { A } from '@solidjs/router'; import PouchDb from 'pouchdb'; import { Component, createEffect, For } from 'solid-js'; import { createServerAction$ } from 'solid-start/server'; @@ -35,7 +36,11 @@ const Invitations: Component = (props) => { caller: 'Invitations / loop', invitations: invitation, }); - return
  • {invitation.doc.mail}
  • ; + return ( +
  • + {invitation.doc.mail} +
  • + ); }} diff --git a/src/lib/db.ts b/src/lib/db.ts new file mode 100644 index 0000000..5da1f9b --- /dev/null +++ b/src/lib/db.ts @@ -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); +}; diff --git a/src/routes/invitations/[id].tsx b/src/routes/invitations/[id].tsx new file mode 100644 index 0000000..ce136a9 --- /dev/null +++ b/src/routes/invitations/[id].tsx @@ -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 ( +
    +

    Invitation:

    + Loading...}> + + +
    + ); +};