-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into 10-11-refactor-stats
- Loading branch information
Showing
53 changed files
with
442 additions
and
560 deletions.
There are no files selected for viewing
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
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,22 @@ | ||
'use server' | ||
|
||
import { revalidatePath } from 'next/cache' | ||
import { createEntry } from '@/lib/fauna' | ||
|
||
export async function createEntryAction(prevState: any, formData: FormData) { | ||
const name = formData.get('name') as string | ||
const message = formData.get('message') as string | ||
try { | ||
await createEntry(name, message) | ||
revalidatePath('/') | ||
return { | ||
successMessage: 'Thank you for signing the guest book', | ||
errorMessage: null, | ||
} | ||
} catch (error) { | ||
return { | ||
successMessage: null, | ||
errorMessage: 'Something went wrong. Please try again', | ||
} | ||
} | ||
} |
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,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; |
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,52 @@ | ||
import cn from 'classnames' | ||
import formatDate from 'date-fns/format' | ||
import EntryForm from '@/components/EntryForm' | ||
import { EntryType } from './page' | ||
|
||
const EntryItem = ({ entry }: { entry: EntryType }) => ( | ||
<div className="flex flex-col space-y-2"> | ||
<div className="prose dark:prose-dark w-full">{entry.message}</div> | ||
<div className="flex items-center space-x-3"> | ||
<p className="text-sm text-gray-500">{entry.name}</p> | ||
<span className="text-gray-200 dark:text-gray-800">/</span> | ||
<p className="text-sm text-gray-400 dark:text-gray-600"> | ||
{formatDate( | ||
new Date(entry.createdAt.isoString), | ||
"d MMM yyyy 'at' h:mm bb" | ||
)} | ||
</p> | ||
</div> | ||
</div> | ||
) | ||
|
||
export default async function GuestbookPage({ | ||
entries, | ||
}: { | ||
entries: EntryType[] | ||
}) { | ||
return ( | ||
<main className="max-w-4xl mx-auto p-4"> | ||
<div | ||
className={cn( | ||
'border border-blue-200 rounded p-6', | ||
'my-4 w-full dark:border-gray-800 bg-blue-50', | ||
'dark:bg-blue-opaque' | ||
)} | ||
> | ||
<h5 className={cn('text-lg md:text-xl font-bold', 'text-gray-900')}> | ||
Sign the Guestbook | ||
</h5> | ||
<p className="my-1 text-gray-800"> | ||
Share a message for a future visitor. | ||
</p> | ||
<EntryForm /> | ||
</div> | ||
|
||
<div className="mt-4 space-y-8 px-2"> | ||
{entries?.map((entry) => ( | ||
<EntryItem key={entry.id} entry={entry} /> | ||
))} | ||
</div> | ||
</main> | ||
) | ||
} |
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,21 @@ | ||
import './globals.css' | ||
|
||
export const metadata: { | ||
title: string | ||
description: string | ||
} = { | ||
title: 'Next.js + Fauna example', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
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,16 @@ | ||
import { getAllEntries } from '@/lib/fauna' | ||
import GuestbookPage from './guestbook-page' | ||
|
||
export type EntryType = { | ||
id: string | ||
name: string | ||
message: string | ||
createdAt: { | ||
isoString: string | ||
} | ||
} | ||
|
||
export default async function Page() { | ||
const entries = (await getAllEntries()) as EntryType[] | ||
return <GuestbookPage entries={entries} /> | ||
} |
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,65 @@ | ||
'use client' | ||
|
||
import cn from 'classnames' | ||
import { createEntryAction } from '@/actions/entry' | ||
// @ts-ignore | ||
import { experimental_useFormState as useFormState } from 'react-dom' | ||
import { experimental_useFormStatus as useFormStatus } from 'react-dom' | ||
import LoadingSpinner from '@/components/LoadingSpinner' | ||
import SuccessMessage from '@/components/SuccessMessage' | ||
import ErrorMessage from '@/components/ErrorMessage' | ||
|
||
const inputClasses = cn( | ||
'block py-2 bg-white dark:bg-gray-800', | ||
'rounded-md border-gray-300 focus:ring-blue-500', | ||
'focus:border-blue-500 text-gray-900 dark:text-gray-100' | ||
) | ||
|
||
const initialState = { | ||
successMessage: null, | ||
errorMessage: null, | ||
} | ||
|
||
export default function EntryForm() { | ||
const [state, formAction] = useFormState(createEntryAction, initialState) | ||
const { pending } = useFormStatus() | ||
|
||
return ( | ||
<> | ||
<form className="flex relative my-4" action={formAction}> | ||
<input | ||
required | ||
className={cn(inputClasses, 'w-1/3 mr-2 px-4')} | ||
aria-label="Your name" | ||
placeholder="Your name..." | ||
name="name" | ||
/> | ||
<input | ||
required | ||
className={cn(inputClasses, 'pl-4 pr-32 flex-grow')} | ||
aria-label="Your message" | ||
placeholder="Your message..." | ||
name="message" | ||
/> | ||
<button | ||
className={cn( | ||
'flex items-center justify-center', | ||
'absolute right-1 top-1 px-4 font-bold h-8', | ||
'bg-gray-100 dark:bg-gray-700 text-gray-900', | ||
'dark:text-gray-100 rounded w-28' | ||
)} | ||
type="submit" | ||
disabled={pending} | ||
> | ||
{pending ? <LoadingSpinner /> : 'Sign'} | ||
</button> | ||
</form> | ||
{state?.successMessage ? ( | ||
<SuccessMessage>{state.successMessage}</SuccessMessage> | ||
) : null} | ||
{state?.errorMessage ? ( | ||
<ErrorMessage>{state.errorMessage}</ErrorMessage> | ||
) : null} | ||
</> | ||
) | ||
} |
6 changes: 5 additions & 1 deletion
6
...les/with-fauna/components/ErrorMessage.js → ...es/with-fauna/components/ErrorMessage.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
File renamed without changes.
6 changes: 5 additions & 1 deletion
6
...s/with-fauna/components/SuccessMessage.js → .../with-fauna/components/SuccessMessage.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
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 was deleted.
Oops, something went wrong.
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'server-only' | ||
import { Client, fql, QuerySuccess, QueryValueObject } from 'fauna' | ||
|
||
const client = new Client({ | ||
secret: process.env.FAUNA_CLIENT_SECRET, | ||
}) | ||
|
||
export const getAllEntries = async () => { | ||
try { | ||
const dbresponse: QuerySuccess<QueryValueObject> = await client.query(fql` | ||
Entry.all() | ||
`) | ||
return dbresponse.data.data | ||
} catch (error: any) { | ||
throw new Error(error.message) | ||
} | ||
} | ||
|
||
export const createEntry = async (name: string, message: string) => { | ||
try { | ||
const dbresponse = await client.query(fql` | ||
Entry.create({ | ||
name: ${name}, | ||
message: ${message}, | ||
createdAt: Time.now(), | ||
})`) | ||
return dbresponse.data | ||
} catch (error: any) { | ||
throw new Error(error.message) | ||
} | ||
} |
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,7 @@ | ||
const nextConfig = { | ||
experimental: { | ||
serverActions: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
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
Oops, something went wrong.