-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: 프로젝트 삭제 로직 useDeleteProjectsMutation 훅으로 통합 및 리팩토링 #74
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
Merged
tkyoun0421
merged 3 commits into
amicable-development-center:develop
from
namee-h:feat/profile
Jun 28, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
src/entities/projects/hooks/useDeleteProjectsMutation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import type { UseMutationResult } from "@tanstack/react-query"; | ||
|
|
||
| import { deleteApplication } from "@entities/projects/api/getProjectApplicationsApi"; | ||
| import { deleteUserLikes } from "@entities/projects/api/getProjectLikeApi"; | ||
| import { deleteProjectsEverywhere } from "@entities/projects/api/projectsApi"; | ||
|
|
||
| import queryKeys from "@shared/react-query/queryKey"; | ||
| import { useLikeStore } from "@shared/stores/likeStore"; | ||
| import { useProjectStore } from "@shared/stores/projectStore"; | ||
| import { useSnackbarStore } from "@shared/stores/snackbarStore"; | ||
| import { ProjectCollectionTabType } from "@shared/types/project"; | ||
| import type { ProjectListRes } from "@shared/types/project"; | ||
|
|
||
| interface DeleteProjectsParams { | ||
| type: ProjectCollectionTabType; | ||
| ids: string[]; | ||
| user: { uid: string } | null; | ||
| appliedProjectsData?: ProjectListRes[]; | ||
| myLikedProjectsData?: ProjectListRes[]; | ||
| } | ||
|
|
||
| const ERROR_MSG = "프로젝트 삭제에 실패했습니다."; | ||
|
|
||
| export const useDeleteProjectsMutation = (): UseMutationResult< | ||
| void, | ||
| unknown, | ||
| DeleteProjectsParams | ||
| > => { | ||
| const queryClient = useQueryClient(); | ||
| const { removeLikeProjects } = useLikeStore(); | ||
| const { setAppliedProjects, setLikeProjects } = useProjectStore(); | ||
| const { showSuccess, showError } = useSnackbarStore(); | ||
|
|
||
| // 관심 프로젝트 삭제 | ||
| const deleteLikes = async ( | ||
| userUid: string, | ||
| ids: string[], | ||
| myLikedProjectsData?: ProjectListRes[] | ||
| ): Promise<void> => { | ||
| await deleteUserLikes(userUid, ids); | ||
|
|
||
| // 전역 상태 동기화 | ||
| removeLikeProjects(ids); | ||
| setLikeProjects( | ||
| myLikedProjectsData?.filter((p: ProjectListRes) => !ids.includes(p.id)) || | ||
| [] | ||
| ); | ||
|
|
||
| showSuccess("관심 프로젝트가 삭제되었습니다."); | ||
| }; | ||
|
|
||
| // 지원한 프로젝트 삭제 | ||
| const deleteApplied = async ( | ||
| userUid: string, | ||
| ids: string[], | ||
| appliedProjectsData?: ProjectListRes[] | ||
| ): Promise<void> => { | ||
| // applications 컬렉션에서 제거 (병렬 처리) | ||
| const deletePromises = ids.map((projectId) => | ||
| deleteApplication(userUid, projectId) | ||
| ); | ||
| await Promise.all(deletePromises); | ||
|
|
||
| // 전역 상태 동기화 | ||
| setAppliedProjects( | ||
| appliedProjectsData?.filter((p: ProjectListRes) => !ids.includes(p.id)) || | ||
| [] | ||
| ); | ||
|
|
||
| showSuccess("지원한 프로젝트가 삭제되었습니다."); | ||
| }; | ||
|
|
||
| // 만든 프로젝트 삭제 | ||
| const deleteCreated = async ( | ||
| userUid: string, | ||
| ids: string[], | ||
| appliedProjectsData?: ProjectListRes[], | ||
| myLikedProjectsData?: ProjectListRes[] | ||
| ): Promise<void> => { | ||
| const res = await deleteProjectsEverywhere(ids, userUid); | ||
|
|
||
| if (!res.success) { | ||
| showError(res.error || ERROR_MSG); | ||
| throw new Error(res.error || ERROR_MSG); | ||
| } | ||
|
|
||
| // 전역 상태 동기화 | ||
| setAppliedProjects( | ||
| appliedProjectsData?.filter((p: ProjectListRes) => !ids.includes(p.id)) || | ||
| [] | ||
| ); | ||
| setLikeProjects( | ||
| myLikedProjectsData?.filter((p: ProjectListRes) => !ids.includes(p.id)) || | ||
| [] | ||
| ); | ||
| removeLikeProjects(ids); | ||
|
|
||
| showSuccess("만든 프로젝트가 삭제되었습니다."); | ||
| }; | ||
|
|
||
| // 쿼리 무효화 함수들 | ||
| const invalidateLikeQueries = async (): Promise<void> => { | ||
| const queries = [ | ||
| [queryKeys.myLikedProjects, "details"], | ||
| [queryKeys.myLikedProjects, "ids"], | ||
| [queryKeys.projectLike], | ||
| [queryKeys.projectLikedUser], | ||
| [queryKeys.projects], // 홈페이지, 프로젝트 찾기 페이지 동기화 | ||
| ]; | ||
|
|
||
| await Promise.all( | ||
| queries.map((queryKey) => queryClient.invalidateQueries({ queryKey })) | ||
| ); | ||
| }; | ||
|
Comment on lines
+103
to
+115
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. 호오 그러쿤욥 ! 사실 이 부분이 이전에 동기처리로 되어있었는데 너무 복잡해보여서 바꿨습니당..ㅎㅎ |
||
|
|
||
| const invalidateAppliedQueries = async (): Promise<void> => { | ||
| const queries = [ | ||
| [queryKeys.myAppliedProjects, "details"], | ||
| [queryKeys.myAppliedProjects, "ids"], | ||
| [queryKeys.projectAppliedUser], | ||
| ]; | ||
|
|
||
| await Promise.all( | ||
| queries.map((queryKey) => queryClient.invalidateQueries({ queryKey })) | ||
| ); | ||
| }; | ||
|
|
||
| const invalidateCreatedQueries = async (userUid: string): Promise<void> => { | ||
| const queries = [ | ||
| [queryKeys.myLikedProjects, "details"], | ||
| [queryKeys.myAppliedProjects, "details"], | ||
| [queryKeys.projects], | ||
| ["userProfile", userUid], | ||
| [queryKeys.projectLike], | ||
| [queryKeys.projectLikedUser], | ||
| [queryKeys.projectAppliedUser], | ||
| ]; | ||
|
|
||
| await Promise.all( | ||
| queries.map((queryKey) => queryClient.invalidateQueries({ queryKey })) | ||
| ); | ||
| }; | ||
|
|
||
| // 타입별 쿼리 무효화 | ||
| const invalidateQueries = async ( | ||
| type: ProjectCollectionTabType, | ||
| user?: { uid: string } | null | ||
| ): Promise<void> => { | ||
| switch (type) { | ||
| case ProjectCollectionTabType.Likes: | ||
| await invalidateLikeQueries(); | ||
| break; | ||
| case ProjectCollectionTabType.Applied: | ||
| await invalidateAppliedQueries(); | ||
| break; | ||
| case ProjectCollectionTabType.Created: | ||
| if (user) { | ||
| await invalidateCreatedQueries(user.uid); | ||
| } | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| // 메인 삭제 로직 | ||
| const handleDelete = async ({ | ||
| type, | ||
| ids, | ||
| user, | ||
| appliedProjectsData, | ||
| myLikedProjectsData, | ||
| }: DeleteProjectsParams): Promise<void> => { | ||
| if (!user) { | ||
| throw new Error("로그인이 필요합니다."); | ||
| } | ||
|
|
||
| switch (type) { | ||
| case ProjectCollectionTabType.Likes: | ||
| await deleteLikes(user.uid, ids, myLikedProjectsData); | ||
| break; | ||
| case ProjectCollectionTabType.Applied: | ||
| await deleteApplied(user.uid, ids, appliedProjectsData); | ||
| break; | ||
| case ProjectCollectionTabType.Created: | ||
| await deleteCreated( | ||
| user.uid, | ||
| ids, | ||
| appliedProjectsData, | ||
| myLikedProjectsData | ||
| ); | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| return useMutation<void, unknown, DeleteProjectsParams>({ | ||
| mutationFn: handleDelete, | ||
| onSuccess: async (_data, variables): Promise<void> => { | ||
| await invalidateQueries(variables.type, variables.user); | ||
| }, | ||
| onError: (error: any): void => { | ||
| showError(error?.message || ERROR_MSG); | ||
| }, | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
오호.. 랜덤 ID가 아닌 부분이 이런 장점이 있었네여
firebase 또 쓸 일이 있다면 써먹을 수 있을 것 같네요 배워갑니다 💯