From 616228c997b8aaf8a112271348715e674b99888f Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 1 Oct 2022 14:03:15 -0500 Subject: [PATCH] Styles! --- components/Button.tsx | 1 - components/Page.tsx | 11 ++++---- db-migrations.ts | 57 ++++++++++++++++++++++++++++++++++-------- fresh.gen.ts | 40 ++++++++++++++--------------- import_map.json | 2 ++ islands/Counter.tsx | 2 +- main.ts | 4 +-- routes/index.tsx | 4 +-- routes/note.tsx | 4 +-- routes/note/create.tsx | 6 ++--- routes/register.tsx | 33 ++++++++++++++++-------- twind.config.ts | 22 +++++++++++++--- 12 files changed, 124 insertions(+), 62 deletions(-) diff --git a/components/Button.tsx b/components/Button.tsx index 909d380..0d10b4b 100644 --- a/components/Button.tsx +++ b/components/Button.tsx @@ -6,7 +6,6 @@ export function Button(props: JSX.HTMLAttributes) { + {notes.map(({ id, content, created_at }) => (
diff --git a/routes/note/create.tsx b/routes/note/create.tsx index a8f5ae6..cd76d74 100644 --- a/routes/note/create.tsx +++ b/routes/note/create.tsx @@ -1,6 +1,6 @@ import { Handlers, PageProps } from "$fresh/server.ts"; -import { query } from "../../db.ts"; -import { Page } from "../../components/Page.tsx"; +import { query } from "@/db.ts"; +import { Page } from "@/components/Page.tsx"; type NoteID = string; @@ -8,7 +8,7 @@ export const handler: Handlers = { async POST(request, context) { const content = (await request.formData()).get("content"); if (!content) throw "no content provided"; - const result = await query( + const result = await query<{ id: string }>( "insert into note (content) values ($1) returning id", [content], ); diff --git a/routes/register.tsx b/routes/register.tsx index 1354103..c9982af 100644 --- a/routes/register.tsx +++ b/routes/register.tsx @@ -14,8 +14,12 @@ export const handler: Handlers = { const formData = (await request.formData()); const username = formData.get("username"); const password = formData.get("password"); - if (!username) throw "no username provided"; - if (!password) throw "no password provided"; + if (!username) { + return await context.render({ message: "no username provided" }); + } + if (!password) { + return await context.render({ message: "no password provided" }); + } const hashed_password = await hash(password.toString()); try { const result = await query<{ id: string }>( @@ -27,12 +31,15 @@ export const handler: Handlers = { const { rows: [{ id }] } = result; return await context.render(id); } catch (err) { - if (err instanceof PostgresError) { - console.error("PostgresError:", err); - return await context.render({ message: err.message }); - } else { - throw err; + if ( + err instanceof PostgresError && err.fields.code == "23505" && + err.fields.constraint == "user_username_key" + ) { + return await context.render({ + message: `A user with username '${username}' already exists`, + }); } + throw err; } }, }; @@ -62,14 +69,18 @@ function RegistrationForm(props?: RegistrationError | null) { return ( {props != null && -

{props.message}

} + ( +

+ Error: {props.message} +

+ )}
- + - + diff --git a/twind.config.ts b/twind.config.ts index 360eea8..a7a698d 100644 --- a/twind.config.ts +++ b/twind.config.ts @@ -1,10 +1,24 @@ import { Options } from "$fresh/plugins/twind.ts"; - import { apply } from "twind"; +import { css } from "twind/css"; + +const button = apply( + "bg-blue(400 500(hover:& focus:&) dark:(600 700(hover:& focus:&))) px-4 py-2 rounded cursor-pointer ring(focus:& current)", +); + +const input = apply( + "rounded bg-gray(200 300(hover:& focus:&) dark:(800 700(hover:& focus:&))) px-4 py-2 ring(focus:& current)", +); export default { selfURL: import.meta.url, - preflight: { - body: apply("bg-white text-black dark:(bg-gray-900 text-white)"), - }, + preflight: (preflight) => + css(preflight, { + body: apply("bg-white text-black dark:(bg-gray-900 text-white)"), + "body input, body textarea": input, + "body button, body input[type=submit]": button, + "body a": apply( + "text-blue(600 700(hover:&) dark:(400 300(hover:&))", + ), + }), } as Options;