-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…TapError [Feat/#250] 탭투탭에서 블록이 겹쳐서 쌓이는 문제 해결
- Loading branch information
Showing
7 changed files
with
78 additions
and
52 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
107 changes: 67 additions & 40 deletions
107
src/pages/selectSchedule/components/selectTimeSlot/hooks/useSlotSelection.ts
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 |
---|---|---|
@@ -1,52 +1,79 @@ | ||
import { useSelectContext } from 'pages/selectSchedule/contexts/useSelectContext'; | ||
|
||
const useSlotSeletion = () => { | ||
const {startSlot, setStartSlot, selectedSlots, setSelectedSlots} = useSelectContext(); | ||
const { startSlot, setStartSlot, selectedSlots, setSelectedSlots } = useSelectContext(); | ||
|
||
const handleSelectSlot = (targetSlot: string) => { | ||
setStartSlot(targetSlot); | ||
} | ||
const handleSelectSlot = (targetSlot: string) => { | ||
setStartSlot(targetSlot); | ||
}; | ||
|
||
const handleCompleteSlot = (targetSlot: string) => { | ||
const dateOfStartSlot = startSlot?.substring(0, startSlot.lastIndexOf('/')); | ||
const dateOfTargetSlot = targetSlot.substring(0, targetSlot.lastIndexOf('/')) | ||
if (startSlot && dateOfStartSlot === dateOfTargetSlot){ | ||
const newSelectedSlot = { | ||
date:dateOfStartSlot, | ||
startSlot:startSlot?.substring(startSlot.lastIndexOf('/')+1), | ||
endSlot:targetSlot.substring(targetSlot.lastIndexOf('/')+1), | ||
priority:0, | ||
} | ||
|
||
const keys = Object.keys(selectedSlots).map(Number) | ||
const newKey = keys.length ? Math.max(...keys) + 1 : 0; | ||
const newSelectedSlots = {...selectedSlots}; | ||
newSelectedSlots[newKey] = newSelectedSlot; | ||
setSelectedSlots(newSelectedSlots) | ||
} | ||
setStartSlot(undefined); | ||
} | ||
const handleCompleteSlot = (endSlot: string) => { | ||
const dateOfStartSlot = startSlot && startSlot.substring(0, startSlot.lastIndexOf('/')); | ||
const dateOfEndSlot = endSlot.substring(0, endSlot.lastIndexOf('/')); | ||
if (startSlot && dateOfStartSlot === dateOfEndSlot) { | ||
const newSelectedSlot = { | ||
date: dateOfStartSlot, | ||
startSlot: startSlot && startSlot.substring(startSlot.lastIndexOf('/') + 1), | ||
endSlot: endSlot.substring(endSlot.lastIndexOf('/') + 1), | ||
priority: 0, | ||
}; | ||
|
||
const keys = Object.keys(selectedSlots).map(Number); | ||
const newKey = keys.length ? Math.max(...keys) + 1 : 0; | ||
|
||
const handleDeleteSlot = (selectedEntryId: number) => { | ||
const newSelectedSlots = {...selectedSlots}; | ||
delete newSelectedSlots[selectedEntryId]; | ||
setSelectedSlots(newSelectedSlots); | ||
setSelectedSlots((prev) => { | ||
const newSelectedSlots = { ...prev }; | ||
newSelectedSlots[newKey] = newSelectedSlot; | ||
return newSelectedSlots; | ||
}); | ||
removeOverlappedSlots(endSlot, dateOfStartSlot); | ||
} | ||
setStartSlot(null); | ||
}; | ||
|
||
const onClickSlot = (targetSlot:string, selectedEntryId?:number)=>{ | ||
if (selectedEntryId !== undefined){ | ||
if (startSlot === undefined){ | ||
handleDeleteSlot(selectedEntryId); | ||
} | ||
setStartSlot(undefined) | ||
} else if (startSlot !== undefined){ | ||
handleCompleteSlot(targetSlot) | ||
} else { | ||
handleSelectSlot(targetSlot) | ||
const handleDeleteSlot = (selectedEntryId: number) => { | ||
setSelectedSlots((prev) => { | ||
const newSelectedSlots = { ...prev }; | ||
delete newSelectedSlots[selectedEntryId]; | ||
return newSelectedSlots; | ||
}); | ||
}; | ||
|
||
const removeOverlappedSlots = (endSlot: string, dateOfStartSlot: string) => { | ||
const selectedSlotsPerDate = Object.entries(selectedSlots).filter( | ||
([, slot]) => slot.date === dateOfStartSlot, | ||
); | ||
|
||
selectedSlotsPerDate.forEach( | ||
([id, { startSlot: selectedStartSlot, endSlot: selectedEndSlot }]) => { | ||
const currentStartSlotTime = startSlot && startSlot.split('/').pop(); | ||
const currentEndSlotTime = endSlot.split('/').pop(); | ||
if ( | ||
currentStartSlotTime && | ||
currentEndSlotTime && | ||
selectedStartSlot > currentStartSlotTime && | ||
selectedEndSlot < currentEndSlotTime | ||
) { | ||
handleDeleteSlot(Number(id)); | ||
} | ||
}, | ||
); | ||
}; | ||
|
||
const onClickSlot = (targetSlot: string, selectedEntryId?: number) => { | ||
if (selectedEntryId !== undefined) { | ||
if (startSlot === null) { | ||
handleDeleteSlot(selectedEntryId); | ||
} | ||
setStartSlot(null); | ||
} else if (startSlot !== null) { | ||
handleCompleteSlot(targetSlot); | ||
} else { | ||
handleSelectSlot(targetSlot); | ||
} | ||
}; | ||
|
||
return {startSlot, onClickSlot} | ||
} | ||
return { startSlot, onClickSlot }; | ||
}; | ||
|
||
export default useSlotSeletion | ||
export default useSlotSeletion; |
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