Skip to content

Commit 2aa49f0

Browse files
committed
fix 24 hours wraparound when using time picker (attempt 2)
1 parent 9fb1ad9 commit 2aa49f0

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/data/schedule/move_timeslot.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,33 @@ export const moveTimeslot = (slots: Timeslot[], slotIdx: number, update: { start
114114
}
115115
slots = Object.assign(slots, { [slotIdx]: <Timeslot>{ ...slots[slotIdx], start: timeToString(newTime) } });
116116

117-
if (isDefined(slots[slotIdx - 1].stop)) {
117+
if (slotIdx > 0 && isDefined(slots[slotIdx - 1].stop)) {
118118
//stretch previous slot
119119
slots = Object.assign(slots, { [slotIdx - 1]: <Timeslot>{ ...slots[slotIdx - 1], stop: timeToString(newTime) } });
120120
}
121121
else {
122122
//insert new filler before
123123
slots = [
124124
...slots.slice(0, slotIdx),
125-
<Timeslot>{ ...slots[slotIdx], start: timeToString(stopTime(slots[slotIdx - 1])), stop: timeToString(newTime), actions: [] },
125+
<Timeslot>{
126+
...slots[slotIdx],
127+
start: slotIdx > 0 ? timeToString(stopTime(slots[slotIdx - 1])) : '00:00:00',
128+
stop: timeToString(newTime),
129+
actions: []
130+
},
126131
...slots.slice(slotIdx),
127132
];
128133
slotIdxOut = slotIdx + 1;
129134
}
130135

131136
for (let i = (slotIdxOut + 1); i < slots.length; i++) { //walk through all slots after the modified one
132-
let d1 = computeDuration(startTime(slots[i]), newTime, hass);
133-
let d2 = computeDuration(stopTime(slots[i]), newTime, hass);
134-
137+
let newStopTime = stopTime(slots[slotIdxOut]);
138+
let d1 = computeDuration(startTime(slots[i]), newStopTime, hass);
139+
let d2 = computeDuration(stopTime(slots[i]), newStopTime, hass);
135140

136141
if (d1 > 0 && d2 < 0) {
137142
//timeslot has partial overlap with the new time point, it should be shortened.
138-
slots = Object.assign(slots, { [i]: <Timeslot>{ ...slots[i], start: timeToString(newTime) } });
143+
slots = Object.assign(slots, { [i]: <Timeslot>{ ...slots[i], start: timeToString(newStopTime) } });
139144
}
140145
else if (d1 < 0) {
141146
//timeslot slot ends before the new time point, stop iterating

0 commit comments

Comments
 (0)