\n {/* left part */}\n
\n \n
Author
\n \n {postHook.post.username}\n \n \n\n \n
\n
\n \n \n \n
\n
\n \n \n \n
\n
{postHook.upvote}\n
\n\n
\n \n \n \n
\n
\n \n\n {/* right part */}\n
\n {postHook.post.title}
\n \n {postHook.post.description}\n \n {postHook.post.content}
\n \n {/* Published {new Date(post.published_at).toLocaleString()} */}\n
\n \n Go back\n
\n\n {/* comment section */}\n Comments
\n \n \n contributed as{\" \"}\n \n Guest \n \n \n }\n multiline\n rows={4}\n placeholder=\"Write your thoughts here!\"\n variant=\"filled\"\n value={postHook.comment}\n onChange={postHook.handleChange}\n className={classes.comment}\n />\n \n \n
\n\n {/* existed comment */}\n \n {postHook.post.comments.map((com, index) => {\n const date = new Date(com.createdAt);\n return (\n
\n
\n contributed as{\" \"}\n \n Guest \n \n \n }\n multiline\n value={com.content}\n variant=\"filled\"\n classes={{\n root: classes.comment,\n }}\n disabled={true}\n />\n \n \n Comment at: {monthNames[date.getMonth()]} {date.getDate()},{\" \"}\n {date.getHours()}:{date.getMinutes()}\n \n
\n \n );\n })}\n
\n \n
\n );\n};\n\nexport default Post;\n","import React, { useEffect, useState } from \"react\";\n\nexport function usePost(postId) {\n const [post, setPost] = useState({});\n const [upvote, setUpvote] = useState(0);\n const [comment, setComment] = useState(\"\")\n const [comments, setComments] = useState([])\n\n const getPost = async () => {\n const resp = await fetch(\n `https://worker-api.minh160302.workers.dev/posts/${postId}`\n );\n const postResp = await resp.json();\n setPost(postResp);\n setUpvote(postResp.upvote);\n };\n\n // change upvote value\n const increment = async () => {\n setUpvote(upvote + 1);\n\n post.upvote = upvote + 1;\n await fetch(`https://worker-api.minh160302.workers.dev/posts`, {\n method: \"POST\",\n body: JSON.stringify(post),\n })\n .then((res) => {\n console.log(\"response: \", res);\n })\n .catch((err) => {\n console.log(\"error:\", err);\n });\n };\n\n const decrement = async () => {\n setUpvote(upvote - 1);\n post.upvote = upvote - 1;\n await fetch(`https://worker-api.minh160302.workers.dev/posts`, {\n method: \"POST\",\n body: JSON.stringify(post),\n })\n .then((res) => {\n console.log(\"response: \", res);\n })\n .catch((err) => {\n console.log(\"error:\", err);\n });\n };\n\n\n // comment\n const handleChange = (event) => {\n setComment(event.target.value);\n }\n\n const handleComment = async () => {\n const newComment = {\n content: comment,\n createdAt: Date.now()\n }\n\n post.comments.push(newComment);\n await fetch(`https://worker-api.minh160302.workers.dev/posts`, {\n method: \"POST\",\n body: JSON.stringify(post),\n })\n .then((res) => {\n setComments(post);\n setComment(\"\")\n })\n .then(() => {\n getPost();\n })\n .catch((err) => {\n console.log(\"error:\", err);\n });\n }\n\n const handleCancel = () => {\n setComment(\"\")\n }\n\n useEffect(() => {\n getPost();\n }, [postId]);\n\n return { post, increment, decrement, upvote, \n handleComment, comment, handleChange, handleCancel\n };\n}\n","import React from \"react\"\nimport { Router, RouteComponentProps, Link } from \"@reach/router\"\nimport Posts from './pages/posts'\nimport Post from './pages/post'\nimport { makeStyles } from \"@mui/styles\"\n\n\nconst useStyles = makeStyles({\n container: {\n margin: \"85px 15px 0px 15px\",\n }\n})\n\n\nfunction AppRoutes() {\n const classes = useStyles();\n\n return (\n