Fix conflict checking

pull/6/head
Bailey Pumfleet 2021-04-11 21:51:58 +01:00
parent ddd7ccfb01
commit bc655fd301
1 changed files with 16 additions and 9 deletions

View File

@ -5,7 +5,9 @@ import prisma from '../../lib/prisma';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
const dayjs = require('dayjs'); const dayjs = require('dayjs');
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore'); const isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
const isBetween = require('dayjs/plugin/isBetween');
dayjs.extend(isSameOrBefore); dayjs.extend(isSameOrBefore);
dayjs.extend(isBetween);
export default function Type(props) { export default function Type(props) {
// Initialise state // Initialise state
@ -65,22 +67,27 @@ export default function Type(props) {
} }
// Check for conflicts // Check for conflicts
times.forEach(time => { for(i = times.length - 1; i >= 0; i -= 1) {
busy.forEach(busyTime => { busy.forEach(busyTime => {
let startTime = dayjs(busyTime.start); let startTime = dayjs(busyTime.start);
let endTime = dayjs(busyTime.end); let endTime = dayjs(busyTime.end);
// Check if start times are the same // Check if time has passed
if (dayjs(time).format('HH:mm') == startTime.format('HH:mm')) { if (dayjs(times[i]).isBefore(dayjs())) {
const conflictIndex = times.indexOf(time); times.splice(i, 1);
if (conflictIndex > -1) {
times.splice(conflictIndex, 1);
}
} }
// TODO: Check if time is between start and end times // Check if start times are the same
if (dayjs(times[i]).format('HH:mm') == startTime.format('HH:mm')) {
times.splice(i, 1);
}
// Check if time is between start and end times
if (dayjs(times[i]).isBetween(startTime, endTime)) {
times.splice(i, 1);
}
}); });
}); }
// Display available times // Display available times
const availableTimes = times.map((time) => const availableTimes = times.map((time) =>