Skip to content

Commit

Permalink
Feat:채널 생성시 이미지 업로드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
navyjeongs committed Sep 10, 2023
1 parent 6a65a42 commit 899f76f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/components/MakeChannel/SelectRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import ToggleButton from '@components/Button/Toggle/index';
import useMakeGame from '@hooks/useMakeGame';
import { GameMethod } from '@constants/MakeGame';
import CustomRule from './CustomRule';
import { useRef, useState } from 'react';
import authAPI from '@apis/authAPI';
import Image from 'next/image';

const SelectRule = () => {
const [imageUrl, setImageUrl] = useState<string>();

const {
matchFormat,
handleMatchFormat,
Expand All @@ -18,8 +23,34 @@ const SelectRule = () => {
handleIsUseCustomRule,
customRule,
handleCustomRule,
handleImgUrl,
} = useMakeGame();

const inputRef = useRef<HTMLInputElement | null>(null);

const fetchUploadChannelImage = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (!e.target.files) {
return;
}

const formData = new FormData();
formData.append('uploadImage', e.target.files[0]);

try {
const res = await authAPI<{ imgUrl: string }>({
method: 'post',
url: `/api/image`,
data: formData,
headers: {
'Content-Type': 'multipart/form-data',
},
});

setImageUrl(res.data.imgUrl);
handleImgUrl(res.data.imgUrl);
} catch (error) {}
};

return (
<Container>
<Wrapper>
Expand Down Expand Up @@ -87,6 +118,10 @@ const SelectRule = () => {
)}
</CustomContainer>
</Rule>
<Rule>
<input type='file' accept='image/*' ref={inputRef} onChange={fetchUploadChannelImage} />
{imageUrl && <Image width={50} height={50} src={imageUrl} alt='channelImage' />}
</Rule>
</Wrapper>
</Container>
);
Expand Down

0 comments on commit 899f76f

Please sign in to comment.