Skip to content

Commit

Permalink
feat: 겹치는 더 작은 블록이 있다면 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
simeunseo committed Jul 5, 2024
1 parent b9d8ba4 commit d084000
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
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;
3 changes: 1 addition & 2 deletions src/pages/selectSchedule/utils/changeApiReq.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ScheduleStates } from 'pages/legacy/selectSchedule/types/Schedule';
import {
HostAvailableSchduleRequestType,
UserAvailableScheduleRequestType,
} from 'src/types/createAvailableSchduleType';

import { ScheduleStates } from '../types/Schedule';

export const transformHostScheduleType = (
scheduleList: ScheduleStates[],
): (HostAvailableSchduleRequestType | null)[] => {
Expand Down

0 comments on commit d084000

Please sign in to comment.