Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example fix url #42695

Merged
merged 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/blog-with-comment/components/comment/form.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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