Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions components/committee/CommitteeInterviewTimes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSession } from "next-auth/react";
import FullCalendar from "@fullcalendar/react";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction";
import { periodType, committeeInterviewType } from "../../lib/types/types";
import { periodType, committeeInterviewType, AvailableTime } from "../../lib/types/types";
import toast from "react-hot-toast";
import NotFound from "../../pages/404";
import Button from "../Button";
Expand All @@ -19,8 +19,8 @@ import { XMarkIcon } from "@heroicons/react/24/solid";
interface Interview {
id: string;
title: string;
start: string;
end: string;
start: Date;
end: Date;
}

interface Props {
Expand Down Expand Up @@ -98,11 +98,11 @@ const CommitteeInterviewTimes = ({
if (cleanCommittee === cleanSelectedCommittee) {
setHasAlreadySubmitted(true);
const events = committeeInterviewTimes.availabletimes.map(
(at: any) => ({
(availableTime) => ({
id: crypto.getRandomValues(new Uint32Array(1))[0].toString(),
title: at.room,
start: new Date(at.start).toISOString(),
end: new Date(at.end).toISOString(),
title: availableTime.room,
start: availableTime.start,
end: availableTime.end,
})
);

Expand Down Expand Up @@ -165,8 +165,8 @@ const CommitteeInterviewTimes = ({
const event: Interview = {
id: crypto.getRandomValues(new Uint32Array(1))[0].toString(),
title: roomInput,
start: currentSelection.start.toISOString(),
end: currentSelection.end.toISOString(),
start: currentSelection.start,
end: currentSelection.end,
};

const calendarApi = currentSelection.view.calendar;
Expand All @@ -180,8 +180,7 @@ const CommitteeInterviewTimes = ({

const submit = async (e: BaseSyntheticEvent) => {
e.preventDefault();
const formattedEvents = formatEventsForExport(calendarEvents);
if (formattedEvents.length === 0) {
if (calendarEvents.length === 0) {
toast.error("Fyll inn minst et gyldig tidspunkt");
return;
}
Expand All @@ -190,7 +189,7 @@ const CommitteeInterviewTimes = ({
periodId: period!._id,
period_name: period!.name,
committee: committee,
availabletimes: formattedEvents,
availabletimes: calendarEvents,
timeslot: `${selectedTimeslot}`,
message: "",
};
Expand All @@ -208,7 +207,6 @@ const CommitteeInterviewTimes = ({
throw new Error("Failed to submit data");
}

const result = await response.json();
toast.success("Tidene er sendt inn!");
setHasAlreadySubmitted(true);
setUnsavedChanges(false);
Expand Down Expand Up @@ -262,18 +260,6 @@ const CommitteeInterviewTimes = ({
);
};

const formatEventsForExport = (events: Interview[]) => {
return events.map((event) => {
const startDateTime = new Date(event.start);
const endDateTime = new Date(event.end);
return {
room: event.title,
start: startDateTime.toISOString(),
end: endDateTime.toISOString(),
};
});
};

const handleTimeslotSelection = (e: React.ChangeEvent<HTMLSelectElement>) => {
setSelectedTimeslot(e.target.value);
setUnsavedChanges(true);
Expand Down Expand Up @@ -354,8 +340,8 @@ const CommitteeInterviewTimes = ({

const calculateInterviewsPlanned = () => {
const totalMinutes = calendarEvents.reduce((acc, event) => {
const start = new Date(event.start);
const end = new Date(event.end);
const start = event.start;
const end = event.end;
const duration = (end.getTime() - start.getTime()) / 1000 / 60;
return acc + duration;
}, 0);
Expand Down Expand Up @@ -474,8 +460,8 @@ const CommitteeInterviewTimes = ({
hasAlreadySubmitted
? deleteSubmission
: (e: BaseSyntheticEvent) => {
submit(e);
}
submit(e);
}
}
color={hasAlreadySubmitted ? "orange" : "blue"}
/>
Expand Down
6 changes: 5 additions & 1 deletion lib/api/committeesApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { QueryFunctionContext } from "@tanstack/react-query";
import { WithId } from "mongodb";
import { committeeInterviewType } from "../types/types";

export const fetchOwCommittees = async () => {
return fetch(`/api/periods/ow-committees`).then((res) => res.json());
};

export const fetchCommitteeTimes = async (context: QueryFunctionContext) => {
export const fetchCommitteeTimes = async (
context: QueryFunctionContext
): Promise<{ committees: WithId<committeeInterviewType>[] | undefined }> => {
const periodId = context.queryKey[1];
const committee = context.queryKey[2];

Expand Down
4 changes: 2 additions & 2 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export type periodType = {
};

export type AvailableTime = {
start: string;
end: string;
start: Date;
end: Date;
room: string;
};

Expand Down
2 changes: 1 addition & 1 deletion pages/api/applicants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

if (req.method === "POST") {
const requestBody: applicantType = req.body;
requestBody.date = new Date(new Date().getTime() + 60 * 60 * 2000); // add date with norwegain time (GMT+2)
requestBody.date = new Date();

const { period } = await getPeriodById(String(requestBody.periodId));

Expand Down