Skip to content
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
60 changes: 58 additions & 2 deletions src/app/(user-page)/mypage/_features/ProfileImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import Modal from '@/components/ui/modal/Modal';
import { useUpdateProfileImageMutation } from '@/hooks/mutations/useMyPageMutation';
import { useProfileQuery } from '@/hooks/queries/useMyPageQueries';
import { QUERY_KEYS } from '@/hooks/queries/useMyPageQueries';
import { useQueryClient } from '@tanstack/react-query';
import { Pencil } from 'lucide-react';
import Image from 'next/image';
import { useEffect, useRef, useState } from 'react';

import { MAX_FILE_SIZE } from '../../../../constants/mypage/mypageConstant';
import {
DEFAULT_PROFILE_IMAGE,
MAX_FILE_SIZE,
} from '../../../../constants/mypage/mypageConstant';
import SkeletonProfileImage from './skeletons/SkeletonProfileImage';

const ProfileImage = () => {
Expand All @@ -19,6 +24,7 @@
const [fileSizeError, setFileSizeError] = useState<string | null>(null);
const [isMobile, setIsMobile] = useState<boolean>(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const queryClient = useQueryClient();

// ํ”„๋กœํ•„ ๋ฐ์ดํ„ฐ ์กฐํšŒ
const { data: profileData, isLoading: isProfileLoading } = useProfileQuery();
Expand All @@ -29,6 +35,7 @@

// ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ URL
const profileImageUrl = profileData?.data?.profilePic || null;
const isDefaultImage = profileImageUrl?.includes('default-profile.png');

// ๋ฐ˜์‘ํ˜• ํ™”๋ฉด ํฌ๊ธฐ ๊ฐ์ง€
useEffect(() => {
Expand Down Expand Up @@ -85,10 +92,41 @@
resetImageState();
// ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ
setIsModalOpen(false);
// ๋ฐฐ๋„ˆ ์ฟผ๋ฆฌ ๋ฌดํšจํ™” (์ค‘์š”! - ํ—ค๋”์˜ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋„ ์—…๋ฐ์ดํŠธ๋˜๋„๋ก)
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.banner() });
},
});
};

// ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ํ•ธ๋“ค๋Ÿฌ
const handleResetToDefault = async () => {
try {
// ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ URL์„ Blob์œผ๋กœ ๋ณ€ํ™˜
const response = await fetch(DEFAULT_PROFILE_IMAGE);
const blob = await response.blob();

// ์ด์ „ ๋ฏธ๋ฆฌ๋ณด๊ธฐ URL ํ•ด์ œ
if (previewUrl) URL.revokeObjectURL(previewUrl);

// ์ƒˆ ๋ฏธ๋ฆฌ๋ณด๊ธฐ URL ์ƒ์„ฑ
const objectUrl = URL.createObjectURL(blob);
setPreviewUrl(objectUrl);

// ์„ ํƒ๋œ ํŒŒ์ผ ์„ค์ • (์ด๊ฒƒ์ด handleConfirm์—์„œ ์‚ฌ์šฉ๋จ)
const defaultImageFile = new File([blob], 'default-profile.png', {
type: blob.type,
});
setSelectedFile(defaultImageFile);

// ํŒŒ์ผ ํฌ๊ธฐ ์—๋Ÿฌ ์ดˆ๊ธฐํ™”
setFileSizeError(null);

// ์—ฌ๊ธฐ์„œ updateImage ํ˜ธ์ถœ ์ œ๊ฑฐ - ๋ณ€๊ฒฝ ๋ฒ„ํŠผ ํด๋ฆญํ•  ๋•Œ๋งŒ ์ ์šฉ๋˜๋„๋ก
} catch (error) {
console.error('๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:', error);
}
};

// ์ƒํƒœ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ (์ค‘๋ณต ์ฝ”๋“œ ์ œ๊ฑฐ)
const resetImageState = () => {
setSelectedFile(null);
Expand Down Expand Up @@ -201,7 +239,25 @@
disableConfirm={!!fileSizeError || !selectedFile}
>
<div className="flex flex-col items-center justify-center gap-[16px]">
<div className="typo-head3 text-Cgray700">ํ”„๋กœํ•„ ์ด๋ฏธ์ง€</div>
<div className="flex w-full justify-between">
<div className="typo-head3 text-Cgray700">ํ”„๋กœํ•„ ์ด๋ฏธ์ง€</div>
<button
className={`text-[12px] ${
isDefaultImage && !previewUrl
? 'cursor-not-allowed text-Cgray400 opacity-50'
: 'cursor-pointer text-Cgray500 underline hover:text-Cgray700'
}`}
onClick={
isDefaultImage && !previewUrl ? undefined : handleResetToDefault
}
disabled={isDefaultImage && !previewUrl}
aria-disabled={isDefaultImage && !previewUrl}
>
{isDefaultImage && !previewUrl
? '๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ ์šฉ๋จ'
: '๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋กœ ๋ณ€๊ฒฝ'}
</button>
</div>
<div className="flex justify-center">
<button
type="button"
Expand Down
17 changes: 14 additions & 3 deletions src/components/common/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Logo from '@/assets/icon/logo.svg';
import { useLogoutMutation } from '@/hooks/mutations/useUserMutation';
import { QUERY_KEYS } from '@/hooks/queries/useMyPageQueries';
import { useBannerQueries } from '@/hooks/queries/useMyPageQueries';
import { translateCategoryNameToKor } from '@/util/searchFilter';
import { useQueryClient } from '@tanstack/react-query';
import { MEETING_TYPES } from 'constants/category/category';
Expand Down Expand Up @@ -189,17 +190,26 @@ const NavLinks = ({ isMobile }: { isMobile?: boolean }) => {
);
};

const Header = ({ userInfo }: { userInfo: IBanner }) => {
const Header = ({ userInfo: initialUserInfo }: { userInfo?: IBanner }) => {
const [isOpen, setIsOpen] = useState(false);
const userId = undefined;

// useBannerQueries ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ๋„ˆ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
const { data: queryUserInfo, isLoading } = useBannerQueries({
initialData: initialUserInfo || undefined, // ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ๋กœ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜จ ๊ฐ’ ์‚ฌ์šฉ
});

// ์„œ๋ฒ„์—์„œ ๋ฐ›์€ ์ •๋ณด์™€ ์ฟผ๋ฆฌ๋กœ ๋ฐ›์€ ์ •๋ณด๋ฅผ ๋ณ‘ํ•ฉ
const userInfo = queryUserInfo || initialUserInfo;
const isLogIn = !!userInfo;

const queryClient = useQueryClient();

// ๋ฐฐ๋„ˆ ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ฟผ๋ฆฌ ์บ์‹œ ์—…๋ฐ์ดํŠธ
useEffect(() => {
if (userInfo) {
queryClient.setQueryData(QUERY_KEYS.banner(), userInfo);
}
}, [userInfo]);
}, [userInfo, queryClient]);

return (
<div>
Expand Down Expand Up @@ -244,4 +254,5 @@ const Header = ({ userInfo }: { userInfo: IBanner }) => {
</div>
);
};

export default Header;
4 changes: 4 additions & 0 deletions src/constants/mypage/mypageConstant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Code, Compass, Mars, Paintbrush, Venus, X } from 'lucide-react';

import { IIconProps } from '../../types/mypageTypes';

//๊ธฐ๋ณธ ์ด๋ฏธ์ง€ url
export const DEFAULT_PROFILE_IMAGE =
'https://deving-bucket.s3.ap-northeast-2.amazonaws.com/cat_profile.png';

// ํƒญ ํƒ€์ž… ์ƒ์ˆ˜
export const TAB_TYPES = {
BASIC: 'basic',
Expand Down
81 changes: 71 additions & 10 deletions src/hooks/mutations/useMyPageMutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import {
getBanner,
updateContactInfo,
updatePassword,
updateProfile,
Expand All @@ -23,12 +24,17 @@ export const useUpdateProfileMutation = () => {
onMutate: async (newProfileData) => {
// ์ง„ํ–‰ ์ค‘์ธ ์ฟผ๋ฆฌ ์ทจ์†Œ
await queryClient.cancelQueries({ queryKey: QUERY_KEYS.profile() });
// banner ์ฟผ๋ฆฌ๋„ ์ทจ์†Œ (์ถ”๊ฐ€)
await queryClient.cancelQueries({ queryKey: QUERY_KEYS.banner() });

// ํ˜„์žฌ ์บ์‹œ ์ €์žฅ
const previousData = queryClient.getQueryData<IProfileResponse>(
QUERY_KEYS.profile(),
);

// ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ๋„ ์ €์žฅ
const previousBannerData = queryClient.getQueryData(QUERY_KEYS.banner());

if (previousData) {
// ์บ์‹œ ์—…๋ฐ์ดํŠธ
queryClient.setQueryData<IProfileResponse>(QUERY_KEYS.profile(), {
Expand All @@ -43,9 +49,17 @@ export const useUpdateProfileMutation = () => {
location: newProfileData.location,
},
});

// ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ๋„ ์—…๋ฐ์ดํŠธ (์ด๋ฆ„๋งŒ ๋ณ€๊ฒฝ)
if (previousBannerData) {
queryClient.setQueryData(QUERY_KEYS.banner(), {
...previousBannerData,
name: newProfileData.name,
});
}
}

return { previousData };
return { previousData, previousBannerData };
},

onError: (error, _, context) => {
Expand All @@ -56,14 +70,32 @@ export const useUpdateProfileMutation = () => {
if (context?.previousData) {
queryClient.setQueryData(QUERY_KEYS.profile(), context.previousData);
}
if (context?.previousBannerData) {
queryClient.setQueryData(
QUERY_KEYS.banner(),
context.previousBannerData,
);
}
},

onSuccess: () => {
onSuccess: async () => {
showToast('๊ธฐ๋ณธ ์ •๋ณด๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.', 'success');

// ์„ฑ๊ณต ํ›„ ์ตœ์‹  ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
try {
const bannerData = await getBanner();
if (bannerData) {
queryClient.setQueryData(QUERY_KEYS.banner(), bannerData);
}
} catch (error) {
console.error('๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
}
},

onSettled: () => {
// ์ฟผ๋ฆฌ ๋ฌดํšจํ™”
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.profile() });
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.banner() });
},
});
};
Expand Down Expand Up @@ -134,53 +166,82 @@ export const useUpdateProfileImageMutation = () => {

onMutate: async (file) => {
await queryClient.cancelQueries({ queryKey: QUERY_KEYS.profile() });
await queryClient.cancelQueries({ queryKey: QUERY_KEYS.banner() });

const previousData = queryClient.getQueryData<IProfileResponse>(
QUERY_KEYS.profile(),
);

// ํŒŒ์ผ์„ ์ž„์‹œ URL๋กœ ๋ณ€ํ™˜ (๋ธŒ๋ผ์šฐ์ € ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ)
// ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
const previousBannerData = queryClient.getQueryData(QUERY_KEYS.banner());

// ํŒŒ์ผ์„ ์ž„์‹œ URL๋กœ ๋ณ€ํ™˜
const tempUrl = URL.createObjectURL(file);

if (previousData) {
queryClient.setQueryData<IProfileResponse>(QUERY_KEYS.profile(), {
...previousData,
data: {
...previousData.data,
profilePic: tempUrl, // ์ž„์‹œ URL๋กœ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ ์—…๋ฐ์ดํŠธ
profilePic: tempUrl,
},
});
}

return { previousData, tempUrl };
// ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ์—๋„ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ์—…๋ฐ์ดํŠธ
if (previousBannerData) {
queryClient.setQueryData(QUERY_KEYS.banner(), {
...previousBannerData,
profilePic: tempUrl,
});
}

return { previousData, previousBannerData, tempUrl };
},

onError: (error, _, context) => {
showToast('ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.', 'error');

// ์ด์ „ ๋ฐ์ดํ„ฐ๋กœ ๋กค๋ฐฑ
// ๋กค๋ฐฑ
if (context?.previousData) {
queryClient.setQueryData(QUERY_KEYS.profile(), context.previousData);
}
if (context?.previousBannerData) {
queryClient.setQueryData(
QUERY_KEYS.banner(),
context.previousBannerData,
);
}

// ์ž„์‹œ URL ํ•ด์ œ (๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฐฉ์ง€)
// ์ž„์‹œ URL ํ•ด์ œ
if (context?.tempUrl) {
URL.revokeObjectURL(context.tempUrl);
}
},

onSuccess: (_, __, context) => {
onSuccess: async (response, _, context) => {
showToast('ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.', 'success');

// ์„ฑ๊ณต ์‹œ์—๋„ ์ž„์‹œ URL ํ•ด์ œ ํ•„์š”
// ์ž„์‹œ URL ํ•ด์ œ
if (context?.tempUrl) {
URL.revokeObjectURL(context.tempUrl);
}

// ์„ฑ๊ณต ํ›„ ์ตœ์‹  ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
try {
const bannerData = await getBanner();
if (bannerData) {
queryClient.setQueryData(QUERY_KEYS.banner(), bannerData);
}
} catch (error) {
console.error('๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
}
},

onSettled: () => {
// ์„œ๋ฒ„์—์„œ ์ตœ์‹  ์ด๋ฏธ์ง€ URL ๊ฐ€์ ธ์˜ค๊ธฐ
// ๋ชจ๋“  ๊ด€๋ จ ์ฟผ๋ฆฌ ๋ฌดํšจํ™”
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.profile() });
queryClient.invalidateQueries({ queryKey: QUERY_KEYS.banner() });
},
});
};
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/mutations/useUserMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import {
} from 'service/api/user';
import { ISignupFormData } from 'types/auth';

import { getBanner } from '../../service/api/mypageProfile';
import { QUERY_KEYS } from '../queries/useMyPageQueries';

const useLoginMutation = ({
onSuccessCallback,
}: {
Expand All @@ -37,7 +40,19 @@ const useLoginMutation = ({
// refreshToken ์ €์žฅ
await setRefreshToken(res.refreshToken);

// ๋กœ๊ทธ์ธ ์„ฑ๊ณต ํ›„ ์ฆ‰์‹œ ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์™€์„œ ์บ์‹œ์— ์ €์žฅ
try {
const bannerData = await getBanner();
if (bannerData) {
queryClient.setQueryData(QUERY_KEYS.banner(), bannerData);
console.log('๋กœ๊ทธ์ธ ํ›„ ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ์„ค์ •:', bannerData);
}
} catch (error) {
console.error('๋กœ๊ทธ์ธ ํ›„ ๋ฐฐ๋„ˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ:', error);
}

showToast('๋กœ๊ทธ์ธ ์„ฑ๊ณต', 'success');

// ๋ฉ”์ธํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
onSuccessCallback();
},
Expand Down Expand Up @@ -130,6 +145,9 @@ const useLogoutMutation = () => {
await removeAccessToken();
await removeRefreshToken();

// banner ์ฟผ๋ฆฌ ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™”
queryClient.setQueryData(QUERY_KEYS.banner(), null);

// ๋ชจ๋“  ์บ์‹œ ์ง€์šฐ๊ธฐ
queryClient.invalidateQueries();

Expand Down