diff --git a/app/routes/expenses/new.tsx b/app/routes/expenses/new.tsx
index 5296b37..7b54bfd 100644
--- a/app/routes/expenses/new.tsx
+++ b/app/routes/expenses/new.tsx
@@ -1,3 +1,4 @@
+import { User } from "@prisma/client";
import type { ActionFunction, LoaderFunction } from "remix";
import {
useActionData,
@@ -10,7 +11,7 @@ import {
useLoaderData,
} from "remix";
import { db } from "~/utils/db.server";
-import { requireUserId, getUserId } from "~/utils/session.server";
+import { requireUserId, getUser, getUserId } from "~/utils/session.server";
function validateExpenseDescription(description: string) {
if (description.length < 2) {
@@ -22,6 +23,8 @@ type ActionData = {
formError?: string;
fieldErrors?: {
description: string | undefined;
+ amount?: string | undefined;
+ user?: string | undefined;
};
fields?: {
description: string;
@@ -46,10 +49,15 @@ export const loader: LoaderFunction = async ({ request }) => {
export const action: ActionFunction = async ({ request }) => {
const userId = await requireUserId(request);
+ const user = await getUser(request);
const form = await request.formData();
const description = form.get("description");
- const amount = form.get("amount");
- if (typeof description !== "string" || typeof amount !== "number") {
+ const amount = parseInt(form.get("amount")?.toString() || "0", 10);
+ if (
+ typeof description !== "string" ||
+ typeof amount !== "number" ||
+ user === null
+ ) {
return badRequest({
formError: `Form not submitted correctly.`,
});
@@ -64,7 +72,7 @@ export const action: ActionFunction = async ({ request }) => {
}
const expense = await db.expense.create({
- data: { ...fields, userId: userId },
+ data: { ...fields, userId: userId, teamId: user.teamId },
});
return redirect(`/expenses/${expense.id}`);
};
@@ -93,53 +101,116 @@ export default function NewExpenseRoute() {
}
return (
-