Skip to content

Commit

Permalink
Add toast to notify error when the already registered course is added
Browse files Browse the repository at this point in the history
  • Loading branch information
HosokawaR committed Apr 1, 2023
1 parent 7c9c727 commit 48d4bcc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
36 changes: 32 additions & 4 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,6 +286,7 @@ 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 { asyncFilter, deleteElementInArray } from "~/utils";
import type { Schedule } from "~/domain/schedule";
Expand Down Expand Up @@ -469,18 +470,45 @@ const buttonState = computed(() =>
selectedSearchResults.length > 0 ? "default" : "disabled"
);
const duplicateCourses = ref<DisplayCourse[]>([]);
const duplicatedScheduleCourses = ref<DisplayCourse[]>([]);
const addCourses = async (warning = true) => {
duplicateCourses.value = await (
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);
if (duplicatedCourses.length > 0) {
const text =
`以下のコースはすでに登録されているため追加できません。\n` +
duplicatedCourses
.map(({ code, name }) => `【${code}】${name}`)
.join("\n");
displayToast(text, {
type: "danger",
});
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

0 comments on commit 48d4bcc

Please sign in to comment.