Download next.js/examples/blog-with-comment/hooks/useComment.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/blog-with-comment/hooks/useComment.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/blog-with-comment/hooks/useComment.ts
-
curl -L -o useComment.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/blog-with-comment/hooks/useComment.ts
1.52 kB
| import type { Comment } from "../interfaces"; | |
| import React, { useState } from "react"; | |
| import useSWR from "swr"; | |
| import { useAuth0 } from "@auth0/auth0-react"; | |
| const fetcher = (url) => | |
| fetch(url).then((res) => { | |
| if (res.ok) { | |
| return res.json(); | |
| } | |
| throw new Error(`${res.status} ${res.statusText} while fetching: ${url}`); | |
| }); | |
| export default function useComments() { | |
| const { getAccessTokenSilently } = useAuth0(); | |
| const [text, setText] = useState(""); | |
| const { data: comments, mutate } = useSWR<Comment[]>( | |
| "/api/comment", | |
| fetcher, | |
| { fallbackData: [] }, | |
| ); | |
| const onSubmit = async (e: React.FormEvent) => { | |
| e.preventDefault(); | |
| const token = await getAccessTokenSilently(); | |
| try { | |
| await fetch("/api/comment", { | |
| method: "POST", | |
| body: JSON.stringify({ text }), | |
| headers: { | |
| Authorization: token, | |
| "Content-Type": "application/json", | |
| }, | |
| }); | |
| setText(""); | |
| await mutate(); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| }; | |
| const onDelete = async (comment: Comment) => { | |
| const token = await getAccessTokenSilently(); | |
| try { | |
| await fetch("/api/comment", { | |
| method: "DELETE", | |
| body: JSON.stringify({ comment }), | |
| headers: { | |
| Authorization: token, | |
| "Content-Type": "application/json", | |
| }, | |
| }); | |
| await mutate(); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| }; | |
| return { text, setText, comments, onSubmit, onDelete }; | |
| } | |