Skip to content

Commit f5a2235

Browse files
authored
[SSD-150] 포스트잇 생성 페이지 예외처리 (#88)
* [SSD-150] feat: Postit 입력 Valid 처리 수정 * [SSD-150] fix: 생성 버튼 클릭시 비활성화 처리 * [SSD-150] feat: PostitCreate Alert 구현 * [SSD-150] fix: 입력창 css 수정
1 parent d53a77d commit f5a2235

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

frontend/src/components/UnderBarInput/UnderBarInput.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import styles from './UnderBarInput.module.css';
33

4-
export default function UnderBarInput({ value, placeholder, onChange, name, onBlur }) {
4+
export default function UnderBarInput({ value, placeholder, onChange, name, onBlur, onClick }) {
55
return (
66
<input
77
type="text"
@@ -11,6 +11,7 @@ export default function UnderBarInput({ value, placeholder, onChange, name, onBl
1111
placeholder={placeholder}
1212
onChange={onChange}
1313
onBlur={onBlur}
14+
onClick={onClick}
1415
/>
1516
);
1617
}

frontend/src/pages/postit-create/PostItCreate.jsx

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { useEffect, useState, useRef, useMemo } from 'react';
22
import staticImagePath from '../../constant/staticImagePath';
33
import styles from './PostItCreate.module.css';
44
import UnderBarInput from '../../components/UnderBarInput/UnderBarInput';
55
import BottomButton from '../../components/BottomButton/BottomButton';
66
import { api } from '../../api/api';
77
import { useNavigate, useParams } from 'react-router-dom';
8+
import { ssrModuleExportsKey } from 'vite/module-runner';
9+
import Alert from '../../components/Alert/Alert';
810

911
export default function PostItCreate() {
1012
const { timepaperId } = useParams();
1113
const fileInputRef = useRef(null);
1214
const [isSubmitable, setIsSubmitable] = useState(false);
1315
const templates = [staticImagePath.postitAfternoon, staticImagePath.postitNight];
1416
const [errors, setErrors] = useState({});
17+
const [isTouched, setIsTouched] = useState({ content: false, authorName: false });
18+
const [isShowAlert, setIsShowAlert] = useState(false);
19+
const [alertMessage, setAlertMessage] = useState('');
1520
const navigate = useNavigate();
1621

1722
const INITIAL_FORM_DATA = {
@@ -50,6 +55,10 @@ export default function PostItCreate() {
5055
}));
5156
};
5257

58+
useEffect(() => {
59+
setErrors(validateInput(inputData));
60+
}, [inputData]);
61+
5362
const handleStaticImage = (src) => () => {
5463
setImageState((prev) => ({
5564
...prev,
@@ -59,6 +68,13 @@ export default function PostItCreate() {
5968
}));
6069
};
6170

71+
const handleOnClick = (e) => {
72+
const { name } = e.target;
73+
setIsTouched((prev) => ({ ...prev, [name]: true }));
74+
const a = validateInput(inputData);
75+
setErrors(a);
76+
};
77+
6278
const handleUploadImageClick = () => {
6379
fileInputRef.current.click();
6480
};
@@ -70,7 +86,8 @@ export default function PostItCreate() {
7086

7187
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif'];
7288
if (!validImageTypes.includes(file.type)) {
73-
alert('이미지 파일만 업로드 가능합니다.');
89+
setIsShowAlert(true);
90+
setAlertMessage('이미지 파일만 업로드 가능합니다.');
7491
return;
7592
}
7693

@@ -86,10 +103,6 @@ export default function PostItCreate() {
86103
reader.readAsDataURL(file);
87104
};
88105

89-
const handleBlur = () => {
90-
setErrors(validateInput(inputData));
91-
};
92-
93106
const validateInput = (data) => {
94107
const errors = {};
95108
let isValid = true;
@@ -151,42 +164,54 @@ export default function PostItCreate() {
151164
formData.append('image', imageState.imageData);
152165
}
153166

167+
setIsSubmitable(false);
154168
(async () => {
155169
try {
156170
const response = await api.createPostit(timepaperId, formData);
157171
if (response.status === 201) {
158172
navigate(`/timepaper/${timepaperId}`, { replace: true });
159173
}
160174
} catch (error) {
161-
alert('등록에 실패했습니다.');
162-
} finally {
163-
setIsSubmitable(false);
175+
setIsShowAlert(true);
176+
setAlertMessage('등록에 실패했습니다.');
177+
setIsSubmitable(true);
164178
}
165179
})();
166180
};
167181

182+
const handleAlertButtonClick = () => {
183+
setIsShowAlert(false);
184+
};
185+
168186
return (
169187
<>
188+
{isShowAlert && (
189+
<Alert message={alertMessage} buttonTitle={'확인'} onClick={handleAlertButtonClick}></Alert>
190+
)}
170191
<form action="" className={styles.container}>
171192
<div className={styles.underBarInput}>
172193
<UnderBarInput
173194
onChange={handleOnChange}
174195
name="authorName"
175196
placeholder="작성자 입력"
176-
onBlur={handleBlur}
197+
onClick={handleOnClick}
177198
></UnderBarInput>
178-
{errors.authorName && <p className={styles.error}>{errors.authorName}</p>}
199+
{isTouched.authorName && errors.authorName && (
200+
<p className={styles.error}>{errors.authorName}</p>
201+
)}
179202
</div>
180203
<div className={styles.selectedImage}>
181204
<img src={imageState.preview} className="logo-image" alt="선택된 포스트잇 이미지" />
182205
<div className={styles.textareaContainer}>
183206
<textarea
184207
onChange={handleOnChange}
185-
onBlur={handleBlur}
186208
name="content"
187209
className={styles.textarea}
210+
onClick={handleOnClick}
188211
></textarea>
189-
{errors.content && <p className={styles.textareaError}>{errors.content}</p>}
212+
{isTouched.content && errors.content && (
213+
<p className={styles.textareaError}>{errors.content}</p>
214+
)}
190215
</div>
191216
</div>
192217
<section className={styles.imageContainer}>

frontend/src/pages/postit-create/PostItCreate.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
.textarea {
4747
position: absolute;
48-
top: 113px;
48+
top: 125px;
4949
left: 37px;
5050
background-color: inherit;
5151
border: none;

frontend/src/pages/time-paper-create/TimePaperCreate.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export default function TimePaperCreate() {
9898
<form>
9999
<div className={styles.container}>
100100
{isError && (
101-
<Alert
102-
buttonTitle={'확인'}
103-
message={errorMessage}
104-
onClick={handleAlertButtonClick}
105-
></Alert>
101+
<Alert
102+
buttonTitle={'확인'}
103+
message={errorMessage}
104+
onClick={handleAlertButtonClick}
105+
></Alert>
106106
)}
107107
<div className={styles.inputContainer}>
108108
<UnderBarInput

0 commit comments

Comments
 (0)