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

授業追加時の重複チェックを実装 #665

Merged
merged 3 commits into from
Apr 2, 2023
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
46 changes: 36 additions & 10 deletions src/ui/pages/add/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
</p>
<div class="modal__courses">
<div
v-for="duplicateCourse in duplicateCourses"
v-for="duplicateCourse in duplicatedScheduleCourses"
:key="duplicateCourse.name"
class="duplicated-course"
>
Expand Down Expand Up @@ -286,8 +286,9 @@ import TextFieldSingleLine from "~/ui/components/TextFieldSingleLine.vue";
import ToggleButton from "~/ui/components/ToggleButton.vue";
import { useSwitch } from "~/ui/hooks/useSwitch";
import { addCoursesByCodes } from "~/ui/store/course";
import { displayToast } from "~/ui/store/toast";
import { getApplicableYear } from "~/ui/store/year";
import { deleteElementInArray } from "~/utils";
import { asyncFilter, deleteElementInArray } from "~/utils";
import type { Schedule } from "~/domain/schedule";
import type { DisplayCourse } from "~/presentation/viewmodels/course";

Expand Down Expand Up @@ -469,19 +470,44 @@ const buttonState = computed(() =>
selectedSearchResults.length > 0 ? "default" : "disabled"
);

const duplicateCourses = ref<DisplayCourse[]>([]);
const duplicatedScheduleCourses = ref<DisplayCourse[]>([]);

const addCourses = async (warning = true) => {
duplicateCourses.value = (
await Promise.all(
selectedSearchResults.filter(
({ schedules }) =>
!Usecase.checkScheduleDuplicate(ports)(year.value, schedules)
)
Comment on lines -476 to -480
Copy link
Member Author

Choose a reason for hiding this comment

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

以下の理由で常に !Usecase.checkScheduleDuplicate が false になっていました

await Promise.all(
  selectedSearchResults.filter(
    ({ schedules }) =>
      !Usecase.checkScheduleDuplicate(ports)(year.value, schedules)  // !Boolean(Promise<T>) なので常に false になる
  )
)

const registeredCourse = await Usecase.getRegisteredCoursesByYear(ports)(
year.value
);

if (isResultError(registeredCourse)) throw registeredCourse;

const duplicatedCourses = selectedSearchResults
.filter(({ course: { code: selectedCourseCode } }) => {
return registeredCourse.some(
({ code: registeredCourseCode }) =>
registeredCourseCode === selectedCourseCode
);
})
.map(({ course }) => course);
Comment on lines +482 to +489
Copy link
Member Author

Choose a reason for hiding this comment

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

ここらへんを checkScheduleDuplicate のように Usecase にするべきかどうかは迷ったので意見があればぜひ!

Copy link
Contributor

Choose a reason for hiding this comment

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

Usecaseにした方がいいかなと思いました。

Copy link
Member Author

@HosokawaR HosokawaR Apr 2, 2023

Choose a reason for hiding this comment

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

ありがとうございます!エラー数が増えてきたので一旦これで Merge しますが、後ほど UseCase に直します。

Ref: #666


if (duplicatedCourses.length > 0) {
const text =
`以下のコースはすでに登録されているため追加できません。\n` +
duplicatedCourses
.map(({ code, name }) => `【${code}】${name}`)
.join("\n");
displayToast(text, { type: "danger" });
gtm?.trackEvent({ event: "duplicated-courses-error" });
return;
}

duplicatedScheduleCourses.value = await (
await asyncFilter(
selectedSearchResults,
async ({ schedules }) =>
!(await Usecase.checkScheduleDuplicate(ports)(year.value, schedules))
)
).map(({ course }) => course);

if (warning && duplicateCourses.value.length > 0) {
if (warning && duplicatedScheduleCourses.value.length > 0) {
openDuplicateModal();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/store/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const displayToast = (
option?: { displayPeriod?: number; type?: ToastType }
) => {
const id = createId();
const displayPeriod = option?.displayPeriod ?? 3000;
const displayPeriod = option?.displayPeriod ?? text.length * 240; // 250 characters per minute reading speed
const type = option?.type ?? "danger";

toasts.push({ id, text, type });
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ export const hasProperty = <P extends string, T>(
): obj is { [key in P]: T } => {
return hasUnknownProperty(obj, prop) && clarifyPropertyType(obj, prop, fn);
};

export const asyncFilter = async <T, S>(
array: T[],
asyncCallback: (arg: T) => Promise<S>
) => {
const mask = await Promise.all(array.map(asyncCallback));
return array.filter((_, i) => Boolean(mask[i]));
};