Skip to content

Commit

Permalink
[FE] FEAT: 이미지 초기화 버튼 추가 #229
Browse files Browse the repository at this point in the history
  • Loading branch information
42inshin committed Feb 23, 2025
1 parent 59a0fd4 commit 6dfee9f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/api/endpoints/user/user.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const userApi = {
},

// 프로필 이미지 업데이트
updateProfileImage: async (profileImageUrl: string) => {
updateProfileImage: async (profileImageUrl: string | null) => {
const { data } = await instance.patch(`users/me/profile-image`, {
profileImageUrl,
});
Expand Down
23 changes: 23 additions & 0 deletions src/frontend/src/components/Profile/MyProfile/index.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const Header = styled.div`
justify-content: space-between;
`;

export const ProfileImageContainer = styled.div`
position: relative;
`;

export const ProfileImage = styled.img<{ $onClick: boolean }>`
width: 64px;
height: 64px;
Expand All @@ -32,6 +36,25 @@ export const ProfileImage = styled.img<{ $onClick: boolean }>`
}
`;

export const ProfileImageResetButton = styled.button`
position: absolute;
top: -6px;
left: -6px;
background-color: var(--palette-background-normal-alternative);
display: flex;
align-items: center;
justify-content: center;
border: 1px solid var(--palette-line-normal-neutral);
cursor: pointer;
border-radius: 50%;
padding: 8px;
& > img {
width: 8px;
height: 8px;
}
`;

export const HeaderButtonContainer = styled.div`
display: flex;
gap: 8px;
Expand Down
44 changes: 29 additions & 15 deletions src/frontend/src/components/Profile/MyProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
StateMessageInput,
StateMessagePlus,
ErrorMessage,
ProfileImageResetButton,
ProfileImageContainer,
} from './index.css';

import Edit from '@/assets/img/Edit.svg';
Expand Down Expand Up @@ -99,25 +101,37 @@ export const MyProfile = () => {
}
};

const handleImageReset = async () => {
const updateUser = await updateProfileImage(null);
console.log('image reset', updateUser);
};

return (
<Container>
<Profile>
<Header>
<ProfileImage
src={user.profileImageUrl ?? DefaultProfile}
onClick={handleImageClick}
$onClick={isEditMode}
onError={e => {
e.currentTarget.src = DefaultProfile;
}}
/>
<input
type="file"
ref={fileInputRef}
onChange={handleImageChange}
style={{ display: 'none' }}
accept="image/*"
/>
<ProfileImageContainer>
<ProfileImage
src={user.profileImageUrl ?? DefaultProfile}
onClick={handleImageClick}
$onClick={isEditMode}
onError={e => {
e.currentTarget.src = DefaultProfile;
}}
/>
<input
type="file"
ref={fileInputRef}
onChange={handleImageChange}
style={{ display: 'none' }}
accept="image/*"
/>
{isEditMode && (
<ProfileImageResetButton onClick={handleImageReset}>
<img src={Cancel} alt="cancel" />
</ProfileImageResetButton>
)}
</ProfileImageContainer>
<HeaderButtonContainer>
<IconButton
beforeImgUrl={isEditMode ? Check : Edit}
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/stores/useUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface UserStore {
updateMyProfile: (
updateUserRequestDto: UpdateUserRequestDto,
) => Promise<UserResponseDto | undefined>;
updateProfileImage: (profileImageUrl: string) => Promise<UserResponseDto | undefined>;
updateProfileImage: (profileImageUrl: string | null) => Promise<UserResponseDto | undefined>;
clearProfile: () => void;
}

Expand All @@ -34,7 +34,7 @@ export const useUserStore = create(
set({ user: data });
return data;
},
updateProfileImage: async (profileImageUrl: string) => {
updateProfileImage: async (profileImageUrl: string | null) => {
try {
const data = await userApi.updateProfileImage(profileImageUrl);
set({ user: data });
Expand Down

0 comments on commit 6dfee9f

Please sign in to comment.