From dc66eb40e0117db2849a5033fb0e99c19014980f Mon Sep 17 00:00:00 2001 From: hhjk00 Date: Mon, 30 Oct 2023 15:01:52 +0900 Subject: [PATCH] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20qna=20=EB=AC=B8?= =?UTF-8?q?=EC=9D=98=20=ED=8C=8C=EC=9D=BC=20=EB=93=B1=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cstmrService/inquiry/InquiryWrite.js | 38 +++++----- .../inquiry/UserInquiryContainer.js | 69 +++++++++++++++++-- .../cstmrService/inquiry/model/index.ts | 14 +++- 3 files changed, 90 insertions(+), 31 deletions(-) diff --git a/src/components/cstmrService/inquiry/InquiryWrite.js b/src/components/cstmrService/inquiry/InquiryWrite.js index 7294968..8123538 100644 --- a/src/components/cstmrService/inquiry/InquiryWrite.js +++ b/src/components/cstmrService/inquiry/InquiryWrite.js @@ -26,20 +26,11 @@ function InquiryWrite({ handlerSubmitInquiryForm, memberName, inquiryForm, - handlerDeleteConfirmInquiry + handlerDeleteConfirmInquiry, + fileInputRef, + handlerFileChange, + handlerFileClear }) { - const fileInputRef = useRef(null); - const [selectedFile, setSelectedFile] = useState(null); - - const handleFileChange = event => { - const file = event.target.files[0]; - setSelectedFile(file); - }; - - const handleFileClear = () => { - setSelectedFile(null); - }; - return (
0 ? 'file-selected' : '' }`} > handleFileChange(e)} + onChange={e => handlerFileChange(e)} ref={fileInputRef} /> {/* 현재는 text만 바뀌는상태! input 초기화 작업해주세용 */} - {selectedFile && ( // Display the trash icon only if a file is selected + {inquiryForm.fileInfos.length > 0 && ( // Display the trash icon only if a file is selected handlerFileClear('edit')} /> )}
@@ -208,9 +200,11 @@ function InquiryWrite({
- + {detail.createDt && ( + + )} diff --git a/src/containers/cstmrService/inquiry/UserInquiryContainer.js b/src/containers/cstmrService/inquiry/UserInquiryContainer.js index 65fd98e..01ab683 100644 --- a/src/containers/cstmrService/inquiry/UserInquiryContainer.js +++ b/src/containers/cstmrService/inquiry/UserInquiryContainer.js @@ -2,7 +2,7 @@ import { Button, Badge } from 'reactstrap'; import AppCollapse from '@components/app-collapse'; import { useDispatch, useSelector } from 'react-redux'; import * as Actions from '../../../modules/cstmrService/inquiry/action'; -import { Fragment, useCallback, useEffect, useState } from 'react'; +import { Fragment, useCallback, useEffect, useRef, useState } from 'react'; import moment from 'moment'; import InquiryWrite from '../../../components/cstmrService/inquiry/InquiryWrite'; import { InfoModal } from '../../../components/modal/InfoModal'; @@ -15,13 +15,16 @@ export default function UserInquiryContainer({ memberName }) { state => state.qnaState ); + const fileInputRef = useRef(null); const [isInquiryModalOpen, setIsInquiryModalOpen] = useState(false); const [inquiryForm, setInquiryForm] = useState({ category: '칭찬', contact: '', title: '', - content: '' + content: '', + fileInfos: [] }); + const [validationModal, setValidationModal] = useState({ errorType: { isOpen: false, @@ -129,7 +132,8 @@ export default function UserInquiryContainer({ memberName }) { category: '칭찬', contact: '', title: '', - content: '' + content: '', + fileInfos: [] }); } setIsInquiryModalOpen(!isInquiryModalOpen); @@ -148,9 +152,25 @@ export default function UserInquiryContainer({ memberName }) { [inquiryForm] ); + const handlerFileChange = e => { + const file = e.target.files[0]; + setInquiryForm({ + ...inquiryForm, + fileInfos: [file] + }); + }; + + const handlerFileClear = () => { + setInquiryForm({ + ...inquiryForm, + fileInfos: [] + }); + }; + // 문의 등록 event handler const handlerSubmitInquiryForm = type => { - const { category, title, content, contact, qnaSno } = inquiryForm; + const { category, title, content, contact, fileInfos, qnaSno } = + inquiryForm; if (!contact) { setValidationModal({ @@ -192,6 +212,34 @@ export default function UserInquiryContainer({ memberName }) { } }); return; + } else if (fileInfos.length > 0) { + const allowedExtensions = ['jpg', 'png', 'jpeg', 'gif']; + const fileExtension = fileInfos[0].name.split('.').pop().toLowerCase(); + const maxFileSize = 3 * 1024 * 1024; // 3MB + + if (!allowedExtensions.includes(fileExtension)) { + setValidationModal({ + ...validationModal, + errorType: { + title: '파일 형식 오류', + isOpen: true, + desc: '파일 형식은 jpg, png, jpeg, gif 형식만 가능합니다.' + } + }); + return; + } + + if (fileInfos[0].size > maxFileSize) { + setValidationModal({ + ...validationModal, + errorType: { + title: '파일 크기 오류', + isOpen: true, + desc: '파일 크기는 3MB를 초과할 수 없습니다.' + } + }); + return; + } } if (type === 'post') { @@ -201,6 +249,10 @@ export default function UserInquiryContainer({ memberName }) { form.append('title', title); form.append('content', content); + if (fileInfos.length > 0) { + form.append('files', fileInfos[0]); + } + dispatch(Actions.USER_INQUIRY.request(form)); } else if (type === 'edit') { dispatch( @@ -249,13 +301,16 @@ export default function UserInquiryContainer({ memberName }) {