Skip to content

Commit

Permalink
招待相手の授業時間を考慮して空き時間検出
Browse files Browse the repository at this point in the history
  • Loading branch information
uxiun committed Jan 17, 2025
1 parent be788a9 commit 8fdab99
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
9 changes: 4 additions & 5 deletions src/app/adjust/WeekView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,7 +13,7 @@ type WeekViewProps = {
currentDate: Date
handlePeriodClick: (period: Period) => Promise<void>
isButtonActive: boolean
courses: Course[]
coursePeriods: CoursePeriod[]
}

const handleClassClick = (courseWithPeriod: CourseWithPeriod) => {
Expand All @@ -35,7 +35,7 @@ export function WeekView({
currentDate,
handlePeriodClick,
isButtonActive,
courses,
coursePeriods,
}: WeekViewProps) {
const [currentWeekIndex, setCurrentWeekIndex] = useState(0)

Expand All @@ -47,15 +47,14 @@ 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))
}

const handlePreviousWeek = () => {
setCurrentWeekIndex(prevIndex => Math.max(prevIndex - 1, 0))
}
console.log("coursePeriods:", coursePeriods)

/** @todo Reduce this TOO DEEP nest */
return (
Expand Down
54 changes: 32 additions & 22 deletions src/app/adjust/candidate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +22,7 @@ type Props = {
selectedDurationMinute: number
/** 履修中の講義一覧 */
courses: Course[]
allCourses: Course[]
}

export default function Candidate(props: Props) {
Expand All @@ -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)
Expand Down Expand Up @@ -128,25 +157,6 @@ export default function Candidate(props: Props) {
""
)}

{/* <ul className="py-4 space-y-2">
{freePeriods.map(period => (
<li key={period.start.toString()}>
<Button
disabled={!isButtonActive}
variant={selectedPeriod?.start === period.start ? "secondary" : "ghost"}
className="w-full justify-between font-normal"
onClick={() => handlePeriodClick(period)}
>
<span className="flex items-center mr-2 h-4 w-4">
<span>{formatDate(period.start)}</span>
<span className="px-2">~</span>
<span>{formatDate(period.end)}</span>
</span>
</Button>
</li>
))}
</ul> */}
</div>
)
}
1 change: 1 addition & 0 deletions src/app/adjust/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default function SchedulePlanner(props: {
title={title}
users={users}
courses={courses}
allCourses={allCourses}
/>
<ToastContainer />
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/app/adjust/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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)
const users = (await db.allUsers()).filter(user => user.email !== session?.user.email)
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))
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function resetAndUpdateUserCourses(userId: string, courses: Course[
}
}

export async function getUserCourseIds(userId: string): Promise<string[]> {
export async function getUserCourseCodes(userId: string): Promise<string[]> {
try {
const userCourses = await prisma.course.findMany({
where: { userId: userId },
Expand Down

0 comments on commit 8fdab99

Please sign in to comment.