Skip to content

Commit

Permalink
Merge pull request #132 from TheUpperPart/Feat/#131
Browse files Browse the repository at this point in the history
Feat/#131 채널 생성 시 이미지 업로드 구현
  • Loading branch information
navyjeongs authored Sep 11, 2023
2 parents 7d10d5d + dd05d16 commit 2d51846
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ['localhost', 'k.kakaocdn.net', 's3.ap-northeast-2.amazonaws.com'],
domains: ['localhost', 'k.kakaocdn.net', 'league-hub-s3.s3.ap-northeast-2.amazonaws.com'],
},
reactStrictMode: true,
};

module.exports = nextConfig;
module.exports = nextConfig;
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
11 changes: 9 additions & 2 deletions src/components/providers/MakeGameProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface MakeGameProps {
export interface BasicInfo {
title: string;
participationNum: number;
channelImageUrl: string;
}

export interface IsUseCustomRule {
Expand All @@ -33,6 +32,7 @@ const MakeGameProvider = ({ children }: MakeGameProps) => {
initBasicInfo,
initIsUseCustomRule,
initCustomRule,
initChannelImgUrl,
} = TFTInitialValue;

const [currentStep, setCurrentStep] = useState<number>(initCurrentStep);
Expand All @@ -41,7 +41,7 @@ const MakeGameProvider = ({ children }: MakeGameProps) => {
const [basicInfo, setBasicInfo] = useState<BasicInfo>(initBasicInfo);
const [isUseCustomRule, setIsUseCustomRule] = useState<IsUseCustomRule>(initIsUseCustomRule);
const [customRule, setCustomRule] = useState<CustomRule>(initCustomRule);

const [channelImgUrl, setChannelImgUrl] = useState<string>('');
const handleCurrentStep = () => {
if (currentStep < 3) {
setCurrentStep((prev) => prev + 1);
Expand Down Expand Up @@ -72,13 +72,18 @@ const MakeGameProvider = ({ children }: MakeGameProps) => {
setCustomRule({ ...customRule, [type]: value });
};

const handleImgUrl = (url: string) => {
setChannelImgUrl(url);
};

const resetState = () => {
setCurrentStep(initCurrentStep);
setGameCategory(initCategory);
setMatchFormat(initMatchFormat);
setBasicInfo(initBasicInfo);
setIsUseCustomRule(initIsUseCustomRule);
setCustomRule(initCustomRule);
setChannelImgUrl(initChannelImgUrl);
};

const isHaveBlankValue = () => {
Expand Down Expand Up @@ -108,6 +113,8 @@ const MakeGameProvider = ({ children }: MakeGameProps) => {
handleCustomRule,
resetState,
isHaveBlankValue,
channelImgUrl,
handleImgUrl,
};

return <MakeGameContext.Provider value={contextValue}>{children}</MakeGameContext.Provider>;
Expand Down
3 changes: 2 additions & 1 deletion src/constants/MakeGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface TFTInitial {
initBasicInfo: BasicInfo;
initIsUseCustomRule: IsUseCustomRule;
initCustomRule: CustomRule;
initChannelImgUrl: string;
}

export const TFTInitialValue: TFTInitial = {
Expand All @@ -28,7 +29,6 @@ export const TFTInitialValue: TFTInitial = {
initBasicInfo: {
title: '',
participationNum: 0,
channelImageUrl: '',
},
initIsUseCustomRule: {
tierMax: false,
Expand All @@ -40,4 +40,5 @@ export const TFTInitialValue: TFTInitial = {
tierMin: 0,
playCountMin: 50,
},
initChannelImgUrl: '',
};
2 changes: 2 additions & 0 deletions src/contexts/MakeGameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface MakeGameState {
handleCustomRule: (type: keyof CustomRule, value: number) => void;
resetState: () => void;
isHaveBlankValue: () => boolean;
channelImgUrl: string;
handleImgUrl: (url: string) => void;
}

const MakeGameContext = createContext<MakeGameState | null>(null);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/make-channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const MakeChannel = () => {
customRule,
resetState,
isHaveBlankValue,
channelImgUrl,
} = useMakeGame();

const { addChannel } = useChannels();
Expand All @@ -47,6 +48,7 @@ const MakeChannel = () => {
tierMin: customRule.tierMin,
playCount: isUseCustomRule.playCount,
playCountMin: customRule.playCountMin,
channelImageUrl: channelImgUrl,
},
});
resetState();
Expand Down

0 comments on commit 2d51846

Please sign in to comment.