Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
0fc1d9e9
Unverified
Commit
0fc1d9e9
authored
Dec 24, 2023
by
Lee Robinson
Committed by
GitHub
Dec 24, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: update Redis to App Router (#59311)
parent
d4b7c86e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
1396 additions
and
482 deletions
+1396
-482
.env.example
examples/with-redis/.env.example
+4
-0
.gitignore
examples/with-redis/.gitignore
+0
-1
actions.tsx
examples/with-redis/app/actions.tsx
+42
-0
favicon.ico
examples/with-redis/app/favicon.ico
+0
-0
form.tsx
examples/with-redis/app/form.tsx
+163
-0
globals.css
examples/with-redis/app/globals.css
+0
-0
layout.tsx
examples/with-redis/app/layout.tsx
+14
-0
page.tsx
examples/with-redis/app/page.tsx
+119
-0
spinner.tsx
examples/with-redis/app/spinner.tsx
+29
-0
types.tsx
examples/with-redis/app/types.tsx
+6
-0
redis.ts
examples/with-redis/lib/redis.ts
+0
-5
package.json
examples/with-redis/package.json
+15
-19
_app.tsx
examples/with-redis/pages/_app.tsx
+0
-26
create.ts
examples/with-redis/pages/api/create.ts
+0
-35
features.ts
examples/with-redis/pages/api/features.ts
+0
-14
subscribe.ts
examples/with-redis/pages/api/subscribe.ts
+0
-27
vote.ts
examples/with-redis/pages/api/vote.ts
+0
-29
index.tsx
examples/with-redis/pages/index.tsx
+0
-307
pnpm-lock.yaml
examples/with-redis/pnpm-lock.yaml
+976
-0
upstash.svg
examples/with-redis/public/upstash.svg
+0
-1
vercel.svg
examples/with-redis/public/vercel.svg
+0
-1
tailwind.config.js
examples/with-redis/tailwind.config.js
+0
-13
tailwind.config.ts
examples/with-redis/tailwind.config.ts
+16
-0
tsconfig.json
examples/with-redis/tsconfig.json
+12
-4
No files found.
examples/with-redis/.env.example
0 → 100644
View file @
0fc1d9e9
KV_URL=
KV_REST_API_URL=
KV_REST_API_TOKEN=
KV_REST_API_READ_ONLY_TOKEN=
\ No newline at end of file
examples/with-redis/.gitignore
View file @
0fc1d9e9
...
@@ -4,7 +4,6 @@
...
@@ -4,7 +4,6 @@
/node_modules
/node_modules
/.pnp
/.pnp
.pnp.js
.pnp.js
.yarn/install-state.gz
# testing
# testing
/coverage
/coverage
...
...
examples/with-redis/app/actions.tsx
0 → 100644
View file @
0fc1d9e9
'use server'
import
{
kv
}
from
'@vercel/kv'
import
{
revalidatePath
}
from
'next/cache'
import
{
Feature
}
from
'./types'
export
async
function
saveFeature
(
feature
:
Feature
)
{
await
kv
.
hset
(
`item:
${
feature
.
id
}
`
,
feature
)
await
kv
.
zadd
(
'items_by_score'
,
{
score
:
Number
(
feature
.
score
),
member
:
feature
.
id
,
})
revalidatePath
(
'/'
)
}
export
async
function
saveEmail
(
formData
:
FormData
)
{
const
email
=
formData
.
get
(
'email'
)
function
validateEmail
(
email
:
FormDataEntryValue
)
{
const
re
=
/^
(([^
<>()[
\]\\
.,;:
\s
@"
]
+
(\.[^
<>()[
\]\\
.,;:
\s
@"
]
+
)
*
)
|
(
".+"
))
@
((\[[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\])
|
(([
a-zA-Z
\-
0-9
]
+
\.)
+
[
a-zA-Z
]{2,}))
$/
return
re
.
test
(
String
(
email
).
toLowerCase
())
}
if
(
email
&&
validateEmail
(
email
))
{
await
kv
.
sadd
(
'emails'
,
email
)
revalidatePath
(
'/'
)
}
}
export
async
function
upvote
(
feature
:
Feature
)
{
const
newScore
=
Number
(
feature
.
score
)
+
1
await
kv
.
hset
(
`item:
${
feature
.
id
}
`
,
{
...
feature
,
score
:
newScore
,
})
await
kv
.
zadd
(
'items_by_score'
,
{
score
:
newScore
,
member
:
feature
.
id
})
revalidatePath
(
'/'
)
}
examples/with-redis/
public
/favicon.ico
→
examples/with-redis/
app
/favicon.ico
View file @
0fc1d9e9
File moved
examples/with-redis/app/form.tsx
0 → 100644
View file @
0fc1d9e9
'use client'
import
clsx
from
'clsx'
import
{
useOptimistic
,
useRef
}
from
'react'
import
{
saveFeature
,
upvote
}
from
'./actions'
import
{
v4
as
uuidv4
}
from
'uuid'
import
{
Feature
}
from
'./types'
function
Item
({
isFirst
,
isLast
,
isReleased
,
hasVoted
,
feature
,
pending
,
mutate
,
}:
{
isFirst
:
boolean
isLast
:
boolean
isReleased
:
boolean
hasVoted
:
boolean
feature
:
Feature
pending
:
boolean
mutate
:
any
})
{
return
(
<
form
action=
{
async
()
=>
{
mutate
({
updatedFeature
:
{
...
feature
,
score
:
(
Number
(
feature
.
score
)
+
1
).
toString
(),
},
pending
:
true
,
})
await
upvote
(
feature
)
}
}
className=
{
clsx
(
'p-6 mx-8 flex items-center border-t border-l border-r'
,
isFirst
&&
'rounded-t-md'
,
isLast
&&
'border-b rounded-b-md'
)
}
>
<
button
className=
{
clsx
(
'ring-1 ring-gray-200 rounded-full w-8 min-w-[2rem] h-8 mr-4 focus:outline-none focus:ring focus:ring-blue-300'
,
(
isReleased
||
hasVoted
)
&&
'bg-green-100 cursor-not-allowed ring-green-300'
,
pending
&&
'bg-gray-100 cursor-not-allowed'
)
}
disabled=
{
isReleased
||
hasVoted
||
pending
}
type=
"submit"
>
{
isReleased
?
'✅'
:
'👍'
}
</
button
>
<
h3
className=
"text font-semibold w-full text-left"
>
{
feature
.
title
}
</
h3
>
<
div
className=
"bg-gray-200 text-gray-700 text-sm rounded-xl px-2 ml-2"
>
{
feature
.
score
}
</
div
>
</
form
>
)
}
type
FeatureState
=
{
newFeature
:
Feature
updatedFeature
?:
Feature
pending
:
boolean
}
export
default
function
FeatureForm
({
features
}:
{
features
:
Feature
[]
})
{
const
formRef
=
useRef
<
HTMLFormElement
>
(
null
)
const
[
state
,
mutate
]
=
useOptimistic
(
{
features
,
pending
:
false
},
function
createReducer
(
state
,
newState
:
FeatureState
)
{
if
(
newState
.
newFeature
)
{
return
{
features
:
[...
state
.
features
,
newState
.
newFeature
],
pending
:
newState
.
pending
,
}
}
else
{
return
{
features
:
[
...
state
.
features
.
filter
(
(
f
)
=>
f
.
id
!==
newState
.
updatedFeature
!
.
id
),
newState
.
updatedFeature
,
]
as
Feature
[],
pending
:
newState
.
pending
,
}
}
}
)
let
sortedFeatures
=
state
.
features
.
sort
((
a
,
b
)
=>
{
// First, compare by score in descending order
if
(
Number
(
a
.
score
)
>
Number
(
b
.
score
))
return
-
1
if
(
Number
(
a
.
score
)
<
Number
(
b
.
score
))
return
1
// If scores are equal, then sort by created_at in ascending order
return
new
Date
(
a
.
created_at
).
getTime
()
-
new
Date
(
b
.
created_at
).
getTime
()
})
return
(
<>
<
div
className=
"mx-8 w-full"
>
<
form
className=
"relative my-8"
ref=
{
formRef
}
action=
{
async
(
formData
)
=>
{
const
newFeature
=
{
id
:
uuidv4
(),
title
:
`${formData.get('feature')}`
,
created_at
:
new
Date
().
toISOString
(),
score
:
'1'
,
}
mutate
({
newFeature
,
pending
:
true
,
})
formRef
.
current
?.
reset
()
await
saveFeature
(
newFeature
)
}
}
>
<
input
aria
-
label=
"Suggest a feature for our roadmap"
className=
"pl-3 pr-28 py-3 mt-1 text-lg block w-full border border-gray-200 rounded-md text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-blue-300"
maxLength=
{
150
}
placeholder=
"I want..."
required
type=
"text"
name=
"feature"
disabled=
{
state
.
pending
}
/>
<
button
className=
{
clsx
(
'flex items-center justify-center absolute right-2 top-2 px-4 h-10 text-lg border bg-black text-white rounded-md w-24 focus:outline-none focus:ring focus:ring-blue-300 focus:bg-gray-800'
,
state
.
pending
&&
'bg-gray-700 cursor-not-allowed'
)
}
type=
"submit"
disabled=
{
state
.
pending
}
>
Request
</
button
>
</
form
>
</
div
>
<
div
className=
"w-full"
>
{
sortedFeatures
.
map
((
feature
:
any
,
index
:
number
)
=>
(
<
Item
key=
{
feature
.
id
}
isFirst=
{
index
===
0
}
isLast=
{
index
===
sortedFeatures
.
length
-
1
}
isReleased=
{
false
}
hasVoted=
{
false
}
feature=
{
feature
}
pending=
{
state
.
pending
}
mutate=
{
mutate
}
/>
))
}
</
div
>
</>
)
}
examples/with-redis/
styles
/globals.css
→
examples/with-redis/
app
/globals.css
View file @
0fc1d9e9
File moved
examples/with-redis/app/layout.tsx
0 → 100644
View file @
0fc1d9e9
import
'./globals.css'
import
{
GeistSans
}
from
'geist/font/sans'
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
lang=
"en"
className=
{
GeistSans
.
variable
}
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
examples/with-redis/app/page.tsx
0 → 100644
View file @
0fc1d9e9
import
{
kv
}
from
'@vercel/kv'
import
{
saveEmail
}
from
'./actions'
import
FeatureForm
from
'./form'
import
{
Feature
}
from
'./types'
export
let
metadata
=
{
title
:
'Next.js and Redis Example'
,
description
:
'Feature roadmap example with Next.js with Redis.'
,
}
function
VercelLogo
(
props
:
React
.
SVGProps
<
SVGSVGElement
>
)
{
return
(
<
svg
aria
-
label=
"Vercel Logo"
xmlns=
"http://www.w3.org/2000/svg"
fill=
"none"
viewBox=
"0 0 24 19"
{
...
props
}
>
<
path
clipRule=
"evenodd"
d=
"M12.04 2L2.082 18H22L12.04 2z"
fill=
"#000"
fillRule=
"evenodd"
stroke=
"#000"
strokeWidth=
"1.5"
/>
</
svg
>
)
}
async
function
getFeatures
()
{
let
itemIds
=
await
kv
.
zrange
(
'items_by_score'
,
0
,
100
,
{
rev
:
true
,
})
if
(
!
itemIds
.
length
)
{
return
[]
}
let
multi
=
kv
.
multi
()
itemIds
.
forEach
((
id
)
=>
{
multi
.
hgetall
(
`item:
${
id
}
`
)
})
let
items
:
Feature
[]
=
await
multi
.
exec
()
return
items
.
map
((
item
)
=>
{
return
{
...
item
,
score
:
item
.
score
,
created_at
:
item
.
created_at
,
}
})
}
export
default
async
function
Page
()
{
let
features
=
await
getFeatures
()
return
(
<
div
className=
"flex flex-col items-center justify-center min-h-screen py-2"
>
<
main
className=
"flex flex-col items-center justify-center flex-1 px-4 sm:px-20 text-center"
>
<
div
className=
"flex justify-center items-center bg-black rounded-full w-16 sm:w-24 h-16 sm:h-24 my-8"
>
<
VercelLogo
className=
"h-8 sm:h-16 invert p-3 mb-1"
/>
</
div
>
<
h1
className=
"text-lg sm:text-2xl font-bold mb-2"
>
Help us prioritize our roadmap
</
h1
>
<
h2
className=
"text-md sm:text-xl mx-4"
>
Create or vote up features you want to see in our product.
</
h2
>
<
div
className=
"flex flex-wrap items-center justify-around max-w-4xl my-8 sm:w-full bg-white rounded-md shadow-xl h-full border border-gray-100"
>
<
FeatureForm
features=
{
features
}
/>
<
hr
className=
"border-1 border-gray-200 my-8 mx-8 w-full"
/>
<
div
className=
"mx-8 w-full"
>
<
p
className=
"flex text-gray-500"
>
Leave your email address here to be notified when feature requests
are released.
</
p
>
<
form
className=
"relative my-4"
action=
{
saveEmail
}
>
<
input
name=
"email"
aria
-
label=
"Email for updates"
placeholder=
"Email Address"
type=
"email"
autoComplete=
"email"
maxLength=
{
60
}
required
className=
"px-3 py-3 mt-1 text-lg block w-full border border-gray-200 rounded-md text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-blue-300"
/>
<
button
className=
"flex items-center justify-center absolute right-2 top-2 px-4 h-10 border border-gray-200 text-gray-900 rounded-md w-14 focus:outline-none focus:ring focus:ring-blue-300 focus:bg-gray-100"
type=
"submit"
>
OK
</
button
>
</
form
>
<
div
className=
"flex flex-col sm:flex-row justify-between items-center"
>
<
p
className=
"flex items-center my-8 w-full justify-center sm:justify-start"
>
Powered by
<
VercelLogo
className=
"h-5 mx-2"
/>
</
p
>
<
a
target=
"_blank"
rel=
"noopener noreferrer"
className=
"flex rounded focus:outline-none focus:ring focus:ring-blue-300 mb-4 sm:mb-0 min-w-max"
href=
{
`https://vercel.com/new/git/external?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&`
}
>
<
img
src=
"https://vercel.com/button"
alt=
"Vercel Deploy Button"
/>
</
a
>
</
div
>
</
div
>
</
div
>
</
main
>
</
div
>
)
}
examples/with-redis/app/spinner.tsx
0 → 100644
View file @
0fc1d9e9
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
>
)
}
examples/with-redis/app/types.tsx
0 → 100644
View file @
0fc1d9e9
export
type
Feature
=
{
id
:
string
title
:
string
score
:
string
created_at
:
string
}
examples/with-redis/lib/redis.ts
deleted
100644 → 0
View file @
d4b7c86e
import
Redis
from
'ioredis'
const
redis
=
new
Redis
(
process
.
env
.
REDIS_URL
)
export
default
redis
examples/with-redis/package.json
View file @
0fc1d9e9
{
{
"private"
:
true
,
"private"
:
true
,
"scripts"
:
{
"scripts"
:
{
"dev"
:
"next dev"
,
"dev"
:
"next dev
--turbo
"
,
"build"
:
"next build"
,
"build"
:
"next build"
,
"start"
:
"next start"
"start"
:
"next start"
},
},
"dependencies"
:
{
"dependencies"
:
{
"autoprefixer"
:
"^10.0.4"
,
"@types/node"
:
"20.10.3"
,
"clsx"
:
"^1.1.1"
,
"@types/react"
:
"18.2.42"
,
"ioredis"
:
"^4.23.0"
,
"@types/react-dom"
:
"18.2.17"
,
"@types/uuid"
:
"9.0.7"
,
"@vercel/kv"
:
"^1.0.0"
,
"autoprefixer"
:
"10.4.16"
,
"clsx"
:
"^2.0.0"
,
"geist"
:
"^1.2.0"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"postcss"
:
"^8.1.10"
,
"postcss"
:
"^8.4.32"
,
"react"
:
"^18.2.0"
,
"react"
:
"18.2.0"
,
"react-dom"
:
"^18.2.0"
,
"react-dom"
:
"18.2.0"
,
"react-hot-toast"
:
"^1.0.2"
,
"tailwindcss"
:
"3.3.6"
,
"swr"
:
"^2.0.0"
,
"typescript"
:
"5.3.2"
,
"tailwindcss"
:
"^2.0.2"
,
"uuid"
:
"^9.0.1"
"uuid"
:
"^8.3.2"
},
"devDependencies"
:
{
"@tailwindcss/jit"
:
"0.1.1"
,
"@types/ioredis"
:
"4.28.8"
,
"@types/node"
:
"^16.11.25"
,
"@types/react"
:
"^17.0.2"
,
"@types/uuid"
:
"8.3.4"
,
"typescript"
:
"^4.5.5"
}
}
}
}
examples/with-redis/pages/_app.tsx
deleted
100644 → 0
View file @
d4b7c86e
import
type
{
AppProps
}
from
'next/app'
import
'../styles/globals.css'
import
{
Toaster
}
from
'react-hot-toast'
function
MyApp
({
Component
,
pageProps
}:
AppProps
)
{
return
(
<>
<
Component
{
...
pageProps
}
/>
<
Toaster
position=
"bottom-right"
toastOptions=
{
{
style
:
{
margin
:
'40px'
,
background
:
'#363636'
,
color
:
'#fff'
,
zIndex
:
1
,
},
duration
:
5000
,
}
}
/>
</>
)
}
export
default
MyApp
examples/with-redis/pages/api/create.ts
deleted
100644 → 0
View file @
d4b7c86e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
{
v4
as
uuidv4
}
from
'uuid'
import
redis
from
'../../lib/redis'
export
default
async
function
create
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
const
{
title
}
=
req
.
body
if
(
!
title
)
{
res
.
status
(
400
).
json
({
error
:
'Feature can not be empty'
,
})
}
else
if
(
title
.
length
<
150
)
{
const
id
=
uuidv4
()
const
newEntry
=
{
id
,
title
,
created_at
:
Date
.
now
(),
score
:
1
,
ip
:
'NA'
,
}
await
redis
.
hset
(
'features'
,
id
,
JSON
.
stringify
(
newEntry
))
res
.
status
(
200
).
json
({
body
:
'success'
,
})
}
else
{
res
.
status
(
400
).
json
({
error
:
'Max 150 characters please.'
,
})
}
}
examples/with-redis/pages/api/features.ts
deleted
100644 → 0
View file @
d4b7c86e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
redis
from
'../../lib/redis'
export
default
async
function
getAllFeatures
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
const
features
=
(
await
redis
.
hvals
(
'features'
))
.
map
((
entry
)
=>
JSON
.
parse
(
entry
))
.
sort
((
a
,
b
)
=>
b
.
score
-
a
.
score
)
res
.
status
(
200
).
json
({
features
})
}
examples/with-redis/pages/api/subscribe.ts
deleted
100644 → 0
View file @
d4b7c86e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
redis
from
'../../lib/redis'
function
validateEmail
(
email
:
string
)
{
const
re
=
/^
(([^
<>()[
\]\\
.,;:
\s
@"
]
+
(\.[^
<>()[
\]\\
.,;:
\s
@"
]
+
)
*
)
|
(
".+"
))
@
((\[[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\])
|
(([
a-zA-Z
\-
0-9
]
+
\.)
+
[
a-zA-Z
]{2,}))
$/
return
re
.
test
(
String
(
email
).
toLowerCase
())
}
export
default
async
function
subscribe
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
const
{
email
}
=
req
.
body
if
(
email
&&
validateEmail
(
email
))
{
await
redis
.
sadd
(
'emails'
,
email
)
res
.
status
(
201
).
json
({
body
:
'success'
,
})
}
else
{
res
.
status
(
400
).
json
({
error
:
'Invalid email'
,
})
}
}
examples/with-redis/pages/api/vote.ts
deleted
100644 → 0
View file @
d4b7c86e
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
import
redis
from
'../../lib/redis'
export
default
async
function
upvote
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
const
{
title
,
id
}
=
req
.
body
const
ip
=
req
.
headers
[
'x-forwarded-for'
]
||
req
.
headers
[
'Remote_Addr'
]
||
'NA'
const
count
=
ip
===
'NA'
?
1
:
await
redis
.
sadd
(
's:'
+
title
,
ip
)
if
(
count
===
0
)
{
res
.
status
(
400
).
json
({
error
:
'You can not vote an item multiple times'
,
})
}
else
{
const
entry
=
JSON
.
parse
((
await
redis
.
hget
(
'features'
,
id
))
||
'null'
)
const
updated
=
{
...
entry
,
score
:
entry
.
score
+
1
,
ip
,
}
await
redis
.
hset
(
'features'
,
id
,
JSON
.
stringify
(
updated
))
return
res
.
status
(
201
).
json
(
updated
)
}
}
examples/with-redis/pages/index.tsx
deleted
100644 → 0
View file @
d4b7c86e
import
{
useState
,
useRef
}
from
'react'
import
type
{
NextApiRequest
}
from
'next'
import
type
{
MouseEvent
}
from
'react'
import
Head
from
'next/head'
import
clsx
from
'clsx'
import
useSWR
,
{
mutate
}
from
'swr'
import
toast
from
'react-hot-toast'
import
redis
from
'../lib/redis'
type
Feature
=
{
id
:
string
title
:
string
score
:
number
ip
:
string
}
const
fetcher
=
(
url
:
string
)
=>
fetch
(
url
).
then
((
res
)
=>
res
.
json
())
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
>
)
}
function
Item
({
isFirst
,
isLast
,
isReleased
,
hasVoted
,
feature
,
}:
{
isFirst
:
boolean
isLast
:
boolean
isReleased
:
boolean
hasVoted
:
boolean
feature
:
Feature
})
{
const
upvote
=
async
(
e
:
MouseEvent
<
HTMLButtonElement
>
)
=>
{
e
.
preventDefault
()
const
res
=
await
fetch
(
'/api/vote'
,
{
body
:
JSON
.
stringify
({
id
:
feature
.
id
,
title
:
feature
.
title
,
}),
headers
:
{
'Content-Type'
:
'application/json'
,
},
method
:
'POST'
,
})
const
{
error
}
=
await
res
.
json
()
if
(
error
)
{
return
toast
.
error
(
error
)
}
mutate
(
'/api/features'
)
}
return
(
<
div
className=
{
clsx
(
'p-6 mx-8 flex items-center border-t border-l border-r'
,
isFirst
&&
'rounded-t-md'
,
isLast
&&
'border-b rounded-b-md'
)
}
>
<
button
className=
{
clsx
(
'ring-1 ring-gray-200 rounded-full w-8 min-w-[2rem] h-8 mr-4 focus:outline-none focus:ring focus:ring-blue-300'
,
(
isReleased
||
hasVoted
)
&&
'bg-green-100 cursor-not-allowed ring-green-300'
)
}
disabled=
{
isReleased
||
hasVoted
}
onClick=
{
upvote
}
>
{
isReleased
?
'✅'
:
'👍'
}
</
button
>
<
h3
className=
"text font-semibold w-full text-left"
>
{
feature
.
title
}
</
h3
>
<
div
className=
"bg-gray-200 text-gray-700 text-sm rounded-xl px-2 ml-2"
>
{
feature
.
score
}
</
div
>
</
div
>
)
}
export
default
function
Roadmap
({
features
,
ip
,
}:
{
features
:
Feature
[]
ip
:
string
})
{
const
[
isCreateLoading
,
setCreateLoading
]
=
useState
(
false
)
const
[
isEmailLoading
,
setEmailLoading
]
=
useState
(
false
)
const
featureInputRef
=
useRef
<
HTMLInputElement
>
(
null
)
const
subscribeInputRef
=
useRef
<
HTMLInputElement
>
(
null
)
const
{
data
,
error
}
=
useSWR
(
'/api/features'
,
fetcher
,
{
initialData
:
{
features
},
})
if
(
error
)
{
toast
.
error
(
error
)
}
const
addFeature
=
async
(
e
:
MouseEvent
<
HTMLFormElement
>
)
=>
{
e
.
preventDefault
()
setCreateLoading
(
true
)
const
res
=
await
fetch
(
'/api/create'
,
{
body
:
JSON
.
stringify
({
title
:
featureInputRef
?.
current
?.
value
??
''
,
}),
headers
:
{
'Content-Type'
:
'application/json'
,
},
method
:
'POST'
,
})
const
{
error
}
=
await
res
.
json
()
setCreateLoading
(
false
)
if
(
error
)
{
toast
.
error
(
error
)
return
}
mutate
(
'/api/features'
)
if
(
featureInputRef
.
current
)
{
featureInputRef
.
current
.
value
=
''
}
}
const
subscribe
=
async
(
e
:
MouseEvent
<
HTMLFormElement
>
)
=>
{
e
.
preventDefault
()
setEmailLoading
(
true
)
const
res
=
await
fetch
(
'/api/subscribe'
,
{
body
:
JSON
.
stringify
({
email
:
subscribeInputRef
?.
current
?.
value
??
''
,
}),
headers
:
{
'Content-Type'
:
'application/json'
,
},
method
:
'POST'
,
})
const
{
error
}
=
await
res
.
json
()
setEmailLoading
(
false
)
if
(
error
)
{
return
toast
.
error
(
error
)
}
toast
.
success
(
'You are now subscribed to feature updates!'
)
if
(
subscribeInputRef
.
current
)
{
subscribeInputRef
.
current
.
value
=
''
}
}
return
(
<
div
className=
"flex flex-col items-center justify-center min-h-screen py-2"
>
<
Head
>
<
title
>
Roadmap – Next.js and Redis Example
</
title
>
<
link
rel=
"icon"
href=
"/favicon.ico"
/>
</
Head
>
<
main
className=
"flex flex-col items-center justify-center flex-1 px-4 sm:px-20 text-center"
>
<
div
className=
"flex justify-center items-center bg-black rounded-full w-16 sm:w-24 h-16 sm:h-24 my-8"
>
<
img
src=
"/upstash.svg"
alt=
"Upstash Logo"
className=
"h-8 sm:h-16"
/>
</
div
>
<
h1
className=
"text-lg sm:text-2xl font-bold mb-2"
>
Help us prioritize our roadmap
</
h1
>
<
h2
className=
"text-md sm:text-xl mx-4"
>
Create or vote up features you want to see in the next release.
</
h2
>
<
div
className=
"flex flex-wrap items-center justify-around max-w-4xl my-8 sm:w-full bg-white rounded-md shadow-xl h-full border border-gray-100"
>
<
div
className=
"mx-8 w-full"
>
<
form
className=
"relative my-8"
onSubmit=
{
addFeature
}
>
<
input
ref=
{
featureInputRef
}
aria
-
label=
"Suggest a feature for our roadmap"
placeholder=
"I want..."
type=
"text"
maxLength=
{
150
}
required
className=
"pl-3 pr-28 py-3 mt-1 text-lg block w-full border border-gray-200 rounded-md text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-blue-300"
/>
<
button
className=
"flex items-center justify-center absolute right-2 top-2 px-4 h-10 text-lg border bg-black text-white rounded-md w-24 focus:outline-none focus:ring focus:ring-blue-300 focus:bg-gray-800"
type=
"submit"
>
{
isCreateLoading
?
<
LoadingSpinner
invert
/>
:
'Request'
}
</
button
>
</
form
>
</
div
>
<
div
className=
"w-full"
>
{
data
.
features
.
map
((
feature
:
Feature
,
index
:
number
)
=>
(
<
Item
key=
{
index
}
isFirst=
{
index
===
0
}
isLast=
{
index
===
data
.
features
.
length
-
1
}
isReleased=
{
false
}
hasVoted=
{
feature
.
ip
===
ip
}
feature=
{
feature
}
/>
))
}
</
div
>
<
hr
className=
"border-1 border-gray-200 my-8 mx-8 w-full"
/>
<
div
className=
"mx-8 w-full"
>
<
p
className=
"flex text-gray-500"
>
Leave your email address here to be notified when feature requests
are released.
</
p
>
<
form
className=
"relative my-4"
onSubmit=
{
subscribe
}
>
<
input
ref=
{
subscribeInputRef
}
aria
-
label=
"Email for updates"
placeholder=
"Email Address"
type=
"email"
autoComplete=
"email"
maxLength=
{
60
}
required
className=
"px-3 py-3 mt-1 text-lg block w-full border border-gray-200 rounded-md text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-blue-300"
/>
<
button
className=
"flex items-center justify-center absolute right-2 top-2 px-4 h-10 border border-gray-200 text-gray-900 rounded-md w-14 focus:outline-none focus:ring focus:ring-blue-300 focus:bg-gray-100"
type=
"submit"
>
{
isEmailLoading
?
<
LoadingSpinner
/>
:
'OK'
}
</
button
>
</
form
>
<
div
className=
"flex flex-col sm:flex-row justify-between items-center"
>
<
p
className=
"flex items-center my-8 w-full justify-center sm:justify-start"
>
Powered by
<
img
src=
"/vercel.svg"
alt=
"Vercel Logo"
className=
"h-5 mx-2"
/>
and
<
img
src=
"/upstash.svg"
alt=
"Upstash Logo"
className=
"h-5 mx-2"
/>
</
p
>
<
a
target=
"_blank"
rel=
"noopener noreferrer"
className=
"flex rounded focus:outline-none focus:ring focus:ring-blue-300 mb-4 sm:mb-0 min-w-max"
href=
"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"
>
<
img
src=
"https://vercel.com/button"
alt=
"Vercel Deploy Button"
/>
</
a
>
</
div
>
</
div
>
</
div
>
</
main
>
</
div
>
)
}
export
async
function
getServerSideProps
({
req
}:
{
req
:
NextApiRequest
})
{
const
ip
=
req
.
headers
[
'x-forwarded-for'
]
||
req
.
headers
[
'Remote_Addr'
]
||
'NA'
const
features
=
(
await
redis
.
hvals
(
'features'
))
.
map
((
entry
)
=>
JSON
.
parse
(
entry
))
.
sort
((
a
,
b
)
=>
{
// Primary sort is score
if
(
a
.
score
>
b
.
score
)
return
-
1
if
(
a
.
score
<
b
.
score
)
return
1
// Secondary sort is title
if
(
a
.
title
>
b
.
title
)
return
1
if
(
a
.
title
<
b
.
title
)
return
-
1
return
1
})
return
{
props
:
{
features
,
ip
}
}
}
examples/with-redis/pnpm-lock.yaml
0 → 100644
View file @
0fc1d9e9
This diff is collapsed.
Click to expand it.
examples/with-redis/public/upstash.svg
deleted
100644 → 0
View file @
d4b7c86e
<svg
xmlns=
"http://www.w3.org/2000/svg"
fill=
"none"
viewBox=
"0 0 46 61"
><g
clip-path=
"url(#clip0)"
><path
fill=
"#00E9A3"
d=
"M.098 53.118c10.045 9.946 26.33 9.946 36.375 0 10.045-9.945 10.045-26.07 0-36.017l-4.546 4.503a18.968 18.968 0 010 27.012c-7.534 7.46-19.749 7.46-27.282 0L.098 53.118z"
/><path
fill=
"#00E9A3"
d=
"M9.192 44.114c5.022 4.973 13.165 4.973 18.188 0a12.645 12.645 0 000-18.008l-4.547 4.502a6.323 6.323 0 010 9.004c-2.512 2.487-6.583 2.487-9.094 0l-4.547 4.502zM45.567 8.097c-10.045-9.946-26.33-9.946-36.376 0-10.045 9.946-10.045 26.071 0 36.017l4.547-4.502a18.968 18.968 0 010-27.013c7.534-7.46 19.748-7.46 27.282 0l4.547-4.502z"
/><path
fill=
"#00E9A3"
d=
"M36.473 17.101c-5.022-4.972-13.165-4.972-18.188 0a12.645 12.645 0 000 18.009l4.547-4.502a6.323 6.323 0 010-9.005c2.512-2.486 6.583-2.486 9.094 0l4.547-4.502z"
/><path
fill=
"#fff"
fill-opacity=
".8"
d=
"M45.567 8.097c-10.045-9.946-26.33-9.946-36.376 0-10.045 9.946-10.045 26.071 0 36.017l4.547-4.502a18.968 18.968 0 010-27.013c7.534-7.46 19.748-7.46 27.282 0l4.547-4.502z"
/><path
fill=
"#fff"
fill-opacity=
".8"
d=
"M36.473 17.101c-5.022-4.972-13.165-4.972-18.188 0a12.645 12.645 0 000 18.009l4.547-4.502a6.323 6.323 0 010-9.005c2.512-2.486 6.583-2.486 9.094 0l4.547-4.502z"
/></g><defs><clipPath
id=
"clip0"
><path
fill=
"#fff"
d=
"M0 0h45.574v60.104H0z"
transform=
"translate(.043 .53)"
/></clipPath></defs></svg>
\ No newline at end of file
examples/with-redis/public/vercel.svg
deleted
100644 → 0
View file @
d4b7c86e
<svg
xmlns=
"http://www.w3.org/2000/svg"
fill=
"none"
viewBox=
"0 0 24 19"
><path
fill=
"#000"
fill-rule=
"evenodd"
stroke=
"#000"
stroke-width=
"1.5"
d=
"M12.04 2L2.082 18H22L12.04 2z"
clip-rule=
"evenodd"
/></svg>
\ No newline at end of file
examples/with-redis/tailwind.config.js
deleted
100644 → 0
View file @
d4b7c86e
/** @type {import('tailwindcss').Config} */
module
.
exports
=
{
mode
:
'jit'
,
purge
:
[
'./pages/**/*.{js,ts,jsx,tsx}'
,
'./components/**/*.{js,ts,jsx,tsx}'
],
darkMode
:
false
,
// or 'media' or 'class'
theme
:
{
extend
:
{},
},
variants
:
{
extend
:
{},
},
plugins
:
[],
}
examples/with-redis/tailwind.config.ts
0 → 100644
View file @
0fc1d9e9
import
type
{
Config
}
from
'tailwindcss'
export
default
{
content
:
[
'./app/**/*.{js,ts,jsx,tsx}'
,
'./pages/**/*.{js,ts,jsx,tsx}'
,
'./components/**/*.{js,ts,jsx,tsx}'
,
],
theme
:
{
extend
:
{},
},
future
:
{
hoverOnlyWhenSupported
:
true
,
},
plugins
:
[],
}
satisfies
Config
examples/with-redis/tsconfig.json
View file @
0fc1d9e9
...
@@ -2,20 +2,28 @@
...
@@ -2,20 +2,28 @@
"compilerOptions"
:
{
"compilerOptions"
:
{
"target"
:
"es5"
,
"target"
:
"es5"
,
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"baseUrl"
:
"."
,
"allowJs"
:
true
,
"allowJs"
:
true
,
"skipLibCheck"
:
true
,
"skipLibCheck"
:
true
,
"strict"
:
true
,
"strict"
:
true
,
"forceConsistentCasingInFileNames"
:
true
,
"forceConsistentCasingInFileNames"
:
true
,
"noEmit"
:
true
,
"noEmit"
:
true
,
"incremental"
:
true
,
"esModuleInterop"
:
true
,
"esModuleInterop"
:
true
,
"module"
:
"esnext"
,
"module"
:
"esnext"
,
"moduleResolution"
:
"node"
,
"moduleResolution"
:
"node"
,
"resolveJsonModule"
:
true
,
"resolveJsonModule"
:
true
,
"isolatedModules"
:
true
,
"isolatedModules"
:
true
,
"jsx"
:
"preserve"
"jsx"
:
"preserve"
,
"incremental"
:
true
,
"baseUrl"
:
"."
,
"plugins"
:
[
{
"name"
:
"next"
}
],
"paths"
:
{
"@/*"
:
[
"./*"
]
}
},
},
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
],
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
,
".next/types/**/*.ts"
],
"exclude"
:
[
"node_modules"
]
"exclude"
:
[
"node_modules"
]
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment