Skip to content

Commit

Permalink
example fix url (#42695)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Tey <stevensteel97@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
3 people committed Nov 9, 2022
1 parent bb200b9 commit c1b76bd
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
7 changes: 5 additions & 2 deletions examples/blog-with-comment/components/comment/form.tsx
Expand Up @@ -34,8 +34,11 @@ export default function CommentForm({
<button className="py-2 px-4 rounded bg-blue-600 text-white disabled:opacity-40 hover:bg-blue-700">
Send
</button>
<button className="text-gray-500" onClick={() => logout()}>
Log out
<button
className="text-gray-500"
onClick={() => logout({ returnTo: window.location.origin })}
>
Log Out
</button>
</div>
) : (
Expand Down
19 changes: 4 additions & 15 deletions examples/blog-with-comment/hooks/useComment.ts
@@ -1,39 +1,28 @@
import type { Comment } from '../interfaces'
import React, { useState, useEffect } from 'react'
import React, { useState } from 'react'
import useSWR from 'swr'
import { useAuth0 } from '@auth0/auth0-react'

async function fetcher(url: string) {
const query = new URLSearchParams({ url })
const queryUrl = `${url}?${query.toString()}`

return fetch(queryUrl).then((res) => res.json())
}
const fetcher = (url) => fetch(url).then((res) => res.json())

export default function useComments() {
const { getAccessTokenSilently } = useAuth0()
const [text, setText] = useState('')
const [url, setUrl] = useState<string | null>(null)

const { data: comments, mutate } = useSWR<Comment[]>(
'/api/comment',
fetcher,
{ fallbackData: [] }
)

useEffect(() => {
const url = window.location.origin + window.location.pathname
setUrl(url)
}, [])

const onSubmit = async (e: React.FormEvent) => {
e.preventDefault()
const token = await getAccessTokenSilently()

try {
await fetch('/api/comment', {
method: 'POST',
body: JSON.stringify({ url, text }),
body: JSON.stringify({ text }),
headers: {
Authorization: token,
'Content-Type': 'application/json',
Expand All @@ -52,7 +41,7 @@ export default function useComments() {
try {
await fetch('/api/comment', {
method: 'DELETE',
body: JSON.stringify({ url, comment }),
body: JSON.stringify({ comment }),
headers: {
Authorization: token,
'Content-Type': 'application/json',
Expand Down
6 changes: 6 additions & 0 deletions examples/blog-with-comment/lib/clearUrl.ts
@@ -0,0 +1,6 @@
const clearUrl = (url: string) => {
const { origin, pathname } = new URL(url)
return `${origin}${pathname}`
}

export default clearUrl
6 changes: 4 additions & 2 deletions examples/blog-with-comment/lib/createComment.ts
Expand Up @@ -3,15 +3,17 @@ import type { Comment } from '../interfaces'
import redis from './redis'
import { nanoid } from 'nanoid'
import getUser from './getUser'
import clearUrl from './clearUrl'

export default async function createComments(
req: NextApiRequest,
res: NextApiResponse
) {
const { url, text } = req.body
const url = clearUrl(req.headers.referer)
const { text } = req.body
const { authorization } = req.headers

if (!url || !text || !authorization) {
if (!text || !authorization) {
return res.status(400).json({ message: 'Missing parameter.' })
}

Expand Down
6 changes: 4 additions & 2 deletions examples/blog-with-comment/lib/deleteComment.ts
Expand Up @@ -2,15 +2,17 @@ import type { NextApiRequest, NextApiResponse } from 'next'
import type { User, Comment } from '../interfaces'
import redis from './redis'
import getUser from './getUser'
import clearUrl from './clearUrl'

export default async function deleteComments(
req: NextApiRequest,
res: NextApiResponse
) {
const { url, comment }: { url: string; comment: Comment } = req.body
const url = clearUrl(req.headers.referer)
const { comment }: { url: string; comment: Comment } = req.body
const { authorization } = req.headers

if (!url || !comment || !authorization) {
if (!comment || !authorization) {
return res.status(400).json({ message: 'Missing parameter.' })
}

Expand Down
7 changes: 2 additions & 5 deletions examples/blog-with-comment/lib/fetchComment.ts
@@ -1,16 +1,13 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import type { Comment } from '../interfaces'
import redis from './redis'
import clearUrl from './clearUrl'

export default async function fetchComment(
req: NextApiRequest,
res: NextApiResponse
) {
const { url }: { url?: string } = req.query

if (!url) {
return res.status(400).json({ message: 'Missing parameter.' })
}
const url = clearUrl(req.headers.referer)

if (!redis) {
return res.status(500).json({ message: 'Failed to connect to redis.' })
Expand Down
2 changes: 1 addition & 1 deletion examples/blog-with-comment/package.json
Expand Up @@ -10,7 +10,7 @@
"@tailwindcss/typography": "^0.5.7",
"date-fns": "^2.29.3",
"gray-matter": "4.0.3",
"ioredis": "^5.2.3",
"ioredis": "^5.2.4",
"nanoid": "^4.0.0",
"next": "latest",
"react": "^18.2.0",
Expand Down
6 changes: 5 additions & 1 deletion examples/blog-with-comment/pages/_document.tsx
Expand Up @@ -3,7 +3,11 @@ import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<Head>
<meta charSet="utf-8" />
<meta name="robots" content="follow, index" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
</Head>
<body className="bg-white text-gray-700 antialiased">
<Main />
<NextScript />
Expand Down

0 comments on commit c1b76bd

Please sign in to comment.