Files
remix-render/app/routes/new.tsx
Tommy Parnell 6e18c2c7ef init
2022-04-07 10:18:09 -04:00

50 lines
1.2 KiB
TypeScript

import type { ActionFunction } from "@remix-run/node";
import { redirect } from "@remix-run/node";
import { db } from "~/utils/db.server";
export const action: ActionFunction = async ({
request,
}) => {
const form = await request.formData();
const name = form.get("name");
const content = form.get("content");
// we do this type check to be extra sure and to make TypeScript happy
if (
typeof name !== "string" ||
typeof content !== "string"
) {
throw new Error(`Form not submitted correctly.`);
}
const fields = { name, content };
await db.note.create({ data: fields });
return redirect(`/`);
};
export default function NewNote() {
return (
<div>
<p>Add your own note</p>
<form method="post">
<div>
<label>
Name: <input type="text" name="name" />
</label>
</div>
<div>
<label>
Content: <textarea name="content" />
</label>
</div>
<div>
<button type="submit" className="button">
Add
</button>
</div>
</form>
</div>
);
}