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
13 changes: 6 additions & 7 deletions components/committee/CommitteeInterviewTimes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const CommitteeInterviewTimes = ({
useEffect(() => {
if (period) {
setVisibleRange({
start: new Date(period!.interviewPeriod.start).toISOString(),
end: new Date(period!.interviewPeriod.end).toISOString(),
start: period.interviewPeriod.start.toISOString(),
end: period.interviewPeriod.end.toISOString(),
});
}
}, [period]);
Expand Down Expand Up @@ -101,8 +101,8 @@ const CommitteeInterviewTimes = ({
(at: any) => ({
id: crypto.getRandomValues(new Uint32Array(1))[0].toString(),
title: at.room,
start: new Date(at.start).toISOString(),
end: new Date(at.end).toISOString(),
start: at.start instanceof Date ? at.start.toISOString() : at.start,
end: at.end instanceof Date ? at.end.toISOString() : at.end,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hvorfor trengs denne sjekken her? Vil jo helst at den alltid skal være instanceof Date

})
);

Expand Down Expand Up @@ -317,10 +317,9 @@ const CommitteeInterviewTimes = ({
}, [period]);

const getSubmissionDeadline = (): string => {
const deadlineIso = period!.applicationPeriod.end;
const deadlineDate = period!.applicationPeriod.end;

if (deadlineIso != null && !deadLineHasPassed) {
const deadlineDate = new Date(deadlineIso);
if (deadlineDate != null && !deadLineHasPassed) {
const now = new Date();

if (now > deadlineDate) {
Expand Down
54 changes: 34 additions & 20 deletions components/committee/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ interface TimeSlot {
available: boolean;
}

interface IsoTimeSlot {
start: string;
end: string;
interface DateTimeSlot {
start: Date;
end: Date;
}

export default function Schedule({
Expand All @@ -37,14 +37,24 @@ export default function Schedule({
periodTime: any,
): { [date: string]: string } => {
if (!periodTime) return {};
const startDate = new Date(periodTime.start);
const startDate = new Date(
(periodTime.start instanceof Date
? periodTime.start
: new Date(periodTime.start)
).getTime(),
);
startDate.setHours(startDate.getHours() + 2);
const endDate = new Date(periodTime.end);
const endDate = new Date(
(periodTime.end instanceof Date
? periodTime.end
: new Date(periodTime.end)
).getTime(),
);
endDate.setHours(endDate.getHours() + 2);
const dates: { [date: string]: string } = {};
const dayNames = ["Søn", "Man", "Tir", "Ons", "Tor", "Fre", "Lør"];

let currentDate = new Date(startDate);
let currentDate = new Date(startDate.getTime());

while (currentDate <= endDate) {
const dayIndex = currentDate.getDay();
Expand Down Expand Up @@ -72,8 +82,8 @@ export default function Schedule({
return [hour, minute];
}, []);

const convertToIso = useCallback(
(date: string, timeSlot: string): IsoTimeSlot => {
const convertToDateSlot = useCallback(
(date: string, timeSlot: string): DateTimeSlot => {
const [startTimeStr, endTimeStr] = timeSlot.split(" - ");
const [year, month, day] = date.split("-").map(Number);

Expand All @@ -88,8 +98,8 @@ export default function Schedule({
);

return {
start: startTime.toISOString(),
end: endTime.toISOString(),
start: startTime,
end: endTime,
};
},
[parseTime],
Expand Down Expand Up @@ -118,21 +128,21 @@ export default function Schedule({
});
});

const isoTimeSlotsForExport = allAvailableTimes.map((slot) =>
convertToIso(slot.date, slot.time),
const dateTimeSlotsForExport = allAvailableTimes.map((slot) =>
convertToDateSlot(slot.date, slot.time),
);

setApplicationData((prevData: any) => ({
...prevData,
selectedTimes: isoTimeSlotsForExport,
selectedTimes: dateTimeSlotsForExport,
}));
setIsInitialized(true);
}, [
isInitialized,
applicationData.selectedTimes,
periodTime,
timeSlots,
convertToIso,
convertToDateSlot,
setApplicationData,
]);

Expand Down Expand Up @@ -185,26 +195,30 @@ export default function Schedule({
});
});

const isoTimeSlotsForExport = dataToSend.map((slot) =>
convertToIso(slot.date, slot.time),
const dateTimeSlotsForExport = dataToSend.map((slot) =>
convertToDateSlot(slot.date, slot.time),
);

// Queue the update instead of calling setApplicationData directly
setPendingUpdate(isoTimeSlotsForExport);
setPendingUpdate(dateTimeSlotsForExport);

return newCells;
});
};

const dates = getDatesWithinPeriod(periodTime);

let weekDates: { [date: string]: IsoTimeSlot[] } = {};
let weekDates: { [date: string]: DateTimeSlot[] } = {};

Object.keys(dates).forEach((date) => {
weekDates[date] = [];
applicationData.selectedTimes?.forEach((slot) => {
if (slot?.start?.includes(date) && slot?.end) {
weekDates[date].push({ start: slot.start, end: slot.end });
if (slot?.start && slot?.end) {
const start = new Date(slot.start as unknown as string | number | Date);
const end = new Date(slot.end as unknown as string | number | Date);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tilsvarende her

if (start.toISOString().includes(date)) {
weekDates[date].push({ start, end });
}
}
});
});
Expand Down
10 changes: 2 additions & 8 deletions components/committee/ScheduleColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Props {
date: string;
weekDay: string;
interviewLength: number;
availableSlots: { start: string; end: string }[];
availableSlots: { start: Date; end: Date }[];
onToggleAvailability: (
date: string,
time: string,
Expand All @@ -27,13 +27,7 @@ export default function ScheduleColumn({
const dateOfMonth = date.split("-")[2];
const month = date.split("-")[1];

const adjustedAvailableSlots = availableSlots.map((slot) => {
slot.start = slot.start.replace("Z", "");
slot.end = slot.end.replace("Z", "");
return slot;
});

const availableTimeSlots = convertIsoToScheduleFormat(adjustedAvailableSlots);
const availableTimeSlots = convertIsoToScheduleFormat(availableSlots);
const availableTimes = availableTimeSlots.map((slot) => {
let [firstTime, secondTime] = slot.time.split(" - ").map((s) => s.trim());
return `${firstTime} - ${secondTime}`;
Expand Down
8 changes: 4 additions & 4 deletions components/form/DatePickerInput.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useEffect, useState } from "react";
interface Props {
label?: string;
updateDates: (dates: { start: string; end: string }) => void;
updateDates: (dates: { start: Date | null; end: Date | null }) => void;
}

const DatePickerInput = (props: Props) => {
const [fromDate, setFromDate] = useState("");
const [toDate, setToDate] = useState("");

useEffect(() => {
const startDate = fromDate ? `${fromDate}T00:00` : "";
const endDate = toDate ? `${toDate}T23:59` : "";
props.updateDates({ start: startDate, end: endDate });
const start = fromDate ? new Date(`${fromDate}T00:00`) : null;
const end = toDate ? new Date(`${toDate}T23:59`) : null;
props.updateDates({ start, end });
}, [fromDate, toDate]);

return (
Expand Down
29 changes: 23 additions & 6 deletions lib/api/applicantApi.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import { QueryFunctionContext } from "@tanstack/react-query";
import { applicantType } from "../types/types";
import { parseApplicantDates } from "../utils/parseDates";

export const fetchApplicantByPeriodAndId = async (
context: QueryFunctionContext
) => {
const periodId = context.queryKey[1];
const applicantId = context.queryKey[2];
return fetch(`/api/applicants/${periodId}/${applicantId}`).then((res) =>
res.json()
const data = await fetch(`/api/applicants/${periodId}/${applicantId}`).then(
(res) => res.json()
);
return {
...data,
application: data.application
? parseApplicantDates(data.application)
: undefined,
};
};

export const fetchApplicantsByPeriodId = async (
context: QueryFunctionContext
) => {
const periodId = context.queryKey[1];
return fetch(`/api/applicants/${periodId}`).then((res) => res.json());
const data = await fetch(`/api/applicants/${periodId}`).then((res) =>
res.json()
);
return {
...data,
applications: data.applications?.map(parseApplicantDates),
};
};

export const fetchApplicantsByPeriodIdAndCommittee = async (
context: QueryFunctionContext
) => {
const periodId = context.queryKey[1];
const committee = context.queryKey[2];
return fetch(`/api/committees/applicants/${periodId}/${committee}`).then(
(res) => res.json()
);
const data = await fetch(
`/api/committees/applicants/${periodId}/${committee}`
).then((res) => res.json());
return {
...data,
applicants: data.applicants?.map(parseApplicantDates),
};
};

export const createApplicant = async (applicant: applicantType) => {
Expand Down
11 changes: 8 additions & 3 deletions lib/api/committeesApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QueryFunctionContext } from "@tanstack/react-query";
import { OwGroup } from "../types/types";
import { parseCommitteeDates } from "../utils/parseDates";

export const fetchOwCommittees = async (): Promise<OwGroup[]> => {
return fetch(`/api/periods/ow-committees`).then((res) => res.json());
Expand All @@ -9,7 +10,11 @@ export const fetchCommitteeTimes = async (context: QueryFunctionContext) => {
const periodId = context.queryKey[1];
const committee = context.queryKey[2];

return fetch(`/api/committees/times/${periodId}/${committee}`).then((res) =>
res.json(),
);
const data = await fetch(
`/api/committees/times/${periodId}/${committee}`,
).then((res) => res.json());
return {
...data,
committees: data.committees?.map(parseCommitteeDates),
};
};
13 changes: 11 additions & 2 deletions lib/api/periodApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { QueryFunctionContext } from "@tanstack/react-query";
import { periodType } from "../types/types";
import { parsePeriodDates } from "../utils/parseDates";

export const fetchPeriodById = async (context: QueryFunctionContext) => {
const id = context.queryKey[1];
return fetch(`/api/periods/${id}`).then((res) => res.json());
const data = await fetch(`/api/periods/${id}`).then((res) => res.json());
return {
...data,
period: data.period ? parsePeriodDates(data.period) : undefined,
};
};

export const fetchPeriods = async () => {
return fetch(`/api/periods`).then((res) => res.json());
const data = await fetch(`/api/periods`).then((res) => res.json());
return {
...data,
periods: data.periods?.map(parsePeriodDates),
};
};

export const deletePeriodById = async (id: string) => {
Expand Down
14 changes: 7 additions & 7 deletions lib/mongo/applicants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ export const getApplicantsForCommittee = async (
userCommittees.includes(preference),
);

// Skjuler søkerinformasjon for komitéen etter syv dager etter intervjuperioden
// Skjuler søkerinformasjon for komitéen etter fem dager etter intervjuperioden
const today = new Date();
const sevenDaysAfterInterviewEnd = new Date(period.interviewPeriod.end);
sevenDaysAfterInterviewEnd.setDate(
sevenDaysAfterInterviewEnd.getDate() + 5,
const fiveDaysAfterInterviewEnd = new Date(period.interviewPeriod.end.getTime());
fiveDaysAfterInterviewEnd.setDate(
fiveDaysAfterInterviewEnd.getDate() + 5,
);

if (
new Date(period.applicationPeriod.end) > today ||
today > sevenDaysAfterInterviewEnd
period.applicationPeriod.end > today ||
today > fiveDaysAfterInterviewEnd
) {
applicant.owId = "Skjult";
applicant.name = "Skjult";
Expand All @@ -211,7 +211,7 @@ export const getApplicantsForCommittee = async (
applicant.email = "Skjult";
applicant.about = "Skjult";
applicant.grade = "-";
applicant.selectedTimes = [{ start: "Skjult", end: "Skjult" }];
applicant.selectedTimes = [{ start: new Date(0), end: new Date(0) }];
}

const isSelectedCommitteePresent =
Expand Down
4 changes: 1 addition & 3 deletions lib/mongo/periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ export const getCurrentPeriods = async () => {
try {
if (!periods) await init();

const currentDate = new Date().toISOString();
const currentDate = new Date();

const filter = {
$or: [
{
// Check if current ISO date string is within the application period
"applicationPeriod.start": { $lte: currentDate },
"applicationPeriod.end": { $gte: currentDate },
},
{
// Check if current ISO date string is within the interview period
"interviewPeriod.start": { $lte: currentDate },
"interviewPeriod.end": { $gte: currentDate },
},
Expand Down
8 changes: 4 additions & 4 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type applicantType = {
optionalCommittees: string[];
selectedTimes: [
{
start: string;
end: string;
start: Date;
end: Date;
},
];
date: Date;
Expand Down Expand Up @@ -67,8 +67,8 @@ export type periodType = {
};

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

Expand Down
Loading