Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed Jun 4, 2024
1 parent 9a0785c commit 51d7117
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 68 deletions.
4 changes: 1 addition & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
State Management Collection
<br />
<br />
<a href="https://juejin.cn/book/7311970169411567626?utm_source=course_list">
<img src="./website/static/img/juejin.jpg" alt="State Management Collection" width="200">
</a>
<img src="./website/static/img/juejin.jpg" alt="State Management Collection" width="200">
</h1>
</div>

Expand Down
50 changes: 25 additions & 25 deletions examples/react19/official-exp/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use server";
'use server'

import { revalidatePath } from "next/cache";
import postgres from "postgres";
import { z } from "zod";
import { revalidatePath } from 'next/cache'
import postgres from 'postgres'
import { z } from 'zod'

let sql = postgres(process.env.DATABASE_URL || process.env.POSTGRES_URL!, {
ssl: "allow",
});
ssl: 'allow',
})

// CREATE TABLE todos (
// id SERIAL PRIMARY KEY,
Expand All @@ -15,60 +15,60 @@ let sql = postgres(process.env.DATABASE_URL || process.env.POSTGRES_URL!, {

export async function createTodo(
prevState: {
message: string;
message: string
},
formData: FormData,
) {
const schema = z.object({
todo: z.string().min(1),
});
})
const parse = schema.safeParse({
todo: formData.get("todo"),
});
todo: formData.get('todo'),
})

if (!parse.success) {
return { message: "Failed to create todo" };
return { message: 'Failed to create todo' }
}

const data = parse.data;
const data = parse.data

try {
await sql`
INSERT INTO todos (text)
VALUES (${data.todo})
`;
`

revalidatePath("/");
return { message: `Added todo ${data.todo}` };
revalidatePath('/')
return { message: `Added todo ${data.todo}` }
} catch (e) {
return { message: "Failed to create todo" };
return { message: 'Failed to create todo' }
}
}

export async function deleteTodo(
prevState: {
message: string;
message: string
},
formData: FormData,
) {
const schema = z.object({
id: z.string().min(1),
todo: z.string().min(1),
});
})
const data = schema.parse({
id: formData.get("id"),
todo: formData.get("todo"),
});
id: formData.get('id'),
todo: formData.get('todo'),
})

try {
await sql`
DELETE FROM todos
WHERE id = ${data.id};
`;
`

revalidatePath("/");
return { message: `Deleted todo ${data.todo}` };
revalidatePath('/')
return { message: `Deleted todo ${data.todo}` }
} catch (e) {
return { message: "Failed to delete todo" };
return { message: 'Failed to delete todo' }
}
}
20 changes: 10 additions & 10 deletions examples/react19/official-exp/app/add-form.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
"use client";
'use client'

import { useActionState } from "react";
import { useFormStatus } from "react-dom";
import { createTodo } from "@/app/actions";
import { useActionState } from 'react'
import { useFormStatus } from 'react-dom'
import { createTodo } from '@/app/actions'

const initialState = {
message: "",
};
message: '',
}

function SubmitButton() {
const { pending } = useFormStatus();
const { pending } = useFormStatus()

return (
<button type="submit" aria-disabled={pending}>
Add
</button>
);
)
}

export function AddForm() {
const [state, formAction] = useActionState(createTodo, initialState);
const [state, formAction] = useActionState(createTodo, initialState)

return (
<form action={formAction}>
Expand All @@ -30,5 +30,5 @@ export function AddForm() {
{state?.message}
</p>
</form>
);
)
}
20 changes: 10 additions & 10 deletions examples/react19/official-exp/app/delete-form.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
"use client";
'use client'

import { useActionState } from "react";
import { useFormStatus } from "react-dom";
import { deleteTodo } from "@/app/actions";
import { useActionState } from 'react'
import { useFormStatus } from 'react-dom'
import { deleteTodo } from '@/app/actions'

const initialState = {
message: "",
};
message: '',
}

function DeleteButton() {
const { pending } = useFormStatus();
const { pending } = useFormStatus()

return (
<button type="submit" aria-disabled={pending}>
Delete
</button>
);
)
}

export function DeleteForm({ id, todo }: { id: number; todo: string }) {
const [state, formAction] = useActionState(deleteTodo, initialState);
const [state, formAction] = useActionState(deleteTodo, initialState)

return (
<form action={formAction}>
Expand All @@ -30,5 +30,5 @@ export function DeleteForm({ id, todo }: { id: number; todo: string }) {
{state?.message}
</p>
</form>
);
)
}
12 changes: 6 additions & 6 deletions examples/react19/official-exp/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import "./global.css";
import './global.css'

export const metadata = {
title: "Next.js Forms Example",
description: "Example application with forms and Postgres.",
};
title: 'Next.js Forms Example',
description: 'Example application with forms and Postgres.',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode;
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
)
}
14 changes: 7 additions & 7 deletions examples/react19/official-exp/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import postgres from "postgres";
import postgres from 'postgres'

import { AddForm } from "@/app/add-form";
import { DeleteForm } from "@/app/delete-form";
import { AddForm } from '@/app/add-form'
import { DeleteForm } from '@/app/delete-form'

let sql = postgres(process.env.DATABASE_URL || process.env.POSTGRES_URL!, {
ssl: "allow",
});
ssl: 'allow',
})

export default async function Home() {
let todos = await sql`SELECT * FROM todos`;
let todos = await sql`SELECT * FROM todos`

return (
<main>
Expand All @@ -23,5 +23,5 @@ export default async function Home() {
))}
</ul>
</main>
);
)
}
14 changes: 7 additions & 7 deletions examples/react19/server-actions/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
'./app/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
extend: {
backgroundImage: {
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
"gradient-conic":
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
},
},
},
plugins: [],
};
}

0 comments on commit 51d7117

Please sign in to comment.