-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: 좋아요 에러 개선 및 공유하기 기능 추가 #52
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { useState } from "react"; | ||
| import { useParams } from "react-router-dom"; | ||
|
|
||
| import useProjectLike from "@features/projects/queries/useProjectLike"; | ||
| import useProjectUnLike from "@features/projects/queries/useProjectUnLike"; | ||
|
|
@@ -7,48 +8,43 @@ import { useAuthStore } from "@shared/stores/authStore"; | |
|
|
||
| interface LikeResult { | ||
| isLike: boolean; | ||
| like: () => void; | ||
| unlike: () => void; | ||
| likeFn: () => void; | ||
| } | ||
|
|
||
| const useLike = ({ | ||
| projectID, | ||
| likedUsers, | ||
| }: { | ||
| projectID: string; | ||
| likedUsers: string[]; | ||
| }): LikeResult => { | ||
| const useLike = ({ likedUsers }: { likedUsers: string[] }): LikeResult => { | ||
| const { id: projectID } = useParams(); | ||
| const user = useAuthStore((state) => state.user); | ||
| const { mutate: updateLike, isPending: likePending } = useProjectLike(); | ||
| const { mutate: updateUnLike, isPending: unLikePending } = useProjectUnLike(); | ||
|
|
||
| const initLike = user && likedUsers.includes(user.uid); | ||
| const [isLike, setIsLike] = useState(initLike || false); | ||
|
|
||
| const like = (): void => { | ||
| const updateLikeStatus = (): void => { | ||
| if (!projectID) return; | ||
| if (likePending || unLikePending) { | ||
| alert("동작이 너무 빠릅니다. 잠시 후에 시도해주십시오."); | ||
|
|
||
| if (!user) { | ||
| alert("로그인 해주세요."); | ||
| return; | ||
| } | ||
| setIsLike(true); | ||
| updateLike(projectID); | ||
| }; | ||
|
|
||
| const unlike = (): void => { | ||
| if (!projectID) return; | ||
| if (likePending || unLikePending) { | ||
| alert("동작이 너무 빠릅니다. 잠시 후에 시도해주십시오."); | ||
| return; | ||
| } | ||
| setIsLike(false); | ||
| updateUnLike(projectID); | ||
|
|
||
| if (!isLike) { | ||
| setIsLike(true); | ||
| updateLike(projectID); | ||
| } else { | ||
| setIsLike(false); | ||
| updateUnLike(projectID); | ||
| } | ||
|
Comment on lines
+36
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 비슷한 로직인 것 같아서 하나의 함수에서 관리하는 게 좋을 것 같다고 생각이 들어서 고민해보다가
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그쵸 ㅠㅠ api 부터 합칠까 고민했는데 |
||
| }; | ||
|
|
||
| return { | ||
| isLike, | ||
| like, | ||
| unlike, | ||
| likeFn: updateLikeStatus, | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,10 @@ export const formatDate = (date?: string | Timestamp): string => { | |
| } | ||
| return date; | ||
| }; | ||
|
|
||
| export const shareProjectUrl = (): void => { | ||
| navigator.clipboard | ||
| .writeText(window.location.href) | ||
| .then(() => alert("URL이 복사되었습니다.")) | ||
| .catch(() => alert("복사 실패")); | ||
|
Comment on lines
+21
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 웹 API네요 또 하나 배워갑니다 :) |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 isLike 상태를 useState로만 관리하고 있는데,
유저 프로필에서 프로젝트 리스트 삭제 기능을 추가하면서 관심/좋아요 리스트를 전역상태 관리로 바꿨는데
프로필에서 관심 프로젝트를 삭제하면 디테일 페이지의 UI와 상태가 불일치 할 수도? 있을 것 같아요🥲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
관심/좋아요 리스트를 전역상태로 바꿧다는 말은 api 호출로 db값을 수정하지않는다는 말씀이신가요 ?
상세페이지의 좋아요 여부 체크는 게시글(projects 컬렉션)에 등록된 좋아요한 유저 목록을 api로 불러와 현재 로그인 한 사용자의 id와 비교하여 보여주므로 프로필의 전역상태와는 무관할 것 같습니다 😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엇 아니요 ! 전역 상태만 바꾸는 게 아니라, 반드시 API로 DB(파이어베이스) 값을 먼저 수정하고,
그 결과를 전역 상태에 반영해서 UI를 동기화한다는 말이었어요 !
상세페이지와 프로필 등 여러 곳에서 관심/좋아요 상태가 항상 일치하려면,
DB와 전역 상태를 항상 동기화하는 구조가 안전하지 않을까 생각합니다 !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아하 네~
상세 페이지에서는 항상 최신 상태의 db값을 들고오며 user 프로필에서 uid 값 이외엔 사용하지 않으니 수정하지 않아도 괜찮을 것 같습니다 😉