-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-form.tsx
76 lines (66 loc) · 1.93 KB
/
post-form.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
'use client';
import { Button } from '@workspace/ui/components/button';
import { useRouter } from 'next/navigation';
import { useActionState } from 'react';
import { Post } from '@/entities/post';
import { createPostAction, updatePostAction } from '@/features/post/actions';
import { TextField } from '@/shared/ui';
interface PostFormProps {
post?: Post;
}
export function PostForm({ post }: PostFormProps) {
const router = useRouter();
const isEditMode = Boolean(post);
const [actionResult, formAction, isPending] = useActionState(
isEditMode ? updatePostAction : createPostAction,
null
);
return (
<form action={formAction} className="mx-8 flex flex-col">
<h1 className="mb-4 text-center text-xl font-bold">
{isEditMode ? '게시글 수정' : '새 게시글 작성'}
</h1>
{post?.id && <input type="hidden" name="postId" value={post.id} />}
<TextField
name="title"
label="제목"
required
disabled={isPending}
defaultValue={post?.title || ''}
/>
<TextField
name="content"
label="내용"
required
disabled={isPending}
isTextArea
defaultValue={post?.content || ''}
className="min-h-[30vh]"
/>
<TextField
name="author"
label="작성자"
required
disabled={isPending}
readOnly={!!post}
defaultValue={post?.author || ''}
/>
{actionResult?.status === false && (
<p className="mt-2 text-sm text-red-500">{actionResult.error}</p>
)}
<div className="flex justify-end gap-2">
<Button
type="button"
variant="outline"
onClick={() => router.back()}
disabled={isPending}
>
취소
</Button>
<Button type="submit" disabled={isPending}>
{isPending ? '저장 중...' : '작성하기'}
</Button>
</div>
</form>
);
}