blob: 5fb8ba89eb235305f6f25e7efc9ed3beb81d0218 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
"use client";
import { useState } from "react";
import { api } from "~/trpc/react";
export function LatestPost() {
const [latestPost] = api.post.getLatest.useSuspenseQuery();
const trpcUtilities = api.useUtils();
const [name, setName] = useState("");
const createPost = api.post.create.useMutation({
onSuccess: async () => {
await trpcUtilities.post.invalidate();
setName("");
},
});
return (
<div className="w-full max-w-sm">
{latestPost ? (
<p className="truncate text-[#999999]">
your most recent post:{" "}
<span className="text-white">{latestPost.name}</span>
</p>
) : (
<p className="text-[#666666]">you have no posts yet.</p>
)}
<form
className="mt-3 flex flex-col gap-3"
onSubmit={(formSubmitEvent) => {
formSubmitEvent.preventDefault();
createPost.mutate({ name });
}}
>
<input
className="w-full border border-[#2a2a2a] bg-[#0f0f0f] px-3 py-2 text-white placeholder:text-[#666666] focus:border-[#666666]"
onChange={(inputChangeEvent) =>
setName(inputChangeEvent.target.value)
}
placeholder="title"
type="text"
value={name}
/>
<button
className="w-full border border-[#2a2a2a] bg-[#0f0f0f] px-4 py-2 text-white transition-colors duration-100 hover:border-[#666666] disabled:text-[#666666] disabled:hover:border-[#2a2a2a]"
disabled={createPost.isPending}
type="submit"
>
{createPost.isPending ? "submitting ..." : "submit"}
</button>
</form>
</div>
);
}
|