Skip to content

Commit

Permalink
feat: 토픽 사진 추가할 때 사이즈 제한 주기
Browse files Browse the repository at this point in the history
  • Loading branch information
GC-Park committed Oct 19, 2023
1 parent d7f5fa6 commit 32e0aa5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
27 changes: 19 additions & 8 deletions frontend/src/components/TopicInfo/UpdatedTopicInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Space from '../common/Space';
import InputContainer from '../InputContainer';
import Text from '../common/Text';
import useCompressImage from '../../hooks/useCompressImage';
import Image from '../common/Image';
import ImageCommon from '../common/Image';
import { DEFAULT_TOPIC_IMAGE } from '../../constants';
import { putApi } from '../../apis/putApi';

Expand Down Expand Up @@ -135,6 +135,7 @@ function UpdatedTopicInfo({
) => {
const file = event.target.files && event.target.files[0];
const formData = new FormData();
const currentImage = new Image();

if (!file) {
showToast(
Expand All @@ -145,12 +146,22 @@ function UpdatedTopicInfo({
}

const compressedFile = await compressImage(file);
formData.append('image', compressedFile);

await putApi(`/topics/images/${id}`, formData);

const updatedImageUrl = URL.createObjectURL(compressedFile);
setChangedImages(updatedImageUrl);
currentImage.src = URL.createObjectURL(compressedFile);

currentImage.onload = async () => {
if (currentImage.width < 300) {
showToast(
'warning',
'이미지의 크기가 너무 작습니다. 다른 이미지를 선택해 주세요.',
);
return;
}
formData.append('image', compressedFile);
await putApi(`/topics/images/${id}`, formData);

const updatedImageUrl = URL.createObjectURL(compressedFile);
setChangedImages(updatedImageUrl);
};
};

return (
Expand Down Expand Up @@ -288,7 +299,7 @@ const ImageInputButton = styled.input`
display: none;
`;

const TopicImage = styled(Image)`
const TopicImage = styled(ImageCommon)`
border-radius: ${({ theme }) => theme.radius.medium};
`;

Expand Down
18 changes: 15 additions & 3 deletions frontend/src/pages/NewTopic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function NewTopic() {
event: React.ChangeEvent<HTMLInputElement>,
) => {
const file = event.target.files && event.target.files[0];
const currentImage = new Image();
if (!file) {
showToast(
'error',
Expand All @@ -136,9 +137,20 @@ function NewTopic() {
}

const compressedFile = await compressImage(file);

setFormImage(compressedFile);
setShowImage(URL.createObjectURL(file));
currentImage.src = URL.createObjectURL(compressedFile);

currentImage.onload = () => {
if (currentImage.width < 300) {
showToast(
'warning',
'이미지의 크기가 너무 작습니다. 다른 이미지를 선택해 주세요.',
);
return;
}

setFormImage(compressedFile);
setShowImage(URL.createObjectURL(file));
};
};

useEffect(() => {
Expand Down

0 comments on commit 32e0aa5

Please sign in to comment.