-
Notifications
You must be signed in to change notification settings - Fork 4
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
[Feat/#250] 탭투탭에서 블록이 겹쳐서 쌓이는 문제 해결 #251
Changes from all commits
d084000
1abd347
647955d
3cc4c47
41a251a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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. p4) key가 [0,1,2] 를 반환한다면 Math.max(...keys)+1 을 사용해서 연산을 한 번 더 하는 것보다,
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.
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 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; |
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.
p4) 이 두 부분이 slot에 해당하는 date 즉 날짜를 가져오는 부분으로 이해했는데 이 두가지 변수가 다른 경우의 수가 있나요? 현재 저희 서비스에서는 날짜가 넘어가는 로직을 다루고 있진 않은 것 같아서요!
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.
handleCompleteSlot()
에서는 시작시간(startSlot)이 선택된 상태에서 종료시간(endSlot)을 선택했을 때 그게 유효한 선택인지를 확인해야합니다.그래서 startSlot과 endSlot의 날짜가 일치한지를 비교해야합니다! 시작 시간은 월요일에 찍어두고 종료시간을 수요일에 찍으면 블럭이 만들어질 수 없으니까, 그렇지 않은 경우에만 블록을 생성하는 겁니다.
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.
아하! 클릭한 두 날짜가 같아야만 블록을 생성하는 로직이군요! 이해했습니다. 감사합니다.