Unverified Commit 7c7d981b authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

examples: progressive enhancement for Redis example (#59937)

The example had `useOptimistic` and wasn't structured so that the
browser could fall back to `action` if JS was still loading or disabled.
parent 247f9e83
...@@ -6,7 +6,6 @@ The example is a roadmap voting application where users can enter and vote for f ...@@ -6,7 +6,6 @@ The example is a roadmap voting application where users can enter and vote for f
- Users can add and upvote items (features in the roadmap) - Users can add and upvote items (features in the roadmap)
- Users can enter their email addresses to be notified about the released items. - Users can enter their email addresses to be notified about the released items.
- The API records the ip-addresses of the voters, so it does not allow multiple votes on the same item from the same IP address.
## Demo ## Demo
...@@ -14,10 +13,9 @@ The example is a roadmap voting application where users can enter and vote for f ...@@ -14,10 +13,9 @@ The example is a roadmap voting application where users can enter and vote for f
## Deploy Your Own ## Deploy Your Own
This examples uses [Upstash](https://upstash.com) (Serverless Redis Database) as its data storage. During deployment, This examples uses [Upstash](https://upstash.com) (Serverless Redis Database) as its data storage. During deployment. The integration will help you create a free Redis database and link it to your Vercel project automatically.
you will be asked to connect with Upstash. The integration will help you create a free Redis database and link it to your Vercel project automatically.
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-redis&project-name=redis-roadmap&repository-name=redis-roadmap&demo-title=Redis%20Roadmap&demo-description=Create%20and%20upvote%20features%20for%20your%20product.&demo-url=https%3A%2F%2Froadmap-redis.vercel.app%2F&integration-ids=oac_V3R1GIpkoJorr6fqyiwdhl17) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-redis&project-name=redis-roadmap&repository-name=redis-roadmap&demo-title=Redis%20Roadmap&demo-description=Create%20and%20upvote%20features%20for%20your%20product.&demo-url=https%3A%2F%2Froadmap-redis.vercel.app%2F&stores=%5B%7B"type"%3A"kv"%7D%5D&)
## How to use ## How to use
......
...@@ -4,11 +4,15 @@ import { kv } from '@vercel/kv' ...@@ -4,11 +4,15 @@ import { kv } from '@vercel/kv'
import { revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
import { Feature } from './types' import { Feature } from './types'
export async function saveFeature(feature: Feature) { export async function saveFeature(feature: Feature, formData: FormData) {
await kv.hset(`item:${feature.id}`, feature) let newFeature = {
...feature,
title: formData.get('feature') as string,
}
await kv.hset(`item:${newFeature.id}`, newFeature)
await kv.zadd('items_by_score', { await kv.zadd('items_by_score', {
score: Number(feature.score), score: Number(newFeature.score),
member: feature.id, member: newFeature.id,
}) })
revalidatePath('/') revalidatePath('/')
......
...@@ -23,16 +23,21 @@ function Item({ ...@@ -23,16 +23,21 @@ function Item({
pending: boolean pending: boolean
mutate: any mutate: any
}) { }) {
const upvoteWithId = upvote.bind(null, feature)
return ( return (
<form <form
action={async () => { action={upvoteWithId}
onSubmit={async (event) => {
event.preventDefault()
mutate({ mutate({
updatedFeature: { updatedFeature: {
...feature, ...feature,
score: (Number(feature.score) + 1).toString(), score: Number(feature.score) + 1,
}, },
pending: true, pending: true,
}) })
await upvote(feature) await upvote(feature)
}} }}
className={clsx( className={clsx(
...@@ -100,18 +105,27 @@ export default function FeatureForm({ features }: { features: Feature[] }) { ...@@ -100,18 +105,27 @@ export default function FeatureForm({ features }: { features: Feature[] }) {
return new Date(a.created_at).getTime() - new Date(b.created_at).getTime() return new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
}) })
let featureStub = {
id: uuidv4(),
title: '', // will used value from form
created_at: new Date().toISOString(),
score: '1',
}
let saveWithNewFeature = saveFeature.bind(null, featureStub)
return ( return (
<> <>
<div className="mx-8 w-full"> <div className="mx-8 w-full">
<form <form
className="relative my-8" className="relative my-8"
ref={formRef} ref={formRef}
action={async (formData) => { action={saveWithNewFeature}
const newFeature = { onSubmit={async (event) => {
id: uuidv4(), event.preventDefault()
title: `${formData.get('feature')}`, let formData = new FormData(event.currentTarget)
created_at: new Date().toISOString(), let newFeature = {
score: '1', ...featureStub,
title: formData.get('feature') as string,
} }
mutate({ mutate({
...@@ -119,7 +133,7 @@ export default function FeatureForm({ features }: { features: Feature[] }) { ...@@ -119,7 +133,7 @@ export default function FeatureForm({ features }: { features: Feature[] }) {
pending: true, pending: true,
}) })
formRef.current?.reset() formRef.current?.reset()
await saveFeature(newFeature) await saveFeature(newFeature, formData)
}} }}
> >
<input <input
......
import clsx from 'clsx'
export default function LoadingSpinner({ invert }: { invert?: boolean }) {
return (
<svg
className={clsx(
'animate-spin h-5 w-5 text-gray-900 dark:text-gray-100',
invert && 'text-gray-100 dark:text-gray-900'
)}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
)
}
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"autoprefixer": "10.4.16", "autoprefixer": "10.4.16",
"clsx": "^2.0.0", "clsx": "^2.0.0",
"geist": "^1.2.0", "geist": "^1.2.0",
"next": "latest", "next": "canary",
"postcss": "^8.4.32", "postcss": "^8.4.32",
"react": "18.2.0", "react": "18.2.0",
"react-dom": "18.2.0", "react-dom": "18.2.0",
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment