|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { FormProvider, useForm } from 'react-hook-form'; |
| 6 | +import { z } from 'zod'; |
| 7 | +import Button from '@/components/common/ui/button'; |
| 8 | +import FormField from '@/components/common/ui/form/form-field'; |
| 9 | +import { TextAreaInput } from '@/components/common/ui/input'; |
| 10 | +import { Modal } from '@/components/common/ui/modal'; |
| 11 | +import { GroupItems } from '@/components/common/ui/toggle'; |
| 12 | +import { |
| 13 | + useCreateEvaluation, |
| 14 | + useGetMissionEvaluationGrades, |
| 15 | +} from '@/hooks/queries/evaluation-api'; |
| 16 | +import { useToastStore } from '@/stores/use-toast-store'; |
| 17 | + |
| 18 | +const CreateEvaluationFormSchema = z.object({ |
| 19 | + gradeCode: z.string().min(1, '평가 등급을 선택해주세요.'), |
| 20 | + comment: z.string().min(1, '정성 코멘트를 입력해주세요.').max(1000), |
| 21 | +}); |
| 22 | + |
| 23 | +type CreateEvaluationFormValues = z.infer<typeof CreateEvaluationFormSchema>; |
| 24 | + |
| 25 | +interface CreateEvaluationModalProps { |
| 26 | + homeworkId: number; |
| 27 | +} |
| 28 | + |
| 29 | +export default function CreateEvaluationModal({ |
| 30 | + homeworkId, |
| 31 | +}: CreateEvaluationModalProps) { |
| 32 | + const [open, setOpen] = useState<boolean>(false); |
| 33 | + |
| 34 | + return ( |
| 35 | + <Modal.Root open={open} onOpenChange={setOpen}> |
| 36 | + <Modal.Trigger asChild> |
| 37 | + <Button size="medium" className="font-designer-16r w-fit"> |
| 38 | + 과제 평가하기 |
| 39 | + </Button> |
| 40 | + </Modal.Trigger> |
| 41 | + |
| 42 | + <Modal.Portal> |
| 43 | + <Modal.Overlay /> |
| 44 | + <Modal.Content className="w-[840px]"> |
| 45 | + <Modal.Header variant="form"> |
| 46 | + <Modal.Title className="font-designer-20b text-text-strong"> |
| 47 | + 평가하기 |
| 48 | + </Modal.Title> |
| 49 | + <Modal.CloseButton onClick={() => setOpen(false)} /> |
| 50 | + </Modal.Header> |
| 51 | + |
| 52 | + <CreateEvaluationForm |
| 53 | + homeworkId={homeworkId} |
| 54 | + onClose={() => setOpen(false)} |
| 55 | + /> |
| 56 | + </Modal.Content> |
| 57 | + </Modal.Portal> |
| 58 | + </Modal.Root> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +interface CreateEvaluationFormProps { |
| 63 | + homeworkId: number; |
| 64 | + onClose: () => void; |
| 65 | +} |
| 66 | + |
| 67 | +function CreateEvaluationForm({ |
| 68 | + homeworkId, |
| 69 | + onClose, |
| 70 | +}: CreateEvaluationFormProps) { |
| 71 | + const methods = useForm<CreateEvaluationFormValues>({ |
| 72 | + resolver: zodResolver(CreateEvaluationFormSchema), |
| 73 | + mode: 'onChange', |
| 74 | + defaultValues: { |
| 75 | + gradeCode: undefined, |
| 76 | + comment: '', |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const { handleSubmit, formState } = methods; |
| 81 | + |
| 82 | + const { data: grades } = useGetMissionEvaluationGrades(); |
| 83 | + const { mutate: createEvaluation } = useCreateEvaluation(); |
| 84 | + const showToast = useToastStore((state) => state.showToast); |
| 85 | + |
| 86 | + const onValidSubmit = (values: CreateEvaluationFormValues) => { |
| 87 | + createEvaluation( |
| 88 | + { |
| 89 | + homeworkId, |
| 90 | + request: values, |
| 91 | + }, |
| 92 | + { |
| 93 | + onSuccess: () => { |
| 94 | + showToast('평가가 성공적으로 제출되었습니다!'); |
| 95 | + onClose(); |
| 96 | + }, |
| 97 | + onError: () => { |
| 98 | + showToast('평가 제출에 실패했습니다. 다시 시도해주세요.', 'error'); |
| 99 | + }, |
| 100 | + }, |
| 101 | + ); |
| 102 | + }; |
| 103 | + |
| 104 | + const gradeOptions = (grades ?? []) |
| 105 | + .sort((a, b) => (a.orderNum ?? 0) - (b.orderNum ?? 0)) |
| 106 | + .map((grade) => ({ |
| 107 | + value: grade.code, |
| 108 | + label: `${grade.label} (${grade.score === 0 ? '0' : (grade.score?.toFixed(1) ?? '-')})`, |
| 109 | + })); |
| 110 | + |
| 111 | + return ( |
| 112 | + <FormProvider {...methods}> |
| 113 | + <Modal.Body variant="form"> |
| 114 | + <form |
| 115 | + id="create-evaluation" |
| 116 | + className="flex flex-col gap-300" |
| 117 | + onSubmit={handleSubmit(onValidSubmit)} |
| 118 | + > |
| 119 | + <FormField<CreateEvaluationFormValues, 'gradeCode'> |
| 120 | + name="gradeCode" |
| 121 | + label="평가 점수 선택" |
| 122 | + direction="vertical" |
| 123 | + required |
| 124 | + > |
| 125 | + <GroupItems |
| 126 | + variant="square" |
| 127 | + options={gradeOptions} |
| 128 | + multiple={false} |
| 129 | + allowDeselect={false} |
| 130 | + /> |
| 131 | + </FormField> |
| 132 | + |
| 133 | + <FormField<CreateEvaluationFormValues, 'comment'> |
| 134 | + name="comment" |
| 135 | + label="정성 코멘트" |
| 136 | + direction="vertical" |
| 137 | + required |
| 138 | + > |
| 139 | + <TextAreaInput |
| 140 | + id="comment" |
| 141 | + placeholder="정성 코멘트를 입력해 주세요." |
| 142 | + className="min-h-[230px]" |
| 143 | + maxLength={1000} |
| 144 | + /> |
| 145 | + </FormField> |
| 146 | + </form> |
| 147 | + </Modal.Body> |
| 148 | + |
| 149 | + <Modal.Footer variant="form"> |
| 150 | + <Modal.Close asChild> |
| 151 | + <Button color="secondary" size="large" onClick={onClose}> |
| 152 | + 취소 |
| 153 | + </Button> |
| 154 | + </Modal.Close> |
| 155 | + <Button |
| 156 | + color="primary" |
| 157 | + size="large" |
| 158 | + type="submit" |
| 159 | + form="create-evaluation" |
| 160 | + disabled={!formState.isValid || formState.isSubmitting} |
| 161 | + > |
| 162 | + 평가 완료 |
| 163 | + </Button> |
| 164 | + </Modal.Footer> |
| 165 | + </FormProvider> |
| 166 | + ); |
| 167 | +} |
0 commit comments