Skip to content

Commit

Permalink
refactor: 캘린더 날짜의 변화값을 구하는 유틸함수의 이름명 변경, 내부 변수 상수화
Browse files Browse the repository at this point in the history
  • Loading branch information
wzrabbit committed Nov 13, 2023
1 parent 43fb55d commit 6d31c42
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions frontend/src/utils/generateScheduleBarsByMousePoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateScheduleBars } from '~/utils/generateScheduleBars';
import { CALENDAR, ONE_DAY } from '~/constants/calendar';
import type { Schedule } from '~/types/schedule';
import type { CalendarSize } from '~/types/size';

Expand All @@ -14,36 +15,6 @@ interface GenerateScheduleBarsByMousePointProps {
calendarSize: CalendarSize;
}

const getCalendarGridDifference = (
relativeX: number,
relativeY: number,
calendarWidth: number,
calendarHeight: number,
) => {
const rowDifference = Math.round((relativeY * 6) / calendarHeight);
const columnDifference = Math.round((relativeX * 7) / calendarWidth);
const calculatedDifference = rowDifference * 7 + columnDifference;

return calculatedDifference;
};

const changeDateTimeByDays = (
dateTime: Schedule['startDateTime'],
days: number,
) => {
const changedDate = new Date(Number(new Date(dateTime)) + 86_400_000 * days);

const year = String(changedDate.getFullYear()).padStart(4, '0');
const month = String(changedDate.getMonth() + 1).padStart(2, '0');
const day = String(changedDate.getDate()).padStart(2, '0');
const time = dateTime.split(' ')[1];
const [minute, second] = time.split(':');

const changedDateTime: Schedule['startDateTime'] = `${year}-${month}-${day} ${minute}:${second}`;

return changedDateTime;
};

export const generateScheduleBarsByMousePoint = (
params: GenerateScheduleBarsByMousePointProps,
) => {
Expand All @@ -59,7 +30,7 @@ export const generateScheduleBarsByMousePoint = (
calendarSize,
} = params;

const difference = getCalendarGridDifference(
const difference = getCalendarDateDifferenceByMousePoint(
relativeX,
relativeY,
calendarWidth,
Expand All @@ -80,3 +51,38 @@ export const generateScheduleBarsByMousePoint = (

return generatedScheduleBars;
};

const getCalendarDateDifferenceByMousePoint = (
relativeX: number,
relativeY: number,
calendarWidth: number,
calendarHeight: number,
) => {
const rowDifference = Math.round(
(relativeY * CALENDAR.ROW_SIZE) / calendarHeight,
);
const columnDifference = Math.round(
(relativeX * CALENDAR.COLUMN_SIZE) / calendarWidth,
);
const calculatedDifference =
rowDifference * CALENDAR.COLUMN_SIZE + columnDifference;

return calculatedDifference;
};

const changeDateTimeByDays = (
dateTime: Schedule['startDateTime'],
days: number,
) => {
const changedDate = new Date(Number(new Date(dateTime)) + ONE_DAY * days);

const year = String(changedDate.getFullYear()).padStart(4, '0');
const month = String(changedDate.getMonth() + 1).padStart(2, '0');
const day = String(changedDate.getDate()).padStart(2, '0');
const time = dateTime.split(' ')[1];
const [minute, second] = time.split(':');

const changedDateTime: Schedule['startDateTime'] = `${year}-${month}-${day} ${minute}:${second}`;

return changedDateTime;
};

0 comments on commit 6d31c42

Please sign in to comment.