|
| 1 | +import { |
| 2 | + PostCongregationNewsRequest, |
| 3 | + PutCongregationNewsRequest, |
| 4 | +} from "@/types/congregationNews/request"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Input } from "@/components/ui/input"; |
| 7 | +import { |
| 8 | + Select, |
| 9 | + SelectContent, |
| 10 | + SelectItem, |
| 11 | + SelectTrigger, |
| 12 | + SelectValue, |
| 13 | +} from "@/components/ui/select"; |
| 14 | +import { usePostCongregationNews, usePutCongregationNews } from "@/query/congregationNews"; |
| 15 | +import { useRouter } from "next/router"; |
| 16 | +import { SubmitHandler, useForm } from "react-hook-form"; |
| 17 | +import { CONGREGATION_NEWS_TYPE_OPTIONS } from "./config"; |
| 18 | +import { cn } from "@/lib/utils"; |
| 19 | + |
| 20 | +interface CongregationNewsFormProps { |
| 21 | + initialData?: { |
| 22 | + _id: string; |
| 23 | + type: PostCongregationNewsRequest["type"]; |
| 24 | + description: string; |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +const CongregationNewsForm = ({ initialData }: CongregationNewsFormProps) => { |
| 29 | + const { push } = useRouter(); |
| 30 | + const { |
| 31 | + register, |
| 32 | + handleSubmit, |
| 33 | + watch, |
| 34 | + setValue, |
| 35 | + formState: { errors }, |
| 36 | + } = useForm<PostCongregationNewsRequest | PutCongregationNewsRequest>({ |
| 37 | + defaultValues: initialData |
| 38 | + ? { |
| 39 | + type: initialData.type, |
| 40 | + description: initialData.description, |
| 41 | + } |
| 42 | + : undefined, |
| 43 | + }); |
| 44 | + |
| 45 | + // Select validation을 위해 register 추가 |
| 46 | + register("type", { required: true }); |
| 47 | + |
| 48 | + const selectedType = watch("type"); |
| 49 | + const { mutate: postCongregationNews, isPending: isPostPending } = usePostCongregationNews(); |
| 50 | + const { mutate: putCongregationNews, isPending: isPutPending } = usePutCongregationNews(); |
| 51 | + |
| 52 | + const isPending = isPostPending || isPutPending; |
| 53 | + const isEditMode = !!initialData; |
| 54 | + |
| 55 | + const onSubmit: SubmitHandler<PostCongregationNewsRequest | PutCongregationNewsRequest> = ( |
| 56 | + data, |
| 57 | + ) => { |
| 58 | + if (!selectedType || !data.description) { |
| 59 | + return alert("모든 정보를 입력해주세요."); |
| 60 | + } |
| 61 | + |
| 62 | + if (isEditMode && initialData) { |
| 63 | + putCongregationNews( |
| 64 | + { id: initialData._id, request: data as PutCongregationNewsRequest }, |
| 65 | + { |
| 66 | + onSuccess: () => { |
| 67 | + push("/congregation-news"); |
| 68 | + }, |
| 69 | + }, |
| 70 | + ); |
| 71 | + } else { |
| 72 | + postCongregationNews(data as PostCongregationNewsRequest, { |
| 73 | + onSuccess: () => { |
| 74 | + push("/congregation-news"); |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <form className="flex flex-col gap-5" onSubmit={handleSubmit(onSubmit)}> |
| 82 | + <label className="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-5"> |
| 83 | + <p className="min-w-[100px] text-xl font-bold">타입</p> |
| 84 | + <Select |
| 85 | + value={selectedType} |
| 86 | + onValueChange={(value) => |
| 87 | + setValue("type", value as PostCongregationNewsRequest["type"], { shouldValidate: true }) |
| 88 | + } |
| 89 | + required |
| 90 | + > |
| 91 | + <SelectTrigger |
| 92 | + className={cn("w-[233px]", !selectedType && errors.type && "border-red-500")} |
| 93 | + > |
| 94 | + <SelectValue placeholder="타입을 선택하세요" /> |
| 95 | + </SelectTrigger> |
| 96 | + <SelectContent> |
| 97 | + {CONGREGATION_NEWS_TYPE_OPTIONS.map((option) => ( |
| 98 | + <SelectItem key={option.value} value={option.value}> |
| 99 | + {option.label} |
| 100 | + </SelectItem> |
| 101 | + ))} |
| 102 | + </SelectContent> |
| 103 | + </Select> |
| 104 | + </label> |
| 105 | + |
| 106 | + <label className="flex flex-col gap-2 sm:flex-row sm:items-start sm:gap-5"> |
| 107 | + <p className="min-w-[100px] pt-2 text-xl font-bold">내용</p> |
| 108 | + <textarea |
| 109 | + {...register("description", { required: true })} |
| 110 | + className={cn( |
| 111 | + "flex min-h-[120px] w-full rounded-md border border-zinc-200 bg-transparent px-3 py-2 text-sm shadow-sm transition-colors", |
| 112 | + "placeholder:text-zinc-500 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-zinc-950", |
| 113 | + "disabled:cursor-not-allowed disabled:opacity-50 dark:border-zinc-800 dark:placeholder:text-zinc-400 dark:focus-visible:ring-zinc-300", |
| 114 | + errors.description && "border-red-500", |
| 115 | + )} |
| 116 | + placeholder="내용을 입력하세요" |
| 117 | + /> |
| 118 | + </label> |
| 119 | + |
| 120 | + {errors.type && <p className="text-sm text-red-500">타입을 선택해주세요.</p>} |
| 121 | + {errors.description && <p className="text-sm text-red-500">내용을 입력해주세요.</p>} |
| 122 | + |
| 123 | + <div className="flex justify-end"> |
| 124 | + <Button variant="outline" type="submit" isLoading={isPending} disabled={isPending}> |
| 125 | + {isEditMode ? "수정하기" : "추가하기"} |
| 126 | + </Button> |
| 127 | + </div> |
| 128 | + </form> |
| 129 | + ); |
| 130 | +}; |
| 131 | + |
| 132 | +export default CongregationNewsForm; |
0 commit comments