-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathnewSol.tsx
88 lines (84 loc) · 2.31 KB
/
newSol.tsx
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
import MainLayout from "@/lib/@components/MainLayout";
import useSols from "@/lib/@hooks/useSols";
import { Button, Flex, Text, Textarea } from "@chakra-ui/react";
import { useState } from "react";
import { PostSolReqBody } from "./api/sol";
export default function NewSol() {
const [solutionInfo, setSolutionInfo] = useState("");
const [solCode, setSolCode] = useState("");
const [isValid, setIsValid] = useState(false);
const { addSolMutaiton } = useSols();
const checkValid = () => {
try {
const info = JSON.parse(solutionInfo);
const valid =
info.probId &&
info.author &&
info.lang &&
info.createdAt &&
solCode !== "";
setIsValid(valid);
return valid;
} catch (e) {
setIsValid(false);
return false;
}
};
const handleSumbit = () => {
if (checkValid()) {
const info = JSON.parse(solutionInfo) as PostSolReqBody;
addSolMutaiton.mutate(
{
author: info.author,
probId: info.probId,
lang: info.lang,
createdAt: info.createdAt,
code: solCode
},
{
onSuccess: () => {
setSolCode("");
setSolutionInfo("");
}
}
);
}
};
return (
<MainLayout title="풀이 추가">
<Flex direction="column" gap="40px" px="40px" pt="40px">
<Flex w="full" justify="space-between">
<Text fontSize="3xl">풀이 추가</Text>
<Button
colorScheme="facebook"
isDisabled={!isValid}
onClick={() => handleSumbit()}
isLoading={addSolMutaiton.isLoading}
>
문제 추가
</Button>
</Flex>
<Text fontSize="xl">제출한 정답</Text>
<Textarea
placeholder="function()"
value={solCode}
onChange={e => {
setSolCode(e.target.value);
}}
onBlur={checkValid}
h="400px"
/>
<Text fontSize="xl">풀이 데이터</Text>
<Textarea
h="120px"
placeholder="probId, author, lang, createdAt in json format"
value={solutionInfo}
onChange={e => {
setSolutionInfo(e.target.value);
}}
onBlur={checkValid}
/>
</Flex>
</MainLayout>
);
}