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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@emotion/css": "^11.13.5",
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@tanstack/react-query": "^5.90.21",
Comment thread
heeeeyong marked this conversation as resolved.
"@types/react-datepicker": "^7.0.0",
"axios": "^1.11.0",
"react": "^19.1.0",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 67 additions & 29 deletions src/components/common/Post/PostFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import like from '../../../assets/feed/like.svg';
import activeLike from '../../../assets/feed/activeLike.svg';
import comment from '../../../assets/feed/comment.svg';
Expand All @@ -7,6 +7,7 @@ import activeSave from '../../../assets/feed/activeSave.svg';
import lockIcon from '../../../assets/feed/lockIcon.svg';
import { postSaveFeed } from '@/api/feeds/postSave';
import { postFeedLike } from '@/api/feeds/postFeedLike';
import { usePreventDoubleClick } from '@/hooks/usePreventDoubleClick';
import { Container } from './PostFooter.styled';

interface PostFooterProps {
Expand Down Expand Up @@ -36,39 +37,71 @@ const PostFooter = ({
const [likeCount, setLikeCount] = useState<number>(initialLikeCount);
const [saved, setSaved] = useState(isSaved);

const handleLike = async () => {
try {
const response = await postFeedLike(feedId, !liked);
const likedRef = useRef(isLiked);
const savedRef = useRef(isSaved);
const { isLoading: isLikeLoading, run: runLike } = usePreventDoubleClick();
const { isLoading: isSaveLoading, run: runSave } = usePreventDoubleClick();

if (response.isSuccess) {
setLiked(response.data.isLiked);
setLikeCount(prev => (response.data.isLiked ? prev + 1 : prev - 1));
console.log('좋아요 상태 변경 성공:', response.data.isLiked);
} else {
console.error('좋아요 상태 변경 실패:', response.message);
useEffect(() => {
setLiked(isLiked);
likedRef.current = isLiked;
}, [isLiked]);

useEffect(() => {
setSaved(isSaved);
savedRef.current = isSaved;
}, [isSaved]);

const handleLike = () => {
runLike(async () => {
const nextLiked = !likedRef.current;
likedRef.current = nextLiked;
setLiked(nextLiked);
setLikeCount(prev => (nextLiked ? prev + 1 : prev - 1));

try {
const response = await postFeedLike(feedId, nextLiked);
if (!response.isSuccess && likedRef.current === nextLiked) {
const rollbackState = !nextLiked;
likedRef.current = rollbackState;
setLiked(rollbackState);
setLikeCount(prev => (nextLiked ? prev - 1 : prev + 1));
}
} catch {
if (likedRef.current === nextLiked) {
const rollbackState = !nextLiked;
likedRef.current = rollbackState;
setLiked(rollbackState);
setLikeCount(prev => (nextLiked ? prev - 1 : prev + 1));
}
}
} catch (error) {
console.error('좋아요 API 호출 실패:', error);
}
});
};

const handleSave = async () => {
try {
const response = await postSaveFeed(feedId, !saved);
const handleSave = () => {
runSave(async () => {
const nextSaved = !savedRef.current;
savedRef.current = nextSaved;
setSaved(nextSaved);
onSaveToggle?.(feedId, nextSaved);

if (response.isSuccess) {
const newSaveState = response.data?.isSaved ?? !saved;
setSaved(newSaveState);
console.log('저장 상태 변경 성공:', newSaveState);
if (onSaveToggle) {
onSaveToggle(feedId, newSaveState);
try {
const response = await postSaveFeed(feedId, nextSaved);
if (!response.isSuccess && savedRef.current === nextSaved) {
const rollbackState = !nextSaved;
savedRef.current = rollbackState;
setSaved(rollbackState);
onSaveToggle?.(feedId, rollbackState);
}
} catch {
if (savedRef.current === nextSaved) {
const rollbackState = !nextSaved;
savedRef.current = rollbackState;
setSaved(rollbackState);
onSaveToggle?.(feedId, rollbackState);
}
} else {
console.error('저장 상태 변경 실패:', response.message);
}
} catch (error) {
console.error('저장 API 호출 실패:', error);
}
});
};

const handleComment = () => {
Expand All @@ -80,7 +113,7 @@ const PostFooter = ({
<Container isDetail={isDetail}>
<div className="left">
<div className="count">
<img src={liked ? activeLike : like} onClick={handleLike} />
<img src={liked ? activeLike : like} onClick={handleLike} style={{ opacity: isLikeLoading ? 0.6 : 1 }} />
<div>{likeCount}</div>
</div>
<div className="count comment">
Expand All @@ -96,7 +129,12 @@ const PostFooter = ({
<img src={lockIcon} alt="비공개" />
)
) : (
<img src={saved ? activeSave : save} onClick={handleSave} alt="저장" />
<img
src={saved ? activeSave : save}
onClick={handleSave}
alt="저장"
style={{ opacity: isSaveLoading ? 0.6 : 1 }}
/>
)}
</div>
</Container>
Expand Down
45 changes: 33 additions & 12 deletions src/components/common/Post/Reply.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useState, useRef } from 'react';
import { useEffect, useState, useRef } from 'react';
import PostHeader from './PostHeader';
import type { CommentData } from '@/api/comments/getComments';
import like from '../../../assets/feed/like.svg';
import activeLike from '../../../assets/feed/activeLike.svg';
import { useReplyActions } from '@/hooks/useReplyActions';
import { usePopupActions } from '@/hooks/usePopupActions';
import { usePreventDoubleClick } from '@/hooks/usePreventDoubleClick';
import { postLike } from '@/api/comments/postLike';
import { deleteComment } from '@/api/comments/deleteComment';
import { DeletedContainer, Container, ReplySection } from './Reply.styled';
Expand Down Expand Up @@ -32,27 +33,46 @@ const Reply = ({
const [liked, setLiked] = useState(isLike);
const [likeCount, setLikeCount] = useState<number>(initialLikeCount);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const containerRef = useRef<HTMLDivElement>(null);
const { isLoading: isLikeLoading, run: runLike } = usePreventDoubleClick();

const { startReply } = useReplyActions();
const { openMoreMenu, closePopup, openSnackbar } = usePopupActions();

const handleLike = async () => {
try {
const response = await postLike(commentId, !liked);
useEffect(() => {
setLiked(isLike);
setLikeCount(initialLikeCount);
}, [isLike, initialLikeCount]);

if (response.isSuccess) {
setLiked(response.data.isLiked);
setLikeCount(prev => (response.data.isLiked ? prev + 1 : prev - 1));
} else {
const handleLike = () => {
runLike(async () => {
const previousLiked = liked;
const previousLikeCount = likeCount;
const nextLiked = !liked;

setLiked(nextLiked);
setLikeCount(prev => (nextLiked ? prev + 1 : prev - 1));

try {
const response = await postLike(commentId, nextLiked);
if (!response.isSuccess) {
setLiked(previousLiked);
setLikeCount(previousLikeCount);
openSnackbar({
message: response.message || '좋아요 처리 중 오류가 발생했습니다.',
variant: 'top',
onClose: () => {},
});
}
} catch {
setLiked(previousLiked);
setLikeCount(previousLikeCount);
openSnackbar({
message: response.message || '좋아요 처리 중 오류가 발생했습니다.',
message: '좋아요 처리 중 오류가 발생했습니다.',
variant: 'top',
onClose: () => {},
});
}
} catch (error) {
console.error('좋아요 상태 변경 실패:', error);
}
});
};

const handleReplyClick = () => {
Expand Down Expand Up @@ -163,6 +183,7 @@ const Reply = ({
handleLike();
}}
alt="좋아요"
style={{ opacity: isLikeLoading ? 0.6 : 1 }}
/>
<div className="count">{likeCount}</div>
</div>
Expand Down
46 changes: 33 additions & 13 deletions src/components/common/Post/SubReply.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useState, useRef } from 'react';
import { useEffect, useState, useRef } from 'react';
import PostHeader from './PostHeader';
import type { ReplyData } from '@/api/comments/getComments';
import like from '../../../assets/feed/like.svg';
import activeLike from '../../../assets/feed/activeLike.svg';
import replyIcon from '../../../assets/feed/replyIcon.svg';
import { useReplyActions } from '@/hooks/useReplyActions';
import { usePopupActions } from '@/hooks/usePopupActions';
import { usePreventDoubleClick } from '@/hooks/usePreventDoubleClick';
import { postLike } from '@/api/comments/postLike';
import { deleteComment } from '@/api/comments/deleteComment';
import { DeletedContainer, Container, ReplyIcon, Content, ReplySection } from './SubReply.styled';
Expand Down Expand Up @@ -34,32 +35,50 @@ const SubReply = ({
const [liked, setLiked] = useState<boolean>(isLike);
const [currentLikeCount, setCurrentLikeCount] = useState<number>(likeCount);
const containerRef = useRef<HTMLDivElement>(null);
const { isLoading: isLikeLoading, run: runLike } = usePreventDoubleClick();

const { startReply } = useReplyActions();
const { openMoreMenu, closePopup, openSnackbar } = usePopupActions();

useEffect(() => {
setLiked(isLike);
setCurrentLikeCount(likeCount);
}, [isLike, likeCount]);

const handleReplyClick = () => {
startReply(creatorNickname, commentId);
};

const handleLike = async () => {
try {
const response = await postLike(commentId, !liked);
const handleLike = () => {
runLike(async () => {
const previousLiked = liked;
const previousLikeCount = currentLikeCount;
const nextLiked = !liked;

if (response.isSuccess) {
setLiked(response.data.isLiked);
setCurrentLikeCount(prev => (response.data.isLiked ? prev + 1 : prev - 1));
} else {
console.error('좋아요 상태 변경 실패:', response.message);
setLiked(nextLiked);
setCurrentLikeCount(prev => (nextLiked ? prev + 1 : prev - 1));

try {
const response = await postLike(commentId, nextLiked);
if (!response.isSuccess) {
setLiked(previousLiked);
setCurrentLikeCount(previousLikeCount);
openSnackbar({
message: response.message || '좋아요 처리 중 오류가 발생했습니다.',
variant: 'top',
onClose: () => {},
});
}
} catch {
setLiked(previousLiked);
setCurrentLikeCount(previousLikeCount);
openSnackbar({
message: response.message || '좋아요 처리 중 오류가 발생했습니다.',
message: '좋아요 처리 중 오류가 발생했습니다.',
variant: 'top',
onClose: () => {},
});
}
} catch (error) {
console.error('좋아요 상태 변경 실패:', error);
}
});
};

const handleDelete = async () => {
Expand Down Expand Up @@ -170,6 +189,7 @@ const SubReply = ({
handleLike();
}}
alt="좋아요"
style={{ opacity: isLikeLoading ? 0.6 : 1 }}
/>
<div className="count">{currentLikeCount}</div>
</div>
Expand Down
Loading