-
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.
- Loading branch information
Showing
2 changed files
with
66 additions
and
42 deletions.
There are no files selected for viewing
105 changes: 65 additions & 40 deletions
105
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,77 @@ | ||
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); | ||
const newSelectedSlots = { ...removeOverlappedSlots(endSlot, dateOfStartSlot) }; | ||
|
||
newSelectedSlots[newKey] = newSelectedSlot; | ||
setSelectedSlots(newSelectedSlots); | ||
} | ||
setStartSlot(undefined); | ||
}; | ||
|
||
const handleDeleteSlot = (selectedEntryId: number) => { | ||
const newSelectedSlots = { ...selectedSlots }; | ||
delete newSelectedSlots[selectedEntryId]; | ||
setSelectedSlots(newSelectedSlots); | ||
}; | ||
|
||
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 removeOverlappedSlots = (endSlot: string, dateOfStartSlot: string) => { | ||
const newSelectedSlots = { ...selectedSlots }; | ||
|
||
const selectedSlotsPerDate = Object.fromEntries( | ||
Object.entries(selectedSlots).filter(([, slot]) => slot.date === dateOfStartSlot), | ||
); | ||
Object.entries(selectedSlotsPerDate).forEach( | ||
([id, { startSlot: selectedStartSlot, endSlot: selectedEndSlot }]) => { | ||
const startSlotTime = startSlot && startSlot.split('/').pop(); | ||
const endSlotTime = endSlot.split('/').pop(); | ||
if ( | ||
startSlotTime && | ||
endSlotTime && | ||
selectedStartSlot > startSlotTime && | ||
selectedEndSlot < endSlotTime | ||
) { | ||
delete newSelectedSlots[parseInt(id)]; | ||
} | ||
}, | ||
); | ||
return newSelectedSlots; | ||
}; | ||
|
||
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); | ||
} | ||
}; | ||
|
||
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