2025-10-11 00:29:04 -07:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useSearchParams } from "react-router";
|
2025-10-10 23:04:50 -07:00
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
2025-10-11 00:29:04 -07:00
|
|
|
import { toast } from "sonner";
|
2025-10-10 23:04:50 -07:00
|
|
|
|
|
|
|
|
import type { Route } from "./+types/home";
|
|
|
|
|
import { Welcome } from "../welcome/welcome";
|
|
|
|
|
|
|
|
|
|
export function meta({}: Route.MetaArgs) {
|
|
|
|
|
return [
|
|
|
|
|
{ title: "New React Router App" },
|
|
|
|
|
{ name: "description", content: "Welcome to React Router!" },
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
|
|
|
const formData = await request.formData();
|
|
|
|
|
let name = formData.get("name");
|
|
|
|
|
let email = formData.get("email");
|
|
|
|
|
if (typeof name !== "string" || typeof email !== "string") {
|
|
|
|
|
return { guestBookError: "Name and email are required" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = name.trim();
|
|
|
|
|
email = email.trim();
|
|
|
|
|
if (!name || !email) {
|
|
|
|
|
return { guestBookError: "Name and email are required" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = database();
|
|
|
|
|
try {
|
|
|
|
|
await db.insert(schema.guestBook).values({ name, email });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return { guestBookError: "Error adding to guest book" };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function loader({ context }: Route.LoaderArgs) {
|
|
|
|
|
const db = database();
|
|
|
|
|
|
|
|
|
|
const guestBook = await db.query.guestBook.findMany({
|
|
|
|
|
columns: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
guestBook,
|
|
|
|
|
message: context.VALUE_FROM_EXPRESS,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Home({ actionData, loaderData }: Route.ComponentProps) {
|
2025-10-11 00:29:04 -07:00
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (searchParams.get("deleted") === "true") {
|
|
|
|
|
toast.success("League deleted successfully");
|
|
|
|
|
// Remove the query parameter
|
|
|
|
|
setSearchParams({});
|
|
|
|
|
}
|
|
|
|
|
}, [searchParams, setSearchParams]);
|
|
|
|
|
|
2025-10-10 23:04:50 -07:00
|
|
|
return (
|
|
|
|
|
<Welcome
|
|
|
|
|
guestBook={loaderData.guestBook}
|
|
|
|
|
guestBookError={actionData?.guestBookError}
|
|
|
|
|
message={loaderData.message}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|