-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 나의 크루 관련 API 연동 구현 - 컴포넌트 분리 - 대시보드 레이아웃 개선
- Loading branch information
Showing
13 changed files
with
269 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
File renamed without changes.
This file contains 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,21 @@ | ||
import CreateCrewIcon from "@assets/icons/crew-create-button-icon.svg?react" | ||
|
||
interface MyCrewHeaderProps { | ||
openCreateModal: () => void | ||
} | ||
|
||
export default function MyCrewHeader(props: MyCrewHeaderProps) { | ||
const { openCreateModal } = props | ||
return ( | ||
<div className="mb-[24px] flex w-full items-center"> | ||
<div className="flex-grow text-[22px] font-bold text-zinc-900">나의 크루</div> | ||
<div | ||
className="flex w-[138px] cursor-pointer items-center justify-center gap-[10px] rounded-[33px] bg-zinc-800 p-[10px] text-sm font-semibold text-white" | ||
onClick={openCreateModal} | ||
> | ||
<CreateCrewIcon /> | ||
<div>크루 만들기</div> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains 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 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 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,76 @@ | ||
import { getGroupScores, getMyGroup, groupUserRank, MyGroupData, withdrawMyGroup } from "@/api" | ||
import { useAuthStore } from "@/store" | ||
import { useMyGroupStore } from "@/store/MyGroup" | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" | ||
import { useCallback, useEffect, useMemo } from "react" | ||
|
||
export default function useMyGroup() { | ||
const queryClient = useQueryClient() | ||
const myName = useAuthStore((state) => state.user.nickname) | ||
const { myGroupData, setMyGroupData } = useMyGroupStore() | ||
|
||
// Fetch group data | ||
const { data, isLoading, error } = useQuery<{ data: MyGroupData } | null, Error>({ | ||
queryKey: ["myGroup"], | ||
queryFn: getMyGroup, | ||
staleTime: 60 * 1000, | ||
retry: false, | ||
}) | ||
|
||
// Fetch group scores | ||
const myGroupScoresQuery = useQuery<{ data: { ranks: groupUserRank[] } }, Error>({ | ||
queryKey: ["groupScores", myGroupData?.id], | ||
queryFn: () => getGroupScores(myGroupData!.id), | ||
enabled: !!myGroupData?.id, | ||
staleTime: 60 * 1000, // Consider data fresh for 1 minute | ||
}) | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setMyGroupData(data.data) | ||
} | ||
}, [data, setMyGroupData]) | ||
|
||
const myRank = useMemo(() => { | ||
return myGroupScoresQuery.data?.data.ranks.find((item) => item.name === myName) | ||
}, [myGroupScoresQuery.data, myName]) | ||
|
||
const refetchAll = () => { | ||
queryClient.invalidateQueries({ queryKey: ["myGroup"] }) | ||
queryClient.invalidateQueries({ queryKey: ["groupScores"] }) | ||
} | ||
|
||
const withdrawMutation = useMutation({ | ||
mutationFn: withdrawMyGroup, | ||
onSuccess: (res) => { | ||
if (res.status === 204) { | ||
setMyGroupData(null) | ||
queryClient.setQueryData(["myGroup"], null) | ||
queryClient.removeQueries({ queryKey: ["groupScores"] }) | ||
} | ||
}, | ||
}) | ||
|
||
const withdrawFromGroup = useCallback(async () => { | ||
if (myGroupData) { | ||
try { | ||
await withdrawMutation.mutateAsync(myGroupData.id) | ||
return { success: true } | ||
} catch (error) { | ||
console.error("Error during group withdrawal:", error) | ||
return { success: false, error } | ||
} | ||
} | ||
return { success: false, error: new Error("No group data available") } | ||
}, [myGroupData, withdrawMutation]) | ||
|
||
return { | ||
myGroupData: myGroupData ?? data?.data, | ||
ranks: myGroupScoresQuery.data?.data.ranks ?? [], | ||
myRank, | ||
isLoading: isLoading || myGroupScoresQuery.isLoading, | ||
error: error || myGroupScoresQuery.error, | ||
withdrawFromGroup, | ||
refetchAll, | ||
} | ||
} |
This file contains 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 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.