Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.5차 QA] 선물 등록 완료 버튼 관련 버그 수정(기본 이미지 문제, 로딩 뷰 처리) #393

Merged
merged 9 commits into from
Feb 23, 2024
30 changes: 11 additions & 19 deletions src/components/GiftAdd/AddGiftFooter/AddGiftFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useNavigate } from 'react-router-dom';
import usePostGift from '../../../hooks/queries/gift/usePostGift';
import GiftAddNextBtn from '../AddGiftLink/common/GiftAddNextBtn/GiftAddNextBtn';
import * as S from './AddGiftFooter.styled';
Expand All @@ -17,6 +16,7 @@ interface AddGiftFooterProps {
setImageUrl: React.Dispatch<React.SetStateAction<string>>;
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void;
fileName: string;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
}

const AddGiftFooter = ({
Expand All @@ -31,30 +31,22 @@ const AddGiftFooter = ({
setImageUrl,
fileName,
updateAddGiftInfo,
setIsLoading,
}: AddGiftFooterProps) => {
const navigate = useNavigate();
const { mutation } = usePostGift(roomId, targetDate, setStep);
const { mutation } = usePostGift(roomId, targetDate, setStep, updateAddGiftInfo, setIsLoading);
const { putImageUrlToS3 } = usePutImageUrlToS3(roomId);

const onClick = async () => {
setIsLoading(true);
const { imageUrlS3 } = await putImageUrlToS3({ fileName, file, roomId, setImageUrl });
if (isActivated) {
updateAddGiftInfo({ name: '', cost: 0, imageUrl: '', url: '' });
mutation.mutate(
{
roomId: roomId,
name: name,
cost: cost,
imageUrl: imageUrlS3,
url: link,
},
{
onSuccess: () => {
navigate(`/add-gift/${roomId}/${targetDate}`);
setStep(0);
},
},
);
mutation.mutate({
roomId: roomId,
name: name,
cost: cost,
imageUrl: imageUrlS3,
url: link,
});
}
};

Expand Down
23 changes: 18 additions & 5 deletions src/components/GiftAdd/AddGiftLayout/AddGiftWithLinkLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ interface AddGiftWithLinkLayoutProps {
openGraph: OpenGraphResponseType;
targetDate: string;
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void;
setModalStatus: React.Dispatch<React.SetStateAction<boolean>>;
addGiftInfo: AddGiftInfo;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
}

const AddGiftWithLinkLayout = ({
Expand All @@ -29,7 +31,9 @@ const AddGiftWithLinkLayout = ({
openGraph,
targetDate,
updateAddGiftInfo,
setModalStatus,
addGiftInfo,
setIsLoading,
}: AddGiftWithLinkLayoutProps) => {
const [isActivated, setIsActivated] = useState(
!!addGiftInfo.name && !!addGiftInfo.cost && !!addGiftInfo.imageUrl,
Expand All @@ -52,15 +56,23 @@ const AddGiftWithLinkLayout = ({

useEffect(() => {
const fetchData = async () => {
console.log('OpenGraph imageUrl', openGraph);
setNameText(openGraph.title);
const convertedOgFile = await useConvertURLtoFile(openGraph.image);
setFile(convertedOgFile);
setFileName(openGraph.image);
setImageUrl(openGraph.image);
const convertResult = await useConvertURLtoFile({
url: openGraph.image,
setStep,
setModalStatus,
updateAddGiftInfo,
});
if (convertResult && 'convertedOgFile' in convertResult) {
setFile(convertResult.convertedOgFile);
setFileName(openGraph.image);
setImageUrl(openGraph.image);
}
};

fetchData();
}, [openGraph]);
}, [openGraph, setStep]);

return (
<S.AddGiftWithLinkLayoutWrapper>
Expand Down Expand Up @@ -103,6 +115,7 @@ const AddGiftWithLinkLayout = ({
updateAddGiftInfo={updateAddGiftInfo}
file={file}
setImageUrl={setImageUrl}
setIsLoading={setIsLoading}
/>
</S.AddGiftWithLinkLayoutWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface AddGiftWithLinkLayoutProps {
openGraph: OpenGraphResponseType;
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void;
addGiftInfo: AddGiftInfo;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
}

// 직접 입력 화면
Expand All @@ -31,7 +32,9 @@ export const AddGiftWithoutLinkLayout = ({
setLinkText,
targetDate,
updateAddGiftInfo,
modalStatus,
addGiftInfo,
setIsLoading,
}: AddGiftWithLinkLayoutProps) => {
const [isActivated, setIsActivated] = useState(
!!addGiftInfo.name && !!addGiftInfo.cost && !!addGiftInfo.url && !!addGiftInfo.imageUrl,
Expand All @@ -43,7 +46,7 @@ export const AddGiftWithoutLinkLayout = ({
const [file, setFile] = useState<File | null>(null);
const [, setIsImageUploaded] = useState<boolean>(false);
const [, setPreviewImage] = useState<string | null>(null);
const [isModalOpen, setIsModalOpen] = useState(true);
const [isModalOpen, setIsModalOpen] = useState(modalStatus);

const checkPriceNull = (price: number | null) => {
if (price === null) {
Expand All @@ -55,7 +58,7 @@ export const AddGiftWithoutLinkLayout = ({

// 모달 확인 클릭 버튼
const handleClickConfirmClick = () => {
setIsModalOpen(!isModalOpen);
setIsModalOpen(false);
setStep(3);
};

Expand Down Expand Up @@ -109,6 +112,7 @@ export const AddGiftWithoutLinkLayout = ({
updateAddGiftInfo={updateAddGiftInfo}
file={file}
setImageUrl={setImageUrl}
setIsLoading={setIsLoading}
/>
</S.AddGiftWithLinkLayoutWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const AddGiftImg = ({
<S.IcEmptyThumbnailWrapper>
<input
type='file'
accept='image/*'
accept='image/jpeg, image/png, image/gif, image/heic '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept가 다양해졌네요..! 이미지에 heic도 있군요.!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 저도 이렇게 heic도 추가하겠습니다 !! 감사합니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 폰 이미지 때문에 추가했어요!

style={{ display: 'none' }}
id='imgInput'
onChange={handleImageUpload}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as S from './GiftAddBtnWrapper.styled';
interface GiftAddBtnWrapperProps {
setStep: React.Dispatch<React.SetStateAction<number>>;
isActivated: boolean;
onClick: () => void;
onClick: VoidFunction;
}

const GiftAddBtnWrapper = ({ setStep, isActivated, onClick }: GiftAddBtnWrapperProps) => {
Expand All @@ -16,7 +16,7 @@ const GiftAddBtnWrapper = ({ setStep, isActivated, onClick }: GiftAddBtnWrapperP

return (
<S.GiftAddBtnWrapper>
<S.NoLinkText onClick={handleClickNoLink} >링크 없이 상품을 등록할게요</S.NoLinkText>
<S.NoLinkText onClick={handleClickNoLink}>링크 없이 상품을 등록할게요</S.NoLinkText>
<GiftAddNextBtn children={BtnText} isActivated={isActivated} onClick={onClick} />
</S.GiftAddBtnWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ interface InputUrlProps {
const InputUrl = ({ setIsActivated, text, setText }: InputUrlProps) => {
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const inputValue = e.target.value;
inputValue.length > 0 ? setIsActivated(true) : setIsActivated(false);
setText(inputValue);
inputValue.length > 0 ? setIsActivated(true) : setIsActivated(false);
};

const handleBtnClick = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/context/AddGift/AddGiftContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initialAddGiftInfo: AddGiftInfo = {
name: '',
cost: 0,
imageUrl: '',
url: ',',
url: '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗! 여기있었군요 ㅋㅋㅋㅋ쉼표 고친 것 너무 좋습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋ숨겨져 있던 오타 드디어 해결~!

};

const AddGiftContext = createContext<AddGiftInfoContext>({
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/queries/gift/usePostGift.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { post } from '../../../apis/client';
import { GiftPostRequestType } from '../../../types/gift';
import { AddGiftInfo, GiftPostRequestType } from '../../../types/gift';
import { useNavigate } from 'react-router-dom';
import { MY_GIFT_QUERY_KEY } from './useGetMyGift';

Expand All @@ -12,6 +12,8 @@ export const usePostGift = (
roomId: number,
targetDate: string,
setStep: React.Dispatch<React.SetStateAction<number>>,
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후에는 props로 받는 방법 이외에,

Suggested change
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void,
const { updateAddGiftInfo } = use뭐시기Context;
이렇게 불러와서
updateAddGiftInfo({ name: '', cost: 0, imageUrl: '', url: '' });
바로 사용해도 좋을 같습니다! 아무래도 props type까지 설정해줘야하고 살짝 귀찮을 있으니 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피드백 감사합니다~! 큐에이 하면서도 저 부분 때문에 귀찮아지더라고요ㅠ 그래서 giftee name 관련 context를 추가할 때는 제안해주신 방식과 같은 방식으로 직접 불러오는 방식으로 구현했습니다. 이 부분은 나중에 싹 리팩토링 하겠습니다~!

setIsLoading: React.Dispatch<React.SetStateAction<boolean>>,
) => {
const navigate = useNavigate();

Expand All @@ -24,6 +26,8 @@ export const usePostGift = (
queryClient.invalidateQueries({ queryKey: [MY_GIFT_QUERY_KEY[0], roomId] });
navigate(`/add-gift/${roomId}/${targetDate}`);
setStep(0);
setIsLoading(false);
updateAddGiftInfo({ name: '', cost: 0, imageUrl: '', url: '' });
},
onError: (error) => {
console.log('선물 등록 에러!!', error.message);
Expand Down
43 changes: 36 additions & 7 deletions src/hooks/useConvertURLtoFile.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
const useConvertURLtoFile = async (url: string) => {
const response = await fetch(url);
const data = await response.blob();
const ext = url.split('.').pop(); // url 구조에 맞게 수정할 것
const filename = url.split('/').pop(); // url 구조에 맞게 수정할 것
const metadata = { type: `image/${ext}` };
return new File([data], filename!, metadata);
import { AddGiftInfo } from '../types/gift';

interface convertURLtoFileProps {
url: string;
setStep: React.Dispatch<React.SetStateAction<number>>;
setModalStatus: React.Dispatch<React.SetStateAction<boolean>>;
updateAddGiftInfo: (newInfo: Partial<AddGiftInfo>) => void;
}

const useConvertURLtoFile = async ({
url,
setStep,
setModalStatus,
updateAddGiftInfo,
}: convertURLtoFileProps) => {
console.log('들어오고 있는 url', url);
try {
const response = await fetch(url);

if (response.status === 302) {
return setStep(3);
}

const data = await response.blob();
// const extensions = url.split('.').pop();
const filename = url.split('/').pop();
const metadata = { type: `image/*` };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서는 위에 예외처리 해준 이미지 파일들처럼 지정안해줘도 되나요~?! 왜 위에 except 제가 리뷰남긴 부분은 이미지 타입을 4가지로 지정한 이유는 에러 발생하지 않는 유형이겠죠?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

두 가지 이유 때문에 이 부분은 따로 변경하지 않았습니다!

  1. 이미 input accept 지정으로 한 번 걸러진 아이들이 들어오는 거라서
  2. 추후 다른 확장자를 가졌더라도 예외처리를 통해 해결이 될까봐 일단 유지했습니다!

const convertedOgFile = new File([data], filename!, metadata);
return { convertedOgFile };
} catch (error) {
console.error('error?', error);
updateAddGiftInfo({ imageUrl: '' });
setModalStatus(true);
setStep(3);
return { convertedOgFile: null };
}
};

export default useConvertURLtoFile;
13 changes: 11 additions & 2 deletions src/pages/GiftAdd/GiftAddPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { OpenGraphResponseType } from '../../types/etc';
import { useParams } from 'react-router-dom';
import AddGiftWithLinkLayout from '../../components/GiftAdd/AddGiftLayout/AddGiftWithLinkLayout';
import { useAddGiftContext } from '../../context/AddGift/AddGiftContext';
import Loading from '../Loading/Loading';

const GiftAddPage = () => {
const [step, setStep] = useState(0);
const [isLoading, setIsLoading] = useState(false);

// case 1 링크 입력 페이지 state들
const [itemNum, setItemNum] = useState(0);
Expand Down Expand Up @@ -55,21 +57,27 @@ const GiftAddPage = () => {
);

case 2:
return (
return isLoading ? (
<Loading />
) : (
<AddGiftWithLinkLayout
step={step}
setStep={setStep}
roomId={roomIdNumber}
link={linkText}
openGraph={openGraph}
targetDate={targetDate || ''}
setModalStatus={setModalStatus}
addGiftInfo={addGiftInfo}
updateAddGiftInfo={updateAddGiftInfo}
setIsLoading={setIsLoading}
/>
);

case 3:
return (
return isLoading ? (
<Loading />
) : (
<AddGiftWithoutLinkLayout
step={step}
setStep={setStep}
Expand All @@ -81,6 +89,7 @@ const GiftAddPage = () => {
addGiftInfo={addGiftInfo}
openGraph={openGraph}
updateAddGiftInfo={updateAddGiftInfo}
setIsLoading={setIsLoading}
/>
);
}
Expand Down