From 167f9815228aacb90010636ddbb2b4756dd31bec Mon Sep 17 00:00:00 2001 From: Bailey Pumfleet Date: Tue, 13 Apr 2021 17:16:32 +0100 Subject: [PATCH] Change start and end times of day --- pages/[user]/[type].tsx | 8 +- pages/api/availability/day.ts | 41 +++++++++++ pages/availability/index.tsx | 133 +++++++++++++++++++++++++++++++++- prisma/schema.prisma | 2 + 4 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 pages/api/availability/day.ts 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(
@@ -64,7 +107,7 @@ export default function Availability(props) {
-
+
@@ -108,6 +151,23 @@ export default function Availability(props) {
+
+
+

+ Change the start and end times of your day +

+
+

+ Currently, your day is set to start at {convertMinsToHrsMins(props.user.startTime)} and end at {convertMinsToHrsMins(props.user.endTime)}. +

+
+
+ +
+
+
{showAddModal &&
@@ -161,7 +221,70 @@ export default function Availability(props) { - +
+ +
+
+
+ } + {showChangeTimesModal && +
+
+ + + + +
+
+
+ + + +
+
+ +
+

+ Set the start and end time of your day. +

+
+
+
+
+
+ +
+ + +
+ : +
+ + +
+
+
+ +
+ + +
+ : +
+ + +
+
+
+ +
@@ -183,7 +306,9 @@ export async function getServerSideProps(context) { email: session.user.email, }, select: { - id: true + id: true, + startTime: true, + endTime: true } }); @@ -199,6 +324,6 @@ export async function getServerSideProps(context) { } }); return { - props: {types}, // will be passed to the page component as props + props: {user, types}, // will be passed to the page component as props } } \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4dc055db..b9724c95 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -35,6 +35,8 @@ model User { password String? bio String? avatar String? + startTime Int @default(0) + endTime Int @default(1440) createdDate DateTime @default(now()) @map(name: "created") eventTypes EventType[] credentials Credential[]