[id].js
4.29 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
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';
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 () => {
const { id } = router.query
const response = await createComment(id, comment)
}
const displayComment = () => {
console.log(post.comments);
if (post.comments != undefined) {
return post.comments.map(comment => (
<div className="w-full">
<div className="w-full flex justify-between">
<div className="w-1/2">
<span className="text-gray-700">{comment.username}</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}`);
}
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">{post.explain}</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>
)
}