diff --git a/src/app/adjust/WeekView.tsx b/src/app/adjust/WeekView.tsx index ef94438..d11fb92 100644 --- a/src/app/adjust/WeekView.tsx +++ b/src/app/adjust/WeekView.tsx @@ -3,7 +3,7 @@ import React, { useState } from "react" import { formatTime, getEventPosition } from "../../lib/draft/utils" import { Period } from "@/lib/scheduling" -import { coursePeriodsThroughWeeks } from "@/lib/course" +import { CoursePeriod } from "@/lib/course" import { Course } from "@/third-party/twinte-parser-type" import { Button } from "@/components/ui/button" import { ChevronLeft, ChevronRight } from "lucide-react" @@ -13,7 +13,7 @@ type WeekViewProps = { currentDate: Date handlePeriodClick: (period: Period) => Promise isButtonActive: boolean - courses: Course[] + coursePeriods: CoursePeriod[] } const handleClassClick = (courseWithPeriod: CourseWithPeriod) => { @@ -35,7 +35,7 @@ export function WeekView({ currentDate, handlePeriodClick, isButtonActive, - courses, + coursePeriods, }: WeekViewProps) { const [currentWeekIndex, setCurrentWeekIndex] = useState(0) @@ -47,8 +47,6 @@ export function WeekView({ const hours = Array.from({ length: 24 }, (_, i) => i) - const coursePeriods = coursePeriodsThroughWeeks(courses, currentDate) - const handleNextWeek = () => { setCurrentWeekIndex(prevIndex => Math.min(prevIndex + 1, 3)) } @@ -56,6 +54,7 @@ export function WeekView({ const handlePreviousWeek = () => { setCurrentWeekIndex(prevIndex => Math.max(prevIndex - 1, 0)) } + console.log("coursePeriods:", coursePeriods) /** @todo Reduce this TOO DEEP nest */ return ( diff --git a/src/app/adjust/candidate.tsx b/src/app/adjust/candidate.tsx index 6ce6b03..e4440e7 100644 --- a/src/app/adjust/candidate.tsx +++ b/src/app/adjust/candidate.tsx @@ -11,6 +11,8 @@ import { WeekView } from "./WeekView" import { Course } from "@/third-party/twinte-parser-type" import { YesNoDialog } from "@/components/ui/dialog" import { coursePeriodsThroughWeeks } from "@/lib/course" +import { CoursePeriod, courseToPeriods } from "@/lib/course" +import { getUserCourseCodes } from "@/lib/server" type Props = { title: string @@ -20,6 +22,7 @@ type Props = { selectedDurationMinute: number /** 履修中の講義一覧 */ courses: Course[] + allCourses: Course[] } export default function Candidate(props: Props) { @@ -34,15 +37,41 @@ export default function Candidate(props: Props) { setIsButtonActive(props.title.trim() !== "") }, [props.title]) + // 自分の授業とその時間の配列 + const coursePeriods: CoursePeriod[] = props.courses.map(course => ({ + course, + periods: courseToPeriods(new Date(), course), + })) + async function handleSchedule() { setIsButtonActive(false) try { - const coursePeriods = coursePeriodsThroughWeeks(props.courses, new Date()) + // 招待相手ごとの授業とその時間の配列 + const coursePeriodsOfGuests: CoursePeriod[][] = ( + await Promise.all(props.selectedUserIds.map(getUserCourseCodes)) + ).map(codes => + props.allCourses + .filter(({ code }) => codes.includes(code)) + .map(course => ({ + course, + periods: courseToPeriods(new Date(), course), + })), + ) + + console.debug("自分の授業とその時間の配列", coursePeriods) + console.debug("招待相手ごとの授業とその時間の配列", coursePeriodsOfGuests) + + // 自分と招待相手全員を合わせた授業時間の配列 + const classPeriodsOfUsers: Period[] = [...coursePeriodsOfGuests, coursePeriods].flatMap( + coursePeriods => coursePeriods.flatMap(({ periods }) => periods), + ) + + console.debug("すべての授業の、授業時間の配列", classPeriodsOfUsers) + const periods = [ ...(await periodsOfUsers(props.selectedUserIds, props.excludePeriod)), - coursePeriods.flatMap(({ periods }) => periods), + classPeriodsOfUsers, ] - console.debug("授業とそれぞれの授業時間の配列", coursePeriods) const freePeriods = await findFreePeriods(props.selectedDurationMinute, periods) console.log("freePfreePeriods:", freePeriods) @@ -128,25 +157,6 @@ export default function Candidate(props: Props) { "" )} - {/* */} ) } diff --git a/src/app/adjust/client.tsx b/src/app/adjust/client.tsx index 73264e6..5b80f41 100644 --- a/src/app/adjust/client.tsx +++ b/src/app/adjust/client.tsx @@ -116,6 +116,7 @@ export default function SchedulePlanner(props: { title={title} users={users} courses={courses} + allCourses={allCourses} /> diff --git a/src/app/adjust/page.tsx b/src/app/adjust/page.tsx index f7be30b..7fd08b5 100644 --- a/src/app/adjust/page.tsx +++ b/src/app/adjust/page.tsx @@ -4,7 +4,7 @@ import { getServerSession } from "next-auth" import { authOptions } from "@/lib/auth" import { fetchCourses } from "@/third-party/twinte-parser" import { Course } from "@/third-party/twinte-parser-type" -import { getUserCourseIds } from "@/lib/server" +import { getUserCourseCodes } from "@/lib/server" export default async function AdjustPage() { const session = await getServerSession(authOptions) @@ -12,7 +12,7 @@ export default async function AdjustPage() { const allCourses = (await fetchCourses()) as Course[] // console.log(allCourses) - const userCourseIds = session?.user.id ? await getUserCourseIds(session.user.id) : undefined + const userCourseIds = session?.user.id ? await getUserCourseCodes(session.user.id) : undefined console.log("userCourseIds:", userCourseIds) const courses = allCourses.filter(course => userCourseIds?.includes(course.code)) diff --git a/src/lib/server.ts b/src/lib/server.ts index bc0b83a..f01cd53 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -26,7 +26,7 @@ export async function resetAndUpdateUserCourses(userId: string, courses: Course[ } } -export async function getUserCourseIds(userId: string): Promise { +export async function getUserCourseCodes(userId: string): Promise { try { const userCourses = await prisma.course.findMany({ where: { userId: userId },