create.tsx
2.67 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
import React from 'react';
import { useRouter } from 'next/router';
import { useMutation } from '@apollo/client';
import { CREATE_POST } from '@src/gql/create-post';
import { Form, Input } from 'antd';
import TextArea from 'antd/lib/input/TextArea';
import { CreateButtons, CreateInputs } from '@src/views/Create';
export default function CreatePage() {
const router = useRouter();
const [createPost] = useMutation(CREATE_POST);
const [form] = Form.useForm();
const [contents, setContents] = React.useState({
title: '',
description: '',
});
const handleChange = (e) => {
const {
target: { name, value },
} = e;
setContents({
...contents,
[name]: value,
});
};
const handleSubmit = async (e) => {
e.preventDefault();
const { title, description } = contents;
const {
query: { name },
} = router;
if (!(title && description)) {
alert('필수항목을 모두 입력해주세요');
return;
}
const { data } = await createPost({
variables: {
input: {
category: name,
content: description,
title,
},
},
});
router.push(`/${name}/article?num=${data.createPost.id}`);
};
const handleCancel = () => {
router.back();
};
return (
<div className={'outer-container create-container'}>
<div className={'form-container'}>
<Form form={form} layout={'vertical'}>
<CreateInputs
forms={[
{
form: {
label: '글제목',
tooltip: '게시글 제목은 필수항목입니다',
},
input: {
Item: Input,
value: 'title',
onChange: handleChange,
},
},
{
form: {
label: '글내용',
tooltip: '게시글의 내용은 필수항목입니다',
},
input: {
Item: TextArea,
value: 'description',
id: 'form-textarea',
onChange: handleChange,
},
},
]}
/>
<CreateButtons
buttons={[
{
title: 'Submit',
onClick: handleSubmit,
type: 'primary',
className: 'form-button submit-button',
},
{
title: 'Cancel',
onClick: handleCancel,
className: 'form-button cancel-button',
},
]}
/>
</Form>
</div>
</div>
);
}