|
1 | | -import { useState } from 'react'; |
| 1 | +import { useEffect, useState } from 'react'; |
2 | 2 | import { twMerge } from 'tailwind-merge'; |
3 | 3 |
|
| 4 | +import { postReports } from '@/apis/admin'; |
| 5 | + |
4 | 6 | import ConfirmModal from './ConfirmModal'; |
5 | 7 | import TextareaField from './TextareaField'; |
6 | 8 |
|
7 | 9 | interface ReportModalProps { |
| 10 | + reportType: ReportType; |
| 11 | + letterId: number | null; |
8 | 12 | onClose: () => void; |
9 | 13 | } |
10 | 14 |
|
11 | | -const REPORT_REASON = ['욕설', '비방', '폭언', '성희롱', '기타']; |
| 15 | +interface ReportReason { |
| 16 | + name: string; |
| 17 | + type: Reason; |
| 18 | +} |
| 19 | +const REPORT_REASON: ReportReason[] = [ |
| 20 | + { name: '욕설', type: 'ABUSE' }, |
| 21 | + { name: '비방', type: 'DEFAMATION' }, |
| 22 | + { name: '폭언', type: 'THREATS' }, |
| 23 | + { name: '성희롱', type: 'HARASSMENT' }, |
| 24 | + { name: '기타', type: 'ETC' }, |
| 25 | +]; |
12 | 26 |
|
13 | | -const ReportModal = ({ onClose }: ReportModalProps) => { |
14 | | - const [selected, setSelected] = useState(''); |
15 | | - const [additionalReason, setAdditionalReason] = useState(''); |
| 27 | +const ReportModal = ({ reportType, letterId, onClose }: ReportModalProps) => { |
| 28 | + const [postReportRequest, setPostReportRequest] = useState<PostReportRequest>({ |
| 29 | + reportType: reportType, |
| 30 | + reasonType: '', |
| 31 | + reason: '', |
| 32 | + letterId: letterId, |
| 33 | + }); |
16 | 34 |
|
17 | | - const handleReasonClick = (reason: string) => { |
18 | | - if (selected === reason) setSelected(''); |
19 | | - else setSelected(reason); |
| 35 | + const handleReasonClick = (reason: Reason) => { |
| 36 | + if (postReportRequest.reasonType === reason) |
| 37 | + setPostReportRequest((cur) => ({ ...cur, reasonType: '' })); |
| 38 | + else setPostReportRequest((cur) => ({ ...cur, reasonType: reason })); |
20 | 39 | }; |
21 | 40 |
|
22 | | - const handleSubmit = () => { |
23 | | - onClose(); |
| 41 | + const handleSubmit = async () => { |
| 42 | + const res = await postReports(postReportRequest); |
| 43 | + if (res?.status === 200) { |
| 44 | + alert('신고 처리되었습니다.'); |
| 45 | + onClose(); |
| 46 | + } else if (res?.status === 409) { |
| 47 | + alert('신고한 이력이 있습니다.'); |
| 48 | + onClose(); |
| 49 | + } |
24 | 50 | }; |
25 | 51 |
|
| 52 | + useEffect(() => { |
| 53 | + if (!postReportRequest.letterId) { |
| 54 | + alert('신고 모달을 여는 과정에서 오류가 발생했습니다. 새로고침을 눌러주세요'); |
| 55 | + onClose(); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
26 | 59 | return ( |
27 | 60 | <ConfirmModal |
28 | 61 | title="신고 사유를 선택해주세요" |
29 | 62 | description="신고한 게시물은 관리자 검토 후 처리됩니다." |
30 | 63 | cancelText="취소하기" |
31 | 64 | confirmText="제출하기" |
32 | | - confirmDisabled={selected === ''} |
| 65 | + confirmDisabled={postReportRequest.reasonType === ''} |
33 | 66 | onCancel={onClose} |
34 | 67 | onConfirm={handleSubmit} |
35 | 68 | > |
36 | 69 | <section className="my-6 flex flex-wrap gap-x-2.5 gap-y-2"> |
37 | | - {REPORT_REASON.map((reason) => ( |
| 70 | + {REPORT_REASON.map((reason, idx) => ( |
38 | 71 | <button |
| 72 | + key={idx} |
39 | 73 | type="button" |
40 | 74 | className={twMerge( |
41 | 75 | 'body-m rounded-full bg-white px-5 py-1.5 text-black', |
42 | | - selected === reason && 'bg-primary-2', |
| 76 | + postReportRequest.reasonType === reason.type && 'bg-primary-2', |
43 | 77 | )} |
44 | | - onClick={() => handleReasonClick(reason)} |
| 78 | + onClick={() => handleReasonClick(reason.type)} |
45 | 79 | > |
46 | | - {reason} |
| 80 | + {reason.name} |
47 | 81 | </button> |
48 | 82 | ))} |
49 | 83 | </section> |
50 | 84 | <TextareaField |
51 | 85 | rows={3} |
52 | 86 | placeholder="이곳을 눌러 추가 사유를 작성해주세요" |
53 | | - value={additionalReason} |
54 | | - onChange={(e) => setAdditionalReason(e.target.value)} |
| 87 | + value={postReportRequest.reason} |
| 88 | + onChange={(e) => setPostReportRequest((cur) => ({ ...cur, reason: e.target.value }))} |
55 | 89 | /> |
56 | 90 | </ConfirmModal> |
57 | 91 | ); |
|
0 commit comments