[id].js
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { useRouter } from "next/dist/client/router"
import { createComment, getPostbyId } from "../../api/post"
import "@uiw/react-textarea-code-editor/dist.css";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { Button } from "semantic-ui-react";
import { run } from "../../api/runner";
import { useSession } from "next-auth/client";
import SyntaxHighlighter from 'react-syntax-highlighter';
import { ThumbUpIcon } from "@heroicons/react/solid";
import { likePost } from "../../api/post";
const CodeEditor = dynamic(
() => import("@uiw/react-textarea-code-editor").then((mod) => mod.default),
{ ssr: false }
);
export default function Post() {
const router = useRouter()
const [post, setPost] = useState({})
useEffect(() => {
if (!router.isReady) return;
const { id } = router.query
getPostbyId(id).then(res => {
console.log(res);
setPost(res)
})
}, [router.isReady]);
const [value, setValue] = useState("c");
const [code, setCode] = useState("");
const [answer, setAnswer] = useState("Answer Test");
const [comment, setComment] = useState("");
const addComment = async () => {
console.log("comment is " + comment);
const { id } = router.query
await createComment(id, comment)
}
const displayComment = () => {
console.log(post.comments);
if (post.comments != undefined) {
return post.comments.map(comment => (
<div className="flex w-full border-carbon border-4 rounded-xl my-2">
<div className="w-full justify-between">
<div className=" mx-2">
<p className="text-black w-[90%]">{comment.content}</p>
<span className="text-black float-right">{`작성자 : ${comment.author.name}`}</span>
</div>
</div>
</div>
));
}
return <div></div>
}
const runCode = async function () {
if (useSession.accessToken == null) {
alert("You need to login first!")
router.push("/login")
return;
}
var result = await run(code, value);
console.log(result);
setAnswer(`출력 : ${result}`);
}
const like = async () => {
if (useSession.accessToken == null) {
alert("You need to login first!")
router.push("/login")
return;
}
const { id } = router.query
setPost(await likePost(id));
}
return (
<div className="ml-10 mr-10">
<h3 className="text-3xl font-bold">{post.title}</h3>
<p>{post.testinput}</p>
<p>{post.testoutput}</p>
<select value={value} onChange={(e) => setValue(e.target.value)}>
<option value="c">c</option>
<option value="cpp">cpp</option>
<option value="js">javascript</option>
<option value="ts">typescript</option>
<option value="go">golang</option>
<option value="py">python</option>
</select>
<span className="absolute left-1/2 text-2xl font-semibold">문제 설명</span>
<br></br>
<div className="w-6/12 inline-block">
<CodeEditor
value={code}
language={value}
placeholder="Please Enter the Code."
onChange={(e) => setCode(e.target.value)}
padding={15}
style={{
fontSize: 15,
backgroundColor: "#f5f5f5",
fontFamily:
"ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace"
}}
className="w-8/12 min-h-[24rem] rounded"
/>
</div>
<div className="w-6/12 inline-block align-top">
<div className="w-full">{post.explain}</div>
<Button className="bg-blue-500 rounded-full p-1 text-white " onClick={like}>
<ThumbUpIcon className="h-5 inline-block mb-1 "></ThumbUpIcon>
<span>{` Likes : ${post.likes}`}</span>
</Button>
</div>
<Button onClick={runCode} className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Run</Button>
<div className="">{answer}</div>
<details>
<summary>예시 코드 보기</summary>
<SyntaxHighlighter language="cpp">
{post.example || "Loading Example..."}
</SyntaxHighlighter>
</details>
<div>
<textarea className="w-full h-24" placeholder="Write your comment..." value={comment} onChange={(e) => setComment(e.target.value)}></textarea>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={addComment}>댓글 쓰기</button>
{displayComment()}
</div>
</div>
)
}