|
| 1 | +import CustomHeader from '@/common/components/CustomHeader'; |
| 2 | +import SafeArea from '@/common/components/SafeArea'; |
| 3 | +import { useLocation, useNavigate } from 'react-router'; |
| 4 | +import * as s from './style.css'; |
| 5 | +import Btn from '@/common/components/Button'; |
| 6 | +import { cx } from '@styled-system/css'; |
| 7 | +import { useState } from 'react'; |
| 8 | +import MultilineInputfield from '@/features/post/components/MultilineInputField'; |
| 9 | +import { usePostReport, type CategoryType } from '@/features/report/apis/usePostReport'; |
| 10 | + |
| 11 | +interface Category { |
| 12 | + key: CategoryType; |
| 13 | + label: string; |
| 14 | + checked: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +interface ReportPageState { |
| 18 | + reportedUserId: number; |
| 19 | + itemId: number; |
| 20 | + location: 'CHATROOM' | 'POST'; |
| 21 | +} |
| 22 | + |
| 23 | +const ReportPage = () => { |
| 24 | + const navigate = useNavigate(); |
| 25 | + const location = useLocation(); |
| 26 | + const state = location.state as ReportPageState; |
| 27 | + |
| 28 | + const ownerId = state.reportedUserId; |
| 29 | + const itemId = state.itemId; |
| 30 | + const { mutate: postReport } = usePostReport(); |
| 31 | + |
| 32 | + const initialCategories: Category[] = [ |
| 33 | + { key: 'OFFENSIVE', label: '부적절한 언어 및 비속어를 사용했어요.', checked: false }, |
| 34 | + { key: 'SEXUAL_CONTENT', label: '음란물 또는 성적인 콘텐츠를 포함하고 있어요.', checked: false }, |
| 35 | + { key: 'FAKE_ITEM', label: '허위 매물 및 정보를 포함하고 있어요.', checked: false }, |
| 36 | + { key: 'PRIVACY', label: '본인 또는 타인의 개인정보를 유출했어요.', checked: false }, |
| 37 | + { key: 'SPAM', label: '스팸 및 광고성 게시물이에요.', checked: false }, |
| 38 | + { key: 'OTHER', label: '기타(직접 입력)', checked: false }, |
| 39 | + ]; |
| 40 | + |
| 41 | + const [categories, setCategories] = useState<Category[]>(initialCategories); |
| 42 | + const [desc, setDesc] = useState<string>(''); |
| 43 | + const isOtherChecked = categories.find(c => c.key === 'OTHER')?.checked ?? false; |
| 44 | + const isOtherValid = isOtherChecked && desc.trim() !== ''; |
| 45 | + |
| 46 | + const isAnyChecked = (!isOtherChecked && categories.some(c => c.checked && c.key !== 'OTHER')) || isOtherValid; |
| 47 | + const btnMode: 'main' | 'default' | 'back' | 'disabled' = isAnyChecked ? 'main' : 'disabled'; |
| 48 | + |
| 49 | + const toggleCategory = (key: string) => { |
| 50 | + setCategories(prev => prev.map(cat => (cat.key === key ? { ...cat, checked: !cat.checked } : cat))); |
| 51 | + }; |
| 52 | + |
| 53 | + const report = () => { |
| 54 | + const selected = categories.filter(c => c.checked).map(c => c.key); |
| 55 | + |
| 56 | + postReport( |
| 57 | + { |
| 58 | + body: { |
| 59 | + categories: selected, |
| 60 | + description: desc, |
| 61 | + itemId: itemId, |
| 62 | + location: state.location, |
| 63 | + reportedUserId: ownerId, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + onSuccess: () => { |
| 68 | + navigate(-1); |
| 69 | + // 토스트 띄워주기 |
| 70 | + }, |
| 71 | + }, |
| 72 | + ); |
| 73 | + }; |
| 74 | + |
| 75 | + const handleDesc = (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 76 | + setDesc(e.target.value); |
| 77 | + }; |
| 78 | + |
| 79 | + const Content = ({ category }: { category: Category }) => { |
| 80 | + const { key, label, checked } = category; |
| 81 | + |
| 82 | + return ( |
| 83 | + <button className={s.SubTitle} onClick={() => toggleCategory(key)}> |
| 84 | + {label} |
| 85 | + {checked ? ( |
| 86 | + <div className={cx('mgc_checkbox_fill', s.Icon)} /> |
| 87 | + ) : ( |
| 88 | + <div className={s.CheckBoxContainer}> |
| 89 | + <div className={s.CheckBox} /> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + </button> |
| 93 | + ); |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <SafeArea> |
| 98 | + <CustomHeader onClick={() => navigate(-1)} title="신고하기" /> |
| 99 | + <div className={s.Wrapper}> |
| 100 | + <div className={s.Title}>사용자 신고 사유를 선택해 주세요.</div> |
| 101 | + <div className={s.Content}> |
| 102 | + {categories.map(cat => ( |
| 103 | + <Content key={cat.key} category={cat} /> |
| 104 | + ))} |
| 105 | + {isOtherChecked && ( |
| 106 | + <MultilineInputfield |
| 107 | + placeholder="신고 내용을 작성해 주세요." |
| 108 | + maxLength={500} |
| 109 | + value={desc} |
| 110 | + onChange={handleDesc} |
| 111 | + /> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <div className={s.Btn}> |
| 116 | + <Btn mode={btnMode} onClick={report}> |
| 117 | + 제출하기 |
| 118 | + </Btn> |
| 119 | + </div> |
| 120 | + </SafeArea> |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export default ReportPage; |
0 commit comments