Skip to content

Commit f2cac0d

Browse files
feat: add createdAt
1 parent a850af4 commit f2cac0d

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

data/problems.json

+1-1
Large diffs are not rendered by default.

data/solutions.json

+1-1
Large diffs are not rendered by default.

src/lib/@hooks/useSols.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Sol } from "@/pages/api/sol";
1+
import { PostSolReqBody, Sol } from "@/pages/api/sol";
22
import { useToast } from "@chakra-ui/react";
33
import { QueryClient, useMutation, useQuery } from "@tanstack/react-query";
44
import axios from "axios";
@@ -24,7 +24,7 @@ export default function useSols() {
2424
});
2525

2626
const addSolMutaiton = useMutation({
27-
mutationFn: async (sol: Pick<Sol, "author" | "code" | "probId">) => {
27+
mutationFn: async (sol: PostSolReqBody) => {
2828
await axios.post<Sol[]>("/api/sol", sol, {
2929
headers: {
3030
"Content-Type": "application/json"

src/pages/api/sol.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async function handler(
1818
}
1919

2020
if (req.method === "POST") {
21-
const newSols: Pick<Sol, "author" | "code" | "probId" | "lang"> = req.body;
21+
const newSols: PostSolReqBody = req.body;
2222

2323
const sols = JSON.parse(
2424
await fs.readFile(`${jsonDirectory}/solutions.json`, "utf-8")
@@ -29,7 +29,7 @@ export default async function handler(
2929
author: newSols.author,
3030
code: newSols.code,
3131
probId: newSols.probId,
32-
createdAt: Date.now(),
32+
createdAt: +newSols.createdAt,
3333
lang: newSols.lang
3434
});
3535

@@ -72,3 +72,5 @@ export type Sol = {
7272
createdAt: ReturnType<typeof Date.now>;
7373
lang: "JavaScript" | "Python";
7474
};
75+
76+
export type PostSolReqBody = Omit<Sol, "id">;

src/pages/newSol.tsx

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MainLayout from "@/lib/@components/MainLayout";
22
import useSols from "@/lib/@hooks/useSols";
33
import { Button, Flex, Text, Textarea } from "@chakra-ui/react";
44
import { useState } from "react";
5+
import { PostSolReqBody } from "./api/sol";
56

67
export default function NewSol() {
78
const [solutionInfo, setSolutionInfo] = useState("");
@@ -12,7 +13,12 @@ export default function NewSol() {
1213
const checkValid = () => {
1314
try {
1415
const info = JSON.parse(solutionInfo);
15-
const valid = info.probId && info.author && solCode !== "";
16+
const valid =
17+
info.probId &&
18+
info.author &&
19+
info.lang &&
20+
info.createdAt &&
21+
solCode !== "";
1622
setIsValid(valid);
1723
return valid;
1824
} catch (e) {
@@ -23,11 +29,13 @@ export default function NewSol() {
2329

2430
const handleSumbit = () => {
2531
if (checkValid()) {
26-
const info = JSON.parse(solutionInfo);
32+
const info = JSON.parse(solutionInfo) as PostSolReqBody;
2733
addSolMutaiton.mutate(
2834
{
2935
author: info.author,
3036
probId: info.probId,
37+
lang: info.lang,
38+
createdAt: info.createdAt,
3139
code: solCode
3240
},
3341
{
@@ -54,17 +62,6 @@ export default function NewSol() {
5462
문제 추가
5563
</Button>
5664
</Flex>
57-
<Text fontSize="xl">풀이 데이터</Text>
58-
<Textarea
59-
h="120px"
60-
placeholder="probId, author in json format"
61-
value={solutionInfo}
62-
onChange={e => {
63-
setSolutionInfo(e.target.value);
64-
}}
65-
onBlur={checkValid}
66-
/>
67-
6865
<Text fontSize="xl">제출한 정답</Text>
6966
<Textarea
7067
placeholder="function()"
@@ -75,6 +72,16 @@ export default function NewSol() {
7572
onBlur={checkValid}
7673
h="400px"
7774
/>
75+
<Text fontSize="xl">풀이 데이터</Text>
76+
<Textarea
77+
h="120px"
78+
placeholder="probId, author, lang, createdAt in json format"
79+
value={solutionInfo}
80+
onChange={e => {
81+
setSolutionInfo(e.target.value);
82+
}}
83+
onBlur={checkValid}
84+
/>
7885
</Flex>
7986
</MainLayout>
8087
);

0 commit comments

Comments
 (0)