Skip to content
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

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function OverallScheduleTable({
availableDates,
dataOverallSchedule,
}: OverallScheduleTableProps) {
const [clickedSlot, setClickedSlot] = useState<string | undefined>(undefined);
const [clickedSlot, setClickedSlot] = useState<string | null>(null);
const [clickedUserNames, setClickedUserNames] = useState<string[]>([]);

const getAvailableTimesPerDate = (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/OverallSchedule/contexts/useClickContext.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Dispatch, SetStateAction, createContext, useContext } from 'react';

interface ClickContextType {
clickedSlot: string | undefined;
clickedSlot: string | null;
setClickedSlot: Dispatch<SetStateAction<ClickContextType['clickedSlot']>>;
clickedUserNames: string[];
setClickedUserNames: Dispatch<SetStateAction<ClickContextType['clickedUserNames']>>;
}

export const ClickContext = createContext<ClickContextType>({
clickedSlot: undefined,
setClickedSlot: () => undefined,
clickedSlot: null,
setClickedSlot: () => null,
clickedUserNames: [],
setClickedUserNames: () => [],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SelectContext, SelectedSlotType } from '../contexts/useSelectContext';
import { StepSlotsType, StepbottomItemsType } from '../types';

function SelectScheduleTable({ timeSlots, availableDates }: TimetableStructure) {
const [startSlot, setStartSlot] = useState<string | undefined>(undefined);
const [startSlot, setStartSlot] = useState<string | null>(null);
const [selectedSlots, setSelectedSlots] = useState<SelectedSlotType>({});

const { scheduleStep } = useScheduleStepContext();
Expand Down
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('/'));
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4) 이 두 부분이 slot에 해당하는 date 즉 날짜를 가져오는 부분으로 이해했는데 이 두가지 변수가 다른 경우의 수가 있나요? 현재 저희 서비스에서는 날짜가 넘어가는 로직을 다루고 있진 않은 것 같아서요!

Copy link
Member Author

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의 날짜가 일치한지를 비교해야합니다! 시작 시간은 월요일에 찍어두고 종료시간을 수요일에 찍으면 블럭이 만들어질 수 없으니까, 그렇지 않은 경우에만 블록을 생성하는 겁니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하! 클릭한 두 날짜가 같아야만 블록을 생성하는 로직이군요! 이해했습니다. 감사합니다.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4) key가 [0,1,2] 를 반환한다면 Math.max(...keys)+1 을 사용해서 연산을 한 번 더 하는 것보다,
keys.length을 활용해 아래처럼 활용하는 것은 어려울까요??
key값이 0부터 시작하는 것이 아니라면 반려해주세요!

const newKey = keys.length ? newKey + 1 : 0;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selectedSlots내의 객체들은 계속해서 생성됐다 삭제되기때문에 length가 마지막 key를 의미하지는 않습니다! 그래서 최대값에 +1을 하는 방식을 사용했습니닷!

Copy link
Member

Choose a reason for hiding this comment

The 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;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ScheduleStepContextType {

export const ScheduleStepContext = createContext<ScheduleStepContextType>({
scheduleStep: 'selectTimeSlot',
setScheduleStep: () => undefined,
setScheduleStep: () => null,
});

export function useScheduleStepContext() {
Expand Down
8 changes: 4 additions & 4 deletions src/pages/selectSchedule/contexts/useSelectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export interface SelectedSlotType {
}

interface SelectContextType {
startSlot: string | undefined;
startSlot: string | null;
setStartSlot: Dispatch<SetStateAction<SelectContextType['startSlot']>>;
selectedSlots: SelectedSlotType;
setSelectedSlots: Dispatch<SetStateAction<SelectedSlotType>>;
}

export const SelectContext = createContext<SelectContextType>({
startSlot: undefined,
setStartSlot: () => undefined,
startSlot: null,
setStartSlot: () => null,
selectedSlots: {},
setSelectedSlots: () => undefined,
setSelectedSlots: () => null,
});

export function useSelectContext() {
Expand Down
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
Loading