-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(example): reorganize examples, use Tailwind (#77)
- Loading branch information
Showing
36 changed files
with
363 additions
and
405 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
"root": true, | ||
"extends": "next/core-web-vitals" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/example-app/src/app/(examples)/client-form/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
|
||
import { ResultBox } from "@/app/_components/result-box"; | ||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { StyledInput } from "@/app/_components/styled-input"; | ||
import { useFormState } from "react-dom"; | ||
import { signupAction } from "./signup-action"; | ||
|
||
// Temporary implementation. | ||
export default function SignUpPage() { | ||
const [state, action] = useFormState(signupAction, { | ||
message: "Click on the signup button to see the result.", | ||
}); | ||
|
||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using client form</StyledHeading> | ||
<form action={action} className="flex flex-col mt-8 space-y-4"> | ||
<StyledInput type="text" name="email" placeholder="[email protected]" /> | ||
<StyledInput type="password" name="password" placeholder="••••••••" /> | ||
<StyledButton type="submit">Signup</StyledButton> | ||
</form> | ||
<ResultBox result={state} /> | ||
</main> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/example-app/src/app/(examples)/client-form/signup-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use server"; | ||
|
||
type PrevState = { | ||
message: string; | ||
}; | ||
|
||
// Temporary implementation. | ||
export const signupAction = (prevState: PrevState, formData: FormData) => { | ||
return { | ||
message: "Logged in successfully!", | ||
}; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
|
||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { StyledInput } from "@/app/_components/styled-input"; | ||
import { useState } from "react"; | ||
import { ResultBox } from "../../_components/result-box"; | ||
import { loginUser } from "./login-action"; | ||
|
||
export default function DirectExamplePage() { | ||
const [result, setResult] = useState<any>(undefined); | ||
|
||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using direct call</StyledHeading> | ||
<form | ||
className="flex flex-col mt-8 space-y-4" | ||
onSubmit={async (e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
const input = Object.fromEntries(formData) as { | ||
username: string; | ||
password: string; | ||
}; | ||
const res = await loginUser(input); // this is the typesafe action directly called | ||
setResult(res); | ||
}}> | ||
<StyledInput | ||
type="text" | ||
name="username" | ||
id="username" | ||
placeholder="Username" | ||
/> | ||
<StyledInput | ||
type="password" | ||
name="password" | ||
id="password" | ||
placeholder="Password" | ||
/> | ||
<StyledButton type="submit">Log in</StyledButton> | ||
</form> | ||
<ResultBox result={result} /> | ||
</main> | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/example-app/src/app/(examples)/hook/deleteuser-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use server"; | ||
|
||
import { ActionError, action } from "@/lib/safe-action"; | ||
import { z } from "zod"; | ||
|
||
const input = z.object({ | ||
userId: z.string().min(1).max(10), | ||
}); | ||
|
||
export const deleteUser = action(input, async ({ userId }) => { | ||
await new Promise((res) => setTimeout(res, 1000)); | ||
|
||
if (Math.random() > 0.5) { | ||
throw new ActionError("Could not delete user!"); | ||
} | ||
|
||
return { | ||
deletedUserId: userId, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ChevronLeft } from "lucide-react"; | ||
import Link from "next/link"; | ||
import { type ReactNode } from "react"; | ||
|
||
export default function ExamplesLayout({ children }: { children: ReactNode }) { | ||
return ( | ||
<div> | ||
<Link | ||
href="/" | ||
className="text-center flex items-center justify-center text-blue-600 dark:text-blue-400 hover:underline w-fit mx-auto"> | ||
<ChevronLeft className="w-6 h-6" /> | ||
<span className="text-lg font-semibold tracking-tight">Go back</span> | ||
</Link> | ||
<div className="mt-4">{children}</div> | ||
</div> | ||
); | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/example-app/src/app/(examples)/nested-schema/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use client"; | ||
|
||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { useAction } from "next-safe-action/hooks"; | ||
import { ResultBox } from "../../_components/result-box"; | ||
import { buyProduct } from "./shop-action"; | ||
|
||
export default function NestedSchemaPage() { | ||
const { execute, result, status } = useAction(buyProduct); | ||
|
||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using nested schema</StyledHeading> | ||
<form | ||
className="flex flex-col mt-8 space-y-4" | ||
onSubmit={async (e) => { | ||
e.preventDefault(); | ||
|
||
// Change one of these two to generate validation errors. | ||
const userId = crypto.randomUUID(); | ||
const productId = crypto.randomUUID(); | ||
|
||
execute({ | ||
user: { id: userId }, | ||
product: { deeplyNested: { id: productId } }, | ||
}); // this is the typesafe action called from client | ||
}}> | ||
<StyledButton type="submit">Buy product</StyledButton> | ||
</form> | ||
<ResultBox result={result} status={status} /> | ||
</main> | ||
); | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 5 additions & 10 deletions
15
...mple-app/src/app/optimistic-hook/page.tsx → ...c/app/(examples)/optimistic-hook/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import Link from "next/link"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { getLikes } from "./addlikes-action"; | ||
import AddLikeForm from "./addlikes-form"; | ||
|
||
export const metadata = { | ||
title: "Action using optimistic hook", | ||
}; | ||
|
||
export default function OptimisticHook() { | ||
const likesCount = getLikes(); | ||
return ( | ||
<> | ||
<Link href="/">Go to home</Link> | ||
<h1>Action using optimistic hook</h1> | ||
<pre style={{ marginTop: "1rem" }}> | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using optimistic hook</StyledHeading> | ||
<pre className="mt-4 text-center"> | ||
Server state: {JSON.stringify(likesCount)} | ||
</pre> | ||
{/* Pass the server state to Client Component */} | ||
<AddLikeForm likesCount={likesCount} /> | ||
</> | ||
</main> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/example-app/src/app/(examples)/server-form/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { StyledInput } from "@/app/_components/styled-input"; | ||
import { signup } from "./signup-action"; | ||
|
||
export default function SignUpPage() { | ||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using server form</StyledHeading> | ||
<form action={signup} className="flex flex-col mt-8 space-y-4"> | ||
<StyledInput type="text" name="email" placeholder="[email protected]" /> | ||
<StyledInput type="password" name="password" placeholder="••••••••" /> | ||
<StyledButton type="submit">Signup</StyledButton> | ||
</form> | ||
</main> | ||
); | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.