refactor: update

This commit is contained in:
Mauricio Siu 2024-07-13 00:00:51 -06:00
parent 7c049cf438
commit 71ab356168
2 changed files with 83 additions and 83 deletions

View File

@ -5,46 +5,46 @@ import { useState } from "react";
import { api } from "@/trpc/react"; import { api } from "@/trpc/react";
export function LatestPost() { export function LatestPost() {
const [latestPost] = api.post.getLatest.useSuspenseQuery(); const [latestPost] = api.post.getLatest.useSuspenseQuery();
const utils = api.useUtils(); const utils = api.useUtils();
const [name, setName] = useState(""); const [name, setName] = useState("");
const createPost = api.post.create.useMutation({ const createPost = api.post.create.useMutation({
onSuccess: async () => { onSuccess: async () => {
await utils.post.invalidate(); await utils.post.invalidate();
setName(""); setName("");
}, },
}); });
return ( return (
<div className="w-full max-w-xs"> <div className="w-full max-w-xs">
{latestPost ? ( {latestPost ? (
<p className="truncate">Your most recent post: {latestPost.name}</p> <p className="truncate">Your most recent post: {latestPost.name}</p>
) : ( ) : (
<p>You have no posts yet.</p> <p>You have no posts yet.</p>
)} )}
<form <form
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); e.preventDefault();
createPost.mutate({ name }); createPost.mutate({ name });
}} }}
className="flex flex-col gap-2" className="flex flex-col gap-2"
> >
<input <input
type="text" type="text"
placeholder="Title" placeholder="Title"
value={name} value={name}
onChange={(e) => setName(e.target.value)} onChange={(e) => setName(e.target.value)}
className="w-full rounded-full px-4 py-2 text-black" className="w-full rounded-full px-4 py-2 text-black"
/> />
<button <button
type="submit" type="submit"
className="rounded-full bg-white/10 px-10 py-3 font-semibold transition hover:bg-white/20" className="rounded-full bg-white/10 px-10 py-3 font-semibold transition hover:bg-white/20"
disabled={createPost.isPending} disabled={createPost.isPending}
> >
{createPost.isPending ? "Submitting..." : "Submit"} {createPost.isPending ? "Submitting..." : "Submit"}
</button> </button>
</form> </form>
</div> </div>
); );
} }

View File

@ -4,50 +4,50 @@ import { LatestPost } from "@/app/_components/post";
import { api, HydrateClient } from "@/trpc/server"; import { api, HydrateClient } from "@/trpc/server";
export default async function Home() { export default async function Home() {
const hello = await api.post.hello({ text: "from tRPC" }); const hello = await api.post.hello({ text: "from tRPC" });
void api.post.getLatest.prefetch(); // void api.post.getLatest.prefetch();
return ( return (
<HydrateClient> <HydrateClient>
<main className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#2e026d] to-[#15162c] text-white"> <main className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#2e026d] to-[#15162c] text-white">
<div className="container flex flex-col items-center justify-center gap-12 px-4 py-16"> <div className="container flex flex-col items-center justify-center gap-12 px-4 py-16">
<h1 className="text-5xl font-extrabold tracking-tight sm:text-[5rem]"> <h1 className="text-5xl font-extrabold tracking-tight sm:text-[5rem]">
Create <span className="text-[hsl(280,100%,70%)]">T3</span> App Create <span className="text-[hsl(280,100%,70%)]">T3</span> App
</h1> </h1>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:gap-8"> <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:gap-8">
<Link <Link
className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 hover:bg-white/20" className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 hover:bg-white/20"
href="https://create.t3.gg/en/usage/first-steps" href="https://create.t3.gg/en/usage/first-steps"
target="_blank" target="_blank"
> >
<h3 className="text-2xl font-bold">First Steps </h3> <h3 className="text-2xl font-bold">First Steps </h3>
<div className="text-lg"> <div className="text-lg">
Just the basics - Everything you need to know to set up your Just the basics - Everything you need to know to set up your
database and authentication. database and authentication.
</div> </div>
</Link> </Link>
<Link <Link
className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 hover:bg-white/20" className="flex max-w-xs flex-col gap-4 rounded-xl bg-white/10 p-4 hover:bg-white/20"
href="https://create.t3.gg/en/introduction" href="https://create.t3.gg/en/introduction"
target="_blank" target="_blank"
> >
<h3 className="text-2xl font-bold">Documentation </h3> <h3 className="text-2xl font-bold">Documentation </h3>
<div className="text-lg"> <div className="text-lg">
Learn more about Create T3 App, the libraries it uses, and how Learn more about Create T3 App, the libraries it uses, and how
to deploy it. to deploy it.
</div> </div>
</Link> </Link>
</div> </div>
<div className="flex flex-col items-center gap-2"> <div className="flex flex-col items-center gap-2">
<p className="text-2xl text-white"> <p className="text-2xl text-white">
{hello ? hello.greeting : "Loading tRPC query..."} {hello ? hello.greeting : "Loading tRPC query..."}
</p> </p>
</div> </div>
<LatestPost /> <LatestPost />
</div> </div>
</main> </main>
</HydrateClient> </HydrateClient>
); );
} }