-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 공연을 추가할 때 출연진 정보 순서 변경이 올바르게 동작하도록 수정
- Loading branch information
Showing
6 changed files
with
203 additions
and
182 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
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,70 @@ | ||
import { useChangeCastTeamOrder } from "@boolti/api"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { TempShowCastInfoFormInput } from "~/components/ShowCastInfoFormDialogContent"; | ||
|
||
interface UseCastTeamListOrderParams { | ||
showId?: number; | ||
castTeamList?: TempShowCastInfoFormInput[]; | ||
onChange?: () => void; | ||
} | ||
|
||
const useCastTeamListOrder = (params?: UseCastTeamListOrderParams) => { | ||
const showId = params?.showId; | ||
const castTeamList = params?.castTeamList; | ||
const onChange = params?.onChange; | ||
|
||
const [castTeamListDraft, setCastTeamListDraft] = useState<TempShowCastInfoFormInput[]>([]); | ||
|
||
const changeCastTeamOrder = useChangeCastTeamOrder(); | ||
|
||
const changeCastTeamIndex = useCallback((draggedItemId: number, targetIndex: number) => { | ||
setCastTeamListDraft((prevDraft) => { | ||
if (!prevDraft) return prevDraft; | ||
|
||
const draggedItemIndex = prevDraft.findIndex(({ id }) => id === draggedItemId); | ||
if (draggedItemIndex === -1 || targetIndex < 0 || targetIndex >= prevDraft.length) { | ||
return prevDraft; | ||
} | ||
|
||
const nextDraft = [...prevDraft]; | ||
const [draggedItem] = nextDraft.splice(draggedItemIndex, 1); | ||
nextDraft.splice(targetIndex, 0, draggedItem); | ||
|
||
return nextDraft; | ||
}) | ||
}, []) | ||
|
||
const castTeamDropHoverHandler = useCallback((draggedItemId: number, hoverIndex: number) => { | ||
changeCastTeamIndex(draggedItemId, hoverIndex); | ||
}, [changeCastTeamIndex]); | ||
|
||
const castTeamDropHandler = useCallback(async () => { | ||
if (!castTeamListDraft) return; | ||
|
||
if (showId !== undefined) { | ||
await changeCastTeamOrder.mutateAsync({ | ||
showId, | ||
body: { | ||
castTeamIds: castTeamListDraft.map(({ id }) => id), | ||
}, | ||
}); | ||
} | ||
|
||
onChange?.(); | ||
}, [castTeamListDraft, changeCastTeamOrder, onChange, showId]) | ||
|
||
useEffect(() => { | ||
if (!castTeamList) return; | ||
|
||
setCastTeamListDraft(castTeamList); | ||
}, [castTeamList]) | ||
|
||
return { | ||
castTeamListDraft, | ||
setCastTeamListDraft, | ||
castTeamDropHoverHandler, | ||
castTeamDropHandler, | ||
} | ||
} | ||
|
||
export default useCastTeamListOrder |
Oops, something went wrong.