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

[선물 등록, 선물방 편집] 리액트 쿼리 관련 데이터 실시간 업데이트 문제 해결 #339

Merged
merged 17 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 18 additions & 53 deletions src/components/GiftAdd/AddGiftFooter/AddGiftFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// import { useNavigate } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
// import { IcLeft } from '../../../assets/svg';
import usePostGift from '../../../hooks/queries/gift/usePostGift';
import GiftAddNextBtn from '../AddGiftLink/common/GiftAddNextBtn/GiftAddNextBtn';
import * as S from './AddGiftFooter.styled';
import { OpenGraphResponseType } from '../../../types/etc';

interface ItemInfoType {
roomId: number;
}
Expand Down Expand Up @@ -41,68 +37,37 @@ const AddGiftFooter = ({
// fetchPresignedUrl,
// setImageUrl,
}: AddGiftFooterProps) => {
// const updatedItemInfo = {
// roomId: itemInfo.roomId,
// name: name,
// cost: cost,
// imageUrl: imageUrl,
// url: link,
// };
const navigate = useNavigate();
const { mutation } = usePostGift(itemInfo.roomId, targetDate);
const { mutation } = usePostGift(itemInfo.roomId, targetDate, setStep);

const onClick = async () => {
console.log(fileName);
function onClickBtn() {
// const { presignedUrl } = await fetchPresignedUrl(fileName);
// await saveImageUrl(presignedUrl);
// setImageUrl(presignedUrl);
if (isActivated) {
console.log('값', imageUrl);
if (openGraph.image) {
mutation.mutate(
{
roomId: itemInfo.roomId,
name: name,
cost: cost,
imageUrl: imageUrl,
url: link,
},
{
onSuccess: () => {
// console.log('PUT 서버통신 후 presignedUrl', presignedUrl);
navigate(`/add-gift/${itemInfo.roomId}/${targetDate}`);
setStep(0);
},
},
);
mutation.mutate({
roomId: itemInfo.roomId,
name: name,
cost: cost,
imageUrl: imageUrl,
url: link,
});
} else {
Comment on lines +49 to +55
Copy link
Contributor

Choose a reason for hiding this comment

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

흠.. 뭔가 이 객체를 따로 관리 할 수 있는 방법이 없을까요 ??? 상수화 하여 가독성이 좋아지도록이요!! 이렇게 직접적으로 드러내는게 더 좋은 것 같다면 그것도 오케이인데, if 부분과 else 부분 모두 이 코드가 중복 되어지는거 같아서요!!

Copy link
Contributor Author

@urjimyu urjimyu Feb 16, 2024

Choose a reason for hiding this comment

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

조건문의 경우 presignedUrl을 막으면서 임시로 추가된 것이어서 다른 이슈 해결하면서 중복 제거했습니다! 말씀해주신 대로 상수화를 통해 가독성을 개선하면 훨씬 좋을 것 같네요 감사합니다🥰 context 브랜치에서 상수화 가능할 것 같아서 다른 PR에서 적용해보겠습니다:)

mutation.mutate(
{
roomId: itemInfo.roomId,
name: name,
cost: cost,
imageUrl: '',
url: link,
},
{
onSuccess: () => {
// console.log('PUT 서버통신 후 presignedUrl', presignedUrl);
navigate(`/add-gift/${itemInfo.roomId}/${targetDate}`);
setStep(0);
},
},
);
mutation.mutate({
roomId: itemInfo.roomId,
name: name,
cost: cost,
imageUrl: '',
url: link,
});
}
}
};

// const handlePrevBtnClick = () => {
// setStep(1);
// };
}

return (
<S.AddGiftFooterWrapper>
<GiftAddNextBtn isActivated={isActivated} onClick={onClick} children='완료' />
<GiftAddNextBtn isActivated={isActivated} onClick={onClickBtn} children='완료' />
</S.AddGiftFooterWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as S from './GiftAddNextBtn.styled';
interface GiftAddNextBtnProps {
children: React.ReactNode;
isActivated: boolean;
onClick: () => void;
onClick: VoidFunction;
Copy link
Contributor

Choose a reason for hiding this comment

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

오오 !!!! 매우매우 굳입니다 ㅎㅎㅎ 이거 그때 합숙때 알게 된 것이군요 !! 저도 반영해야겠어요😊

}
const GiftAddNextBtn = ({ children, isActivated, onClick }: GiftAddNextBtnProps) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface GiftAddPageBottomProps {
}

const GiftAddPageBottom = ({ adPrice, myGiftData }: GiftAddPageBottomProps) => {
const [isAdVisible, setIsAdVisible] = useState(myGiftData.length <= 1);
const [isAdVisible, setIsAdVisible] = useState(false);

const handleIcCancelClick = () => {
setIsAdVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import GiftAddPageLayoutHeader from './GiftAddPageLayoutHeader';
import useGetMyGift from '../../../hooks/queries/gift/useGetMyGift';
import EmptyGiftAddButtonsWrapper from '../GiftAddButtons/EmptyGiftAddButtonsWrapper';
import useDeleteMyGift from '../../../hooks/queries/gift/useDeleteMyGift';
// import { useEffect } from 'react';

interface GiftAddPageLayoutProps {
roomId: string;
Expand All @@ -19,14 +18,13 @@ interface GiftAddPageLayoutProps {

const GiftAddPageLayout = ({
targetDate,
// step,
roomId,
setStep,
// itemNum,
setItemNum,
}: GiftAddPageLayoutProps) => {
const roomIdNumber = parseInt(roomId);
const { data } = useGetMyGift({ roomId: roomIdNumber });
const { data } = useGetMyGift({ roomId: roomIdNumber});
setItemNum(data.data.myGiftDtoList.length);
const parsedRoomId = parseInt(roomId);
const { mutation } = useDeleteMyGift(parsedRoomId);
Expand All @@ -42,10 +40,6 @@ const GiftAddPageLayout = ({
mutation.mutate(giftId);
};

// useEffect(()=> {

// }, [])

return (
<S.GiftAddPageWrapper>
<GiftAddPageLayoutHeader title={'내가 등록한 선물'} />
Expand Down
4 changes: 1 addition & 3 deletions src/hooks/queries/etc/usePostMyPresignedUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ const usePostMyPresignedUrl = (roomId: number) => {
const queryClient = useQueryClient();

return useMutation({
mutationKey: [MY_GIFT_QUERY_KEY[0]],
mutationFn: postPresignedUrl,
onSuccess: (data) => {
console.log('usePostPresignedUrl onSuccess 내꺼로 포스트 성공~', data);
queryClient.invalidateQueries({ queryKey: [MY_GIFT_QUERY_KEY[0], roomId] });

queryClient.setQueryData([MY_GIFT_QUERY_KEY[0], roomId], data);
},
onError: (error) => {
console.log('내 선물 POST 중 에러가 발생했습니다.', error.message);
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/queries/gift/useDeleteMyGift.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
import { del } from '../../../apis/client';
import { MY_GIFT_QUERY_KEY } from './useGetMyGift';

export const deleteMyGift = async (giftId: number) => {
export async function deleteMyGift(giftId: number) {
await del(`/gift/my/${giftId}`);
};
}

export const useDeleteMyGift = (roomId: number) => {
const queryClient = useQueryClient();

const mutation = useMutation({
mutationKey: [MY_GIFT_QUERY_KEY[0]],
mutationFn: deleteMyGift,
onSuccess() {
console.log('선물 삭제 성공');
Copy link
Member

Choose a reason for hiding this comment

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

mutationKey 제거 대박 너무 좋아요!
제가 최근에 강의를 봤는데 사용자에게 변경사항을 실제로 보여줄 땐 3가지 방법이 있다합니다!

  1. 낙관적인 업데이트 : 서버 호출이 잘될 거라 가정하고, 잘 안됐을 경우 되돌리는 방식
  2. 서버에서 받은 데이터를 가져오는 겁니다 : mutation을 실행 할 때 업데이트 된 데이터를 가져와 리액트 쿼리 캐시를 업데이트하는 방식
  3. 마지막으로 관련 쿼리를 무효화하는 방식 : 쿼리를 무효화하면, 클라 데이터를 서버의 데이터와 동기화하기 위해 서버에 재요청이 발생하는 방식
스크린샷 2024-02-14 오후 4 20 44 그래서 언니 방식이 3번째 방식으로 진행되었구나! 바로 이해가 쏘옥 되었습니다. 혹시 다른 분들을 위해 리뷰 남겨봅니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

대박.. 이 리뷰 너무너무 좋아요 이렇게 3가지 방식이 있고, 저희는 3번째 방식으로 진행하고 있다는 사실이 이해가 쏙쏙 된거 같습니다 !!!!! 너무너무 감사해요 너무너무 좋아요 !!!!!!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

와,, 정말 리액트 쿼리 설명 너무 이해 쏙쏙 되게 깊이 있게 해주셔서 감사해요!! 덕분에 더 깊은 내용까지 이해하게 되었습니다🫶 좋은 레퍼런스까지,, 정말 감사해요!

Expand Down
28 changes: 18 additions & 10 deletions src/hooks/queries/gift/usePostGift.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { post } from '../../../apis/client';
import { GiftPostRequestType } from '../../../types/gift';
import { useNavigate } from 'react-router-dom';
import { MY_GIFT_QUERY_KEY } from './useGetMyGift';

export const postNewGift = async (body: GiftPostRequestType) => {
post(`/gift`, body);
};
export async function postNewGift(body: GiftPostRequestType) {
await post(`/gift`, body);
}

export const usePostGift = (roomId: number, targetDate: string) => {
export const usePostGift = (
roomId: number,
targetDate: string,
setStep: React.Dispatch<React.SetStateAction<number>>,
) => {
const navigate = useNavigate();

const queryClient = useQueryClient();

const mutation = useMutation({
mutationFn: postNewGift,
onSuccess: () => {
console.log('roomId, targetDate', roomId, targetDate);
onSuccess: (data) => {
console.log('선물 등록 성공!!');
queryClient.setQueryData([MY_GIFT_QUERY_KEY[0], roomId], data);
navigate(`/add-gift/${roomId}/${targetDate}`);
setStep(0);
},

onError: () => {
console.log('선물 등록 에러!!');
onError: (error) => {
console.log('선물 등록 에러!!', error.message);
},
});

Expand Down
10 changes: 4 additions & 6 deletions src/hooks/queries/room/useDeleteRoomMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ export const deleteRoomMember = async ({ roomId, memberId }: DeleteRoomMember) =
await del(`/room/${roomId}/members/${memberId}`);
};

const useDeleteRoomMember = ({ roomId, memberId }: DeleteRoomMember) => {
const useDeleteRoomMember = ({ roomId }: DeleteRoomMember) => {
const queryClient = useQueryClient();

const mutation = useMutation({
mutationKey: [ROOM_MEMBER_QUERY_KEY[0]],
mutationFn: deleteRoomMember,
onSuccess() {
console.log('삭제 성공');
queryClient.invalidateQueries({ queryKey: [ROOM_MEMBER_QUERY_KEY[0], roomId, memberId] });
queryClient.invalidateQueries({ queryKey: [...ROOM_MEMBER_QUERY_KEY, roomId] });
},
onError: () => {
console.log('선물 삭제 중 에러가 발생했습니다.');
onError: (error) => {
console.log('선물 삭제 중 에러가 발생했습니다.', error.message);
},
});
return { mutation };
Expand Down
9 changes: 5 additions & 4 deletions src/pages/MyPage/EditRoom/CardGuest/CardGuest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Type1Tag from '../../../../components/IcTag/Type1/Type1';
import ProfileImage from '../../ProfileImage/ProfileImage';
import * as S from './CardGuest.style';
import useDeleteRoomMember from '../../../../hooks/queries/room/useDeleteRoomMember';
import { useLocation } from 'react-router';
// import { useLocation } from 'react-router';
import { useParams } from 'react-router-dom';

interface CardGuestProps {
user: string;
Expand All @@ -13,9 +14,9 @@ interface CardGuestProps {
}

const CardGuest = ({ user, makerState, profileImageUrl, memberId }: CardGuestProps) => {
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const roomIdString = searchParams.get('roomId');
const params = useParams();
const roomIdString = params.roomId;

const roomId = parseInt(roomIdString || '');
console.log('추출된 초대 코드', roomId);
const { mutation } = useDeleteRoomMember({ roomId, memberId });
Expand Down
10 changes: 5 additions & 5 deletions src/pages/MyPage/EditRoom/EditRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const EditRoom = () => {
console.log('추출된 초대 코드', roomId);
const roomWholeMemberData = useGetRoomMember(roomId).data;
const roomWholeOwnerData = useGetRoomOwner(roomId).data;
console.log(roomWholeMemberData);
console.log(roomWholeOwnerData);

const roomOwnerData = roomWholeOwnerData?.owner;
const roomGifteeData = roomWholeOwnerData.room;
const roomMemberData = roomWholeMemberData.members;
const roomMemberData = roomWholeMemberData;

console.log(roomOwnerData);
console.log('개설자 정보', roomOwnerData);
console.log('선물방', roomGifteeData);
console.log('선물 준비 멤버 정보', roomMemberData);

return (
<S.EditRoomWrapper>
Expand All @@ -36,7 +36,7 @@ const EditRoom = () => {
<CardGuest
user={roomOwnerData.name}
makerState={true}
profileImageUrl={roomOwnerData?.profileImgUrl}
profileImageUrl={roomOwnerData?.profileImageUrl}
memberId={roomOwnerData.ownerId}
/>

Expand Down
2 changes: 1 addition & 1 deletion src/types/member/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type RoomMemberType = {

export type OwnerListType = {
ownerId: number;
profileImgUrl: string;
profileImageUrl: string;
name: string;
};

Expand Down
Loading