diff --git a/pages/[user]/[type].tsx b/pages/[user]/[type].tsx index 435d5294..1bea5227 100644 --- a/pages/[user]/[type].tsx +++ b/pages/[user]/[type].tsx @@ -58,11 +58,11 @@ export default function Type(props) { if (selectedDate == dayjs().format("YYYY-MM-DD")) { var i = (parseInt(dayjs().startOf('hour').format('H') * 60) + parseInt(dayjs().startOf('hour').format('m'))); } else { - var i = 0; + var i = props.user.startTime; } // Until day end, push new times every x minutes - for (;i < 1440; i += parseInt(props.eventType.length)) { + for (;i < props.user.endTime; i += parseInt(props.eventType.length)) { times.push(dayjs(selectedDate).hour(Math.floor(i / 60)).minute(i % 60).startOf(props.eventType.length, 'minute').add(props.eventType.length, 'minute').format("YYYY-MM-DD HH:mm:ss")); } @@ -163,7 +163,9 @@ export async function getServerSideProps(context) { name: true, bio: true, avatar: true, - eventTypes: true + eventTypes: true, + startTime: true, + endTime: true } }); diff --git a/pages/api/availability/day.ts b/pages/api/availability/day.ts new file mode 100644 index 00000000..9816e345 --- /dev/null +++ b/pages/api/availability/day.ts @@ -0,0 +1,41 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { getSession } from 'next-auth/client'; +import prisma from '../../../lib/prisma'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const session = await getSession({req: req}); + + if (!session) { + res.status(401).json({message: "Not authenticated"}); + return; + } + + if (req.method == "PATCH") { + // TODO: Add user ID to user session object + const user = await prisma.user.findFirst({ + where: { + email: session.user.email, + }, + select: { + id: true + } + }); + + if (!user) { res.status(404).json({message: 'User not found'}); return; } + + const startMins = req.body.start; + const endMins = req.body.end; + + const updateDay = await prisma.user.update({ + where: { + id: user.id, + }, + data: { + startTime: startMins, + endTime: endMins + }, + }); + + res.status(200).json({message: 'Start and end times updated successfully'}); + } +} \ No newline at end of file diff --git a/pages/availability/index.tsx b/pages/availability/index.tsx index 7ab42e5a..a20514cc 100644 --- a/pages/availability/index.tsx +++ b/pages/availability/index.tsx @@ -10,10 +10,16 @@ import { useSession, getSession } from 'next-auth/client'; export default function Availability(props) { const [ session, loading ] = useSession(); const [showAddModal, setShowAddModal] = useState(false); + const [showChangeTimesModal, setShowChangeTimesModal] = useState(false); const titleRef = useRef(); const descriptionRef = useRef(); const lengthRef = useRef(); + const startHoursRef = useRef(); + const startMinsRef = useRef(); + const endHoursRef = useRef(); + const endMinsRef = useRef(); + if (loading) { return
Loading...
; } else { @@ -26,6 +32,18 @@ export default function Availability(props) { setShowAddModal(!showAddModal); } + function toggleChangeTimesModal() { + setShowChangeTimesModal(!showChangeTimesModal); + } + + function convertMinsToHrsMins (mins) { + let h = Math.floor(mins / 60); + let m = mins % 60; + h = h < 10 ? '0' + h : h; + m = m < 10 ? '0' + m : m; + return `${h}:${m}`; + } + async function createEventTypeHandler(event) { event.preventDefault(); @@ -47,6 +65,31 @@ export default function Availability(props) { Router.reload(); } + async function updateStartEndTimesHandler(event) { + event.preventDefault(); + + const enteredStartHours = parseInt(startHoursRef.current.value); + const enteredStartMins = parseInt(startMinsRef.current.value); + const enteredEndHours = parseInt(endHoursRef.current.value); + const enteredEndMins = parseInt(endMinsRef.current.value); + + const startMins = enteredStartHours * 60 + enteredStartMins; + const endMins = enteredEndHours * 60 + enteredEndMins; + + // TODO: Add validation + + const response = await fetch('/api/availability/day', { + method: 'PATCH', + body: JSON.stringify({start: startMins, end: endMins}), + headers: { + 'Content-Type': 'application/json' + } + }); + + console.log(response); + Router.reload(); + } + return(+ Currently, your day is set to start at {convertMinsToHrsMins(props.user.startTime)} and end at {convertMinsToHrsMins(props.user.endTime)}. +
++ Set the start and end time of your day. +
+