Unverified Commit 33d96151 authored by Brody Dingel's avatar Brody Dingel Committed by GitHub

Convert `with-redis` example to TypeScript (#34720)

With the recent push to get the examples in TypeScript, I thought I'd convert this `with-redis` example over.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
parent 68e48a08
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
"uuid": "^8.3.2" "uuid": "^8.3.2"
}, },
"devDependencies": { "devDependencies": {
"@tailwindcss/jit": "0.1.1" "@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"
} }
} }
import type { AppProps } from 'next/app'
import '../styles/globals.css' import '../styles/globals.css'
import { Toaster } from 'react-hot-toast' import { Toaster } from 'react-hot-toast'
function MyApp({ Component, pageProps }) { function MyApp({ Component, pageProps }: AppProps) {
return ( return (
<> <>
<Component {...pageProps} /> <Component {...pageProps} />
......
import type { NextApiRequest, NextApiResponse } from 'next'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import redis from '../../lib/redis' import redis from '../../lib/redis'
export default async function create(req, res) { export default async function create(
req: NextApiRequest,
res: NextApiResponse
) {
const { title } = req.body const { title } = req.body
if (!title) { if (!title) {
......
import type { NextApiRequest, NextApiResponse } from 'next'
import redis from '../../lib/redis' import redis from '../../lib/redis'
export default async function getAllFeatures(req, res) { export default async function getAllFeatures(
req: NextApiRequest,
res: NextApiResponse
) {
const features = (await redis.hvals('features')) const features = (await redis.hvals('features'))
.map((entry) => JSON.parse(entry)) .map((entry) => JSON.parse(entry))
.sort((a, b) => b.score - a.score) .sort((a, b) => b.score - a.score)
......
import type { NextApiRequest, NextApiResponse } from 'next'
import redis from '../../lib/redis' import redis from '../../lib/redis'
export default async function subscribe(req, res) { export default async function subscribe(
req: NextApiRequest,
res: NextApiResponse
) {
const { email } = req.body const { email } = req.body
if (email && validateEmail(email)) { if (email && validateEmail(email)) {
...@@ -15,7 +20,7 @@ export default async function subscribe(req, res) { ...@@ -15,7 +20,7 @@ export default async function subscribe(req, res) {
} }
} }
function validateEmail(email) { function validateEmail(email: string) {
const re = 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,}))$/ /^(([^<>()[\]\\.,;:\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()) return re.test(String(email).toLowerCase())
......
import type { NextApiRequest, NextApiResponse } from 'next'
import redis from '../../lib/redis' import redis from '../../lib/redis'
export default async function upvote(req, res) { export default async function upvote(
req: NextApiRequest,
res: NextApiResponse
) {
const { title, id } = req.body const { title, id } = req.body
const ip = const ip =
req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA' req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA'
......
import { useState, useRef } from 'react' import { useState, useRef } from 'react'
import type { NextApiRequest } from 'next'
import type { MouseEvent } from 'react'
import Head from 'next/head' import Head from 'next/head'
import clsx from 'clsx' import clsx from 'clsx'
import useSWR, { mutate } from 'swr' import useSWR, { mutate } from 'swr'
import toast from 'react-hot-toast' import toast from 'react-hot-toast'
import redis from '../lib/redis' import redis from '../lib/redis'
const fetcher = (url) => fetch(url).then((res) => res.json()) type Feature = {
id: string
title: string
score: number
ip: string
}
const fetcher = (url: string) => fetch(url).then((res) => res.json())
function LoadingSpinner({ invert }) { function LoadingSpinner({ invert }: { invert?: boolean }) {
return ( return (
<svg <svg
className={clsx( className={clsx(
...@@ -35,8 +44,20 @@ function LoadingSpinner({ invert }) { ...@@ -35,8 +44,20 @@ function LoadingSpinner({ invert }) {
) )
} }
function Item({ isFirst, isLast, isReleased, hasVoted, feature }) { function Item({
const upvote = async (e) => { isFirst,
isLast,
isReleased,
hasVoted,
feature,
}: {
isFirst: boolean
isLast: boolean
isReleased: boolean
hasVoted: boolean
feature: Feature
}) {
const upvote = async (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault() e.preventDefault()
const res = await fetch('/api/vote', { const res = await fetch('/api/vote', {
...@@ -85,11 +106,17 @@ function Item({ isFirst, isLast, isReleased, hasVoted, feature }) { ...@@ -85,11 +106,17 @@ function Item({ isFirst, isLast, isReleased, hasVoted, feature }) {
) )
} }
export default function Roadmap({ features, ip }) { export default function Roadmap({
features,
ip,
}: {
features: Feature[]
ip: string
}) {
const [isCreateLoading, setCreateLoading] = useState(false) const [isCreateLoading, setCreateLoading] = useState(false)
const [isEmailLoading, setEmailLoading] = useState(false) const [isEmailLoading, setEmailLoading] = useState(false)
const featureInputRef = useRef(null) const featureInputRef = useRef<HTMLInputElement>(null)
const subscribeInputRef = useRef(null) const subscribeInputRef = useRef<HTMLInputElement>(null)
const { data, error } = useSWR('/api/features', fetcher, { const { data, error } = useSWR('/api/features', fetcher, {
initialData: { features }, initialData: { features },
...@@ -99,13 +126,13 @@ export default function Roadmap({ features, ip }) { ...@@ -99,13 +126,13 @@ export default function Roadmap({ features, ip }) {
toast.error(error) toast.error(error)
} }
const addFeature = async (e) => { const addFeature = async (e: MouseEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
setCreateLoading(true) setCreateLoading(true)
const res = await fetch('/api/create', { const res = await fetch('/api/create', {
body: JSON.stringify({ body: JSON.stringify({
title: featureInputRef.current.value, title: featureInputRef?.current?.value ?? '',
}), }),
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -122,16 +149,18 @@ export default function Roadmap({ features, ip }) { ...@@ -122,16 +149,18 @@ export default function Roadmap({ features, ip }) {
} }
mutate('/api/features') mutate('/api/features')
featureInputRef.current.value = '' if (featureInputRef.current) {
featureInputRef.current.value = ''
}
} }
const subscribe = async (e) => { const subscribe = async (e: MouseEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
setEmailLoading(true) setEmailLoading(true)
const res = await fetch('/api/subscribe', { const res = await fetch('/api/subscribe', {
body: JSON.stringify({ body: JSON.stringify({
email: subscribeInputRef.current.value, email: subscribeInputRef?.current?.value ?? '',
}), }),
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -147,7 +176,10 @@ export default function Roadmap({ features, ip }) { ...@@ -147,7 +176,10 @@ export default function Roadmap({ features, ip }) {
} }
toast.success('You are now subscribed to feature updates!') toast.success('You are now subscribed to feature updates!')
subscribeInputRef.current.value = ''
if (subscribeInputRef.current) {
subscribeInputRef.current.value = ''
}
} }
return ( return (
...@@ -189,7 +221,7 @@ export default function Roadmap({ features, ip }) { ...@@ -189,7 +221,7 @@ export default function Roadmap({ features, ip }) {
</form> </form>
</div> </div>
<div className="w-full"> <div className="w-full">
{data.features.map((feature, index) => ( {data.features.map((feature: Feature, index: number) => (
<Item <Item
key={index} key={index}
isFirst={index === 0} isFirst={index === 0}
...@@ -254,7 +286,7 @@ export default function Roadmap({ features, ip }) { ...@@ -254,7 +286,7 @@ export default function Roadmap({ features, ip }) {
) )
} }
export async function getServerSideProps({ req }) { export async function getServerSideProps({ req }: { req: NextApiRequest }) {
const ip = const ip =
req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA' req.headers['x-forwarded-for'] || req.headers['Remote_Addr'] || 'NA'
const features = (await redis.hvals('features')) const features = (await redis.hvals('features'))
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": ".",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
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