Skip to content

Commit 701213c

Browse files
Merge pull request #103 from linked-planet/dev
time table - fixed start slot detection
2 parents e5340ce + 7b61576 commit 701213c

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

library/src/components/timetable/timeTableUtils.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,3 +942,100 @@ describe("bugfix test for left and width calculation", () => {
942942
expect(leftAndWidth.width).toBe(1)
943943
})
944944
})
945+
946+
describe("bugfix test for left and width calculation when item starts after end of day time", () => {
947+
it("should calculate left and width correctly for weeks view", () => {
948+
const item: TimeSlotBooking = {
949+
key: "1",
950+
title: "Test",
951+
startDate: dayjs()
952+
.startOf("week")
953+
.add(1, "days")
954+
.subtract(1, "minute"),
955+
endDate: dayjs().startOf("week").add(2, "days"),
956+
}
957+
958+
const timeSteps = 60
959+
960+
const props = calculateTimeSlotPropertiesForView(
961+
dayjs().startOf("week").add(6, "hours"),
962+
dayjs().startOf("week").add(1, "week").add(22, "hours"),
963+
timeSteps,
964+
"days",
965+
false,
966+
)
967+
968+
// get slots
969+
const result = getStartAndEndSlot(
970+
item,
971+
props.slotsArray,
972+
props.timeFrameDay,
973+
props.viewType,
974+
)
975+
976+
expect(result.status).toBe("in")
977+
expect(result.startSlot).toBe(1) // should be 1 because past 22:00 when the time frame of day ends
978+
expect(result.endSlot).toBe(2)
979+
980+
// calculate left and width
981+
const leftAndWidth = getLeftAndWidth(
982+
item,
983+
result.startSlot,
984+
result.endSlot,
985+
props.slotsArray,
986+
props.timeFrameDay,
987+
props.viewType,
988+
timeSteps,
989+
)
990+
991+
expect(leftAndWidth.left).toBe(0)
992+
expect(leftAndWidth.width).toBe(1)
993+
})
994+
})
995+
996+
describe("bugfix test for left and width calculation when item starts before start of day time", () => {
997+
it("should calculate left and width correctly for weeks view", () => {
998+
const item: TimeSlotBooking = {
999+
key: "1",
1000+
title: "Test",
1001+
startDate: dayjs().startOf("week").add(1, "days").add(1, "hour"),
1002+
endDate: dayjs().startOf("week").add(2, "days"),
1003+
}
1004+
1005+
const timeSteps = 60
1006+
1007+
const props = calculateTimeSlotPropertiesForView(
1008+
dayjs().startOf("week").add(6, "hours"),
1009+
dayjs().startOf("week").add(1, "week").add(22, "hours"),
1010+
timeSteps,
1011+
"days",
1012+
false,
1013+
)
1014+
1015+
// get slots
1016+
const result = getStartAndEndSlot(
1017+
item,
1018+
props.slotsArray,
1019+
props.timeFrameDay,
1020+
props.viewType,
1021+
)
1022+
1023+
expect(result.status).toBe("in")
1024+
expect(result.startSlot).toBe(1) // should be 1 because past 22:00 when the time frame of day ends
1025+
expect(result.endSlot).toBe(2)
1026+
1027+
// calculate left and width
1028+
const leftAndWidth = getLeftAndWidth(
1029+
item,
1030+
result.startSlot,
1031+
result.endSlot,
1032+
props.slotsArray,
1033+
props.timeFrameDay,
1034+
props.viewType,
1035+
timeSteps,
1036+
)
1037+
1038+
expect(leftAndWidth.left).toBe(0)
1039+
expect(leftAndWidth.width).toBe(1)
1040+
})
1041+
})

library/src/components/timetable/timeTableUtils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,23 @@ export function getStartAndEndSlot(
577577
if (startSlot > 0) {
578578
// if the item starts in the middle of a slot, we need to go back one slot to get the start slot
579579
// but only if the time slot before is on the same day, else it means that the booking starts before the time frame range of the day
580-
startSlot--
581580
if (viewType === "hours") {
582581
// if the previous timeslot is on a different day, we know item starts before the first time slot of a day
583582
if (
584-
slotsArray[startSlot].day() !== slotsArray[startSlot + 1].day()
583+
slotsArray[startSlot - 1].day() === slotsArray[startSlot].day()
585584
) {
586-
startSlot++
585+
startSlot--
586+
}
587+
} else {
588+
let startSlotEnd = slotsArray[startSlot - 1]
589+
.startOf(viewType)
590+
.add(timeFrameDay.endHour, "hours")
591+
.add(timeFrameDay.endMinute, "minutes")
592+
if (timeFrameDay.endHour === 0 && timeFrameDay.endMinute === 0) {
593+
startSlotEnd = startSlotEnd.add(1, viewType)
594+
}
595+
if (item.startDate.isBefore(startSlotEnd)) {
596+
startSlot--
587597
}
588598
}
589599
}

showcase/public/showcase-sources.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7893,7 +7893,7 @@ const exampleEntries: TimeTableTypes.TimeTableEntry<
78937893
},
78947894
],
78957895
},
7896-
/*{
7896+
{
78977897
group: {
78987898
id: "group-1",
78997899
title: "Group 1",
@@ -8222,7 +8222,7 @@ const exampleEntries: TimeTableTypes.TimeTableEntry<
82228222
title: "Item 7-1",
82238223
},
82248224
],
8225-
},*/
8225+
},
82268226
]
82278227

82288228
function createTestItems(

0 commit comments

Comments
 (0)