Skip to content

Added client-side-actions page #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/app/client-side-actions/_components/create-post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client";

import { useState } from "react";
import { createPostAction } from "../actions";

export function CreatePost() {
const [name, setName] = useState("");

return (
<form
onSubmit={async (e) => {
e.preventDefault();
await createPostAction({ name }).then(() => {
setName("");
});
}}
className="flex flex-col gap-2"
>
<input
type="text"
placeholder="Title"
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full rounded-full px-4 py-2 text-black"
/>
<button
type="submit"
className="rounded-full bg-white/10 px-10 py-3 font-semibold transition hover:bg-white/20"
>
Submit
</button>
</form>
);
}
16 changes: 16 additions & 0 deletions src/app/client-side-actions/_components/delete-post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use client";

import { deletePostAction } from "../actions";

export const DeleteButton = (post: { id: number }) => {
return (
<button
className="border p-2 font-bold text-red-300"
onClick={async () => {
await deletePostAction(post);
}}
>
Delete
</button>
);
};
14 changes: 14 additions & 0 deletions src/app/client-side-actions/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use server";

import { revalidatePath } from "next/cache";
import { api } from "~/trpc/server";

export async function createPostAction(input: { name: string }) {
await api.post.create({ name: input.name });
revalidatePath("/client-side-action");
}

export async function deletePostAction(post: { id: number }) {
await api.post.delete({ id: post.id });
revalidatePath("/client-side-action");
}
41 changes: 41 additions & 0 deletions src/app/client-side-actions/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { api } from "~/trpc/server";
import type { inferAsyncReturnType } from "@trpc/server";
import { DeleteButton } from "./_components/delete-post";
import { CreatePost } from "./_components/create-post";

export default async function Home() {
const posts = await api.post.getPosts();

return (
<div className="container flex max-w-2xl flex-col gap-24 px-4 py-16">
<div className="flex flex-col text-xl">
<h1 className="text-2xl font-bold">Posts</h1>
{posts.map((post) => (
<PostView post={post} key={post.id} />
))}
</div>

<div className="flex flex-col gap-4">
<h1 className="text-2xl font-bold">Create a new post</h1>

{/* Note: CreatePost HAS to be imported, since it's using
client side JS */}
<CreatePost />
</div>
</div>
);
}

type PostType = NonNullable<
inferAsyncReturnType<typeof api.post.getPosts>
>[number];

function PostView({ post }: { post: PostType }) {
return (
<div className="flex justify-between p-2 hover:bg-gray-800/80">
{post.name}
{/* Note: DeleteButton HAS to be imported, since it's using client side JS */}
<DeleteButton id={post.id} />
</div>
);
}
6 changes: 6 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export default function Home() {
>
RSC + tRPC + Server Actions
</Link>
<Link
href="/client-side-actions"
className="text-xl text-blue-300 underline hover:text-blue-500"
>
RSC + tRPC + (Client side) Server Actions
</Link>
</div>
);
}