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

[webclient] 시간대 필터 레거시 필드 제거 #205

Merged
merged 1 commit into from
Feb 26, 2025
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
4 changes: 4 additions & 0 deletions apps/snutt-webclient/src/entities/search.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type SearchTimeDto } from '@sf/snutt-api/src/apis/snutt-timetable/schemas';

import type { BaseLecture } from './lecture';
import type { Semester } from './semester';

Expand All @@ -11,6 +13,8 @@ export type SearchFilter = {
academicYear: string[];
category: string[];
categoryPre2025: string[];
times: SearchTimeDto[];
timesToExclude: SearchTimeDto[];
credit: number[];
department: string[];
classification: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export const implSearchSnuttApiRepository = ({
etc: filter.etc,
limit: filter.limit,
semester: filter.semester,
time_mask: filter.timeMask,
title: filter.title,
times: filter.times,
timesToExclude: filter.timesToExclude,
year: filter.year,
page: filter.page,
offset: filter.offset,
Expand Down
26 changes: 12 additions & 14 deletions apps/snutt-webclient/src/pages/main/main-searchbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type SearchTimeDto } from '@sf/snutt-api/src/apis/snutt-timetable/schemas';
import { type FormEvent, useState } from 'react';
import styled from 'styled-components';

Expand Down Expand Up @@ -32,21 +33,21 @@ export type SearchForm = {
etc: SearchFilter['etc'];
classification: SearchFilter['classification'];
department: SearchFilter['department'];
manualBitmask: number[];
timeType: 'auto' | 'manual' | null;
times: SearchFilter['times'];
timeType: 'manual' | 'auto' | null;
};

const initialForm = {
title: '',
academicYear: [],
credit: [],
etc: [],
category: [],
categoryPre2025: [],
classification: [],
department: [],
times: [],
timeType: null,
title: '',
manualBitmask: [],
};

const isInitialForm = (form: SearchForm) => JSON.stringify(initialForm) === JSON.stringify(form);
Expand Down Expand Up @@ -83,20 +84,17 @@ export const MainSearchbar = ({ onSearch, currentFullTimetable, resetSearchResul
year,
semester,
title: searchForm.title,
timeMask:
searchForm.timeType === 'manual'
? searchForm.manualBitmask
: searchForm.timeType === 'auto'
? currentFullTimetable
? timeMaskService.getTimetableEmptyTimeBitMask(currentFullTimetable)
: undefined
: undefined,
times: undefinedIfEmpty(searchForm.times),
timesToExclude:
searchForm.timeType === 'auto' && currentFullTimetable
? timeMaskService.getTimesByTimeTable(currentFullTimetable)
: undefined,
limit: 200,
},
});
};

const onChangeBitMask = (manualBitmask: number[]) => setSearchForm((sf) => ({ ...sf, manualBitmask }));
const onChangeTimes = (times: SearchTimeDto[]) => setSearchForm((sf) => ({ ...sf, times }));

const onChangeCheckbox = <
F extends 'academicYear' | 'category' | 'categoryPre2025' | 'classification' | 'credit' | 'department' | 'etc',
Expand Down Expand Up @@ -138,7 +136,7 @@ export const MainSearchbar = ({ onSearch, currentFullTimetable, resetSearchResul
onChangeCheckbox={onChangeCheckbox}
onChangeDepartment={(department) => setSearchForm((sf) => ({ ...sf, department }))}
onChangeTimeRadio={(timeType) => setSearchForm((sf) => ({ ...sf, timeType }))}
onChangeBitMask={onChangeBitMask}
onChangeTimes={onChangeTimes}
/>
</Form>
</Wrapper>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type SearchTimeDto } from '@sf/snutt-api/src/apis/snutt-timetable/schemas';
import { useQuery } from '@tanstack/react-query';
import { useState } from 'react';
import styled from 'styled-components';
Expand All @@ -24,9 +25,9 @@ type Props = {
field: F,
e: SearchForm[F][number],
) => void;
onChangeTimeRadio: (value: 'auto' | 'manual' | null) => void;
onChangeTimeRadio: (value: 'manual' | 'auto' | null) => void;
onChangeDepartment: (value: string[]) => void;
onChangeBitMask: (bm: number[]) => void;
onChangeTimes: (times: SearchTimeDto[]) => void;
};

export const MainSearchbarFilterDialog = ({
Expand All @@ -37,7 +38,7 @@ export const MainSearchbarFilterDialog = ({
searchForm,
onChangeCheckbox,
onChangeTimeRadio,
onChangeBitMask,
onChangeTimes,
onChangeDepartment,
}: Props) => {
const [isTimeModalOpen, setTimeModalOpen] = useState(false);
Expand Down Expand Up @@ -286,7 +287,7 @@ export const MainSearchbarFilterDialog = ({
<MainSearchbarFilterTimeSelectDialog
open={isTimeModalOpen}
onClose={() => setTimeModalOpen(false)}
onChangeBitMask={onChangeBitMask}
onChangeTimes={onChangeTimes}
/>
</StyledDialog>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type SearchTimeDto } from '@sf/snutt-api/src/apis/snutt-timetable/schemas';
import { useState } from 'react';
import styled from 'styled-components';

Expand All @@ -10,12 +11,16 @@ import { useGuardContext } from '@/hooks/useGuardContext';

import { MainSearchbarFilterTimeSelectCell } from './main-searchbar-filter-time-select-cell';

type Props = { open: boolean; onClose: () => void; onChangeBitMask: (bm: number[]) => void };
type Props = {
open: boolean;
onClose: () => void;
onChangeTimes: (times: SearchTimeDto[]) => void;
};

/**
* @note 테스트코드가 붙어있지 않습니다. 수정할 때 주의해 주세요!
*/
export const MainSearchbarFilterTimeSelectDialog = ({ open, onClose, onChangeBitMask }: Props) => {
export const MainSearchbarFilterTimeSelectDialog = ({ open, onClose, onChangeTimes }: Props) => {
const { timeMaskService, errorService } = useGuardContext(ServiceContext);
const [dragStart, setDragStart] = useState<Position | null>(null);
const [currentDrag, setCurrentDrag] = useState<Position | null>(null);
Expand All @@ -24,7 +29,7 @@ export const MainSearchbarFilterTimeSelectDialog = ({ open, onClose, onChangeBit
);

const onConfirm = () => {
onChangeBitMask(timeMaskService.getBitMask(cellStatus));
onChangeTimes(timeMaskService.getTimes(cellStatus));
onClose();
};

Expand Down
102 changes: 51 additions & 51 deletions apps/snutt-webclient/src/usecases/timeMaskService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { describe, expect, it, test } from 'vitest';

import type { FullTimetable } from '@/entities/timetable';
import { expect, test } from 'vitest';

import { getTimeMaskService } from './timeMaskService';

Expand Down Expand Up @@ -47,55 +45,57 @@ test('getUpdatedCellStatus', () => {
]);
});

test('getBitMask', () => {
expect(
timeMaskService.getBitMask([
[false, false, true],
[false, false, false],
]),
).toStrictEqual([0, 0, 2]);
// TODO: 테스트 수정

expect(
timeMaskService.getBitMask([
[false, false, true],
[false, false, false],
[false, true, false],
[true, false, false],
[false, true, false],
[false, true, false],
]),
).toStrictEqual([4, 11, 32]);
});
// test('getBitMask', () => {
// expect(
// timeMaskService.getBitMask([
// [false, false, true],
// [false, false, false],
// ]),
// ).toStrictEqual([0, 0, 2]);

describe('getTimetableEmptyTimeBitMask', () => {
it('empty case', () => {
expect(
timeMaskService.getTimetableEmptyTimeBitMask({ lecture_list: [] } as unknown as FullTimetable),
).toStrictEqual([1073741823, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823]);
});
// expect(
// timeMaskService.getBitMask([
// [false, false, true],
// [false, false, false],
// [false, true, false],
// [true, false, false],
// [false, true, false],
// [false, true, false],
// ]),
// ).toStrictEqual([4, 11, 32]);
// });

it('simple case', () => {
expect(
timeMaskService.getTimetableEmptyTimeBitMask({
lecture_list: [{ class_time_mask: [0, 536870912, 0, 0, 0, 0, 0] }],
} as unknown as FullTimetable),
).toStrictEqual([1073741823, 536870911, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823]);
});
// describe('getTimetableEmptyTimeBitMask', () => {
// it('empty case', () => {
// expect(
// timeMaskService.getTimetableEmptyTimeBitMask({ lecture_list: [] } as unknown as FullTimetable),
// ).toStrictEqual([1073741823, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823]);
// });

it('complex case', () => {
expect(
timeMaskService.getTimetableEmptyTimeBitMask({
lecture_list: [
{ class_time_mask: [4064, 0, 3584, 0, 0, 0, 0] },
{ class_time_mask: [28672, 0, 28672, 0, 0, 0, 0] },
{ class_time_mask: [0, 0, 786432, 0, 0, 0, 0] },
{ class_time_mask: [0, 229376, 0, 229376, 0, 0, 0] },
{ class_time_mask: [0, 3584, 0, 3584, 0, 0, 0] },
{ class_time_mask: [14680064, 0, 14680064, 0, 0, 0, 0] },
{ class_time_mask: [0, 28672, 0, 28672, 0, 0, 0] },
{ class_time_mask: [0, 192, 0, 192, 0, 0, 12582912] },
],
} as FullTimetable),
).toStrictEqual([1059029023, 1073479999, 1058243071, 1073479999, 1073741823, 1073741823, 1061158911]);
});
});
// it('simple case', () => {
// expect(
// timeMaskService.getTimetableEmptyTimeBitMask({
// lecture_list: [{ class_time_mask: [0, 536870912, 0, 0, 0, 0, 0] }],
// } as unknown as FullTimetable),
// ).toStrictEqual([1073741823, 536870911, 1073741823, 1073741823, 1073741823, 1073741823, 1073741823]);
// });

// it('complex case', () => {
// expect(
// timeMaskService.getTimetableEmptyTimeBitMask({
// lecture_list: [
// { class_time_mask: [4064, 0, 3584, 0, 0, 0, 0] },
// { class_time_mask: [28672, 0, 28672, 0, 0, 0, 0] },
// { class_time_mask: [0, 0, 786432, 0, 0, 0, 0] },
// { class_time_mask: [0, 229376, 0, 229376, 0, 0, 0] },
// { class_time_mask: [0, 3584, 0, 3584, 0, 0, 0] },
// { class_time_mask: [14680064, 0, 14680064, 0, 0, 0, 0] },
// { class_time_mask: [0, 28672, 0, 28672, 0, 0, 0] },
// { class_time_mask: [0, 192, 0, 192, 0, 0, 12582912] },
// ],
// } as FullTimetable),
// ).toStrictEqual([1059029023, 1073479999, 1058243071, 1073479999, 1073741823, 1073741823, 1061158911]);
// });
// });
52 changes: 40 additions & 12 deletions apps/snutt-webclient/src/usecases/timeMaskService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { CellStatus, DragMode, Position, TimeMask } from '@/entities/timeMask';
import { type SearchTimeDto } from '@sf/snutt-api/src/apis/snutt-timetable/schemas';

import type { CellStatus, DragMode, Position } from '@/entities/timeMask';
import type { FullTimetable } from '@/entities/timetable';

export interface TimeMaskService {
getInitialCellStatus(row: number, col: number): CellStatus;
getUpdatedCellStatus(prev: CellStatus, dragStart: Position, dragEnd: Position): CellStatus;
checkIsInArea(target: Position, from: Position, to: Position): boolean;
getDragMode(cellStatus: CellStatus, dragStart: Position): DragMode;
getBitMask(cellStatus: CellStatus): TimeMask;
getTimetableEmptyTimeBitMask(timetable: FullTimetable): TimeMask;
getTimes(cellStatus: CellStatus): SearchTimeDto[];
getTimesByTimeTable(timetable: FullTimetable): SearchTimeDto[];
}

export const getTimeMaskService = (): TimeMaskService => {
Expand All @@ -24,17 +26,43 @@ export const getTimeMaskService = (): TimeMaskService => {
),
checkIsInArea,
getDragMode,
getBitMask: (cellStatus) => {
getTimes: (cellStatus) => {
const transposed = cellStatus[0].map((_, j) => cellStatus.map((row) => row[j])); // 행-열 반전
return transposed.map((row) => row.map(Number).reduce((acc, cur) => acc * 2 + cur, 0)) as TimeMask;

const result = transposed.flatMap((cells, day) =>
cells.reduce((acc: SearchTimeDto[], cur, index) => {
if (!cur) return acc;

const startMinute = (index + 16) * 30;

const prev = acc.find((target) => target.endMinute === startMinute - 1);

return [
...acc.filter((target) => target.endMinute !== startMinute - 1),
{
day,
startMinute: prev?.startMinute || startMinute,
endMinute: startMinute + 30 - 1,
} as SearchTimeDto,
];
}, []),
);

return result;
},
getTimesByTimeTable: (timetable) => {
return timetable.lecture_list.flatMap((lecture) => {
return lecture.class_time_json.map((classTime) => {
const [startHour, startMinute] = classTime.start_time.split(':');
const [endHour, endMinute] = classTime.end_time.split(':');
return {
day: classTime.day,
startMinute: Number(startHour) * 60 + Number(startMinute),
endMinute: Number(endHour) * 60 + Number(endMinute),
};
});
});
},
getTimetableEmptyTimeBitMask: (timetable) =>
(timetable?.lecture_list ?? [])
.reduce<TimeMask>(
(acc, cur) => acc.map((mask, day) => mask | cur.class_time_mask[day]) as TimeMask,
[0, 0, 0, 0, 0, 0, 0],
)
.map((mask) => mask ^ 0x3fffffff) as TimeMask,
};
};

Expand Down
1 change: 0 additions & 1 deletion packages/snutt-api/src/apis/snutt-timetable/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export type SearchQueryLegacy = {
department?: string[];
category?: string[];
categoryPre2025?: string[];
time_mask?: Int32[];
times?: SearchTimeDto[];
timesToExclude?: SearchTimeDto[];
etc?: string[];
Expand Down
Loading