1
1
import type { Comment } from '../interfaces'
2
- import React , { useState , useEffect } from 'react'
2
+ import React , { useState } from 'react'
3
3
import useSWR from 'swr'
4
4
import { useAuth0 } from '@auth0/auth0-react'
5
5
6
- async function fetcher ( url : string ) {
7
- const query = new URLSearchParams ( { url } )
8
- const queryUrl = `${ url } ?${ query . toString ( ) } `
9
-
10
- return fetch ( queryUrl ) . then ( ( res ) => res . json ( ) )
11
- }
6
+ const fetcher = ( url ) => fetch ( url ) . then ( ( res ) => res . json ( ) )
12
7
13
8
export default function useComments ( ) {
14
9
const { getAccessTokenSilently } = useAuth0 ( )
15
10
const [ text , setText ] = useState ( '' )
16
- const [ url , setUrl ] = useState < string | null > ( null )
17
11
18
12
const { data : comments , mutate } = useSWR < Comment [ ] > (
19
13
'/api/comment' ,
20
14
fetcher ,
21
15
{ fallbackData : [ ] }
22
16
)
23
17
24
- useEffect ( ( ) => {
25
- const url = window . location . origin + window . location . pathname
26
- setUrl ( url )
27
- } , [ ] )
28
-
29
18
const onSubmit = async ( e : React . FormEvent ) => {
30
19
e . preventDefault ( )
31
20
const token = await getAccessTokenSilently ( )
32
21
33
22
try {
34
23
await fetch ( '/api/comment' , {
35
24
method : 'POST' ,
36
- body : JSON . stringify ( { url , text } ) ,
25
+ body : JSON . stringify ( { text } ) ,
37
26
headers : {
38
27
Authorization : token ,
39
28
'Content-Type' : 'application/json' ,
@@ -52,7 +41,7 @@ export default function useComments() {
52
41
try {
53
42
await fetch ( '/api/comment' , {
54
43
method : 'DELETE' ,
55
- body : JSON . stringify ( { url , comment } ) ,
44
+ body : JSON . stringify ( { comment } ) ,
56
45
headers : {
57
46
Authorization : token ,
58
47
'Content-Type' : 'application/json' ,
0 commit comments