Skip to content

Commit

Permalink
fix(react-scheduler): fix error for overlapping appointments (#3498)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krijovnick committed Feb 3, 2022
1 parent 0f684eb commit d2fdbb8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
65 changes: 30 additions & 35 deletions packages/dx-scheduler-core/src/plugins/appointments/helpers.test.ts
Expand Up @@ -1991,10 +1991,10 @@ describe('Appointments helpers', () => {
blockIndex: 0,
}, {
...appointmentForests[0].items[1],
blockIndex: 1,
blockIndex: 0,
}, {
...appointmentForests[0].items[2],
blockIndex: 2,
blockIndex: 1,
}],
};
const expectedBlocks = [{
Expand All @@ -2004,15 +2004,7 @@ describe('Appointments helpers', () => {
minOffset: 0,
maxOffset: 1,
size: 2,
items: [0],
}, {
start: moment('2020-05-07 08:30'),
end: moment('2020-05-07 09:30'),
endForChildren: moment('2020-05-07 09:30'),
minOffset: 1,
maxOffset: 1,
size: 1,
items: [1],
items: [0, 1],
}, {
start: moment('2020-05-07 09:00'),
end: moment('2020-05-07 09:30'),
Expand Down Expand Up @@ -2082,13 +2074,13 @@ describe('Appointments helpers', () => {
blockIndex: 0,
}, {
...appointmentForests[0].items[1],
blockIndex: 1,
blockIndex: 0,
}, {
...appointmentForests[0].items[2],
blockIndex: 1,
blockIndex: 0,
}, {
...appointmentForests[0].items[3],
blockIndex: 2,
blockIndex: 1,
}],
};
const expectedBlocks = [{
Expand All @@ -2098,15 +2090,7 @@ describe('Appointments helpers', () => {
minOffset: 0,
maxOffset: 2,
size: 3,
items: [0],
}, {
start: moment('2020-05-07 08:30'),
end: moment('2020-05-07 09:30'),
endForChildren: moment('2020-05-07 09:30'),
minOffset: 1,
maxOffset: 2,
size: 2,
items: [1, 2],
items: [0, 1, 2],
}, {
start: moment('2020-05-07 09:00'),
end: moment('2020-05-07 09:30'),
Expand Down Expand Up @@ -2231,10 +2215,10 @@ describe('Appointments helpers', () => {
blockIndex: 2,
}, {
...appointmentForests[0].items[6],
blockIndex: 3,
blockIndex: 2,
}, {
...appointmentForests[0].items[7],
blockIndex: 4,
blockIndex: 3,
}],
};
const expectedBlocks = [{
Expand All @@ -2260,15 +2244,7 @@ describe('Appointments helpers', () => {
minOffset: 0,
maxOffset: 2,
size: 3,
items: [4, 5],
}, {
start: moment('2020-05-07 10:15'),
end: moment('2020-05-07 10:45'),
endForChildren: moment('2020-05-07 10:45'),
minOffset: 2,
maxOffset: 2,
size: 1,
items: [6],
items: [4, 5, 6],
}, {
start: moment('2020-05-07 10:30'),
end: moment('2020-05-07 10:45'),
Expand All @@ -2294,16 +2270,25 @@ describe('Appointments helpers', () => {
end: moment('2020-05-07 09:00'),
minOffset: 0,
maxOffset: 5,
items: [],
}, {
start: moment('2020-05-07 09:00'),
end: moment('2020-05-07 12:00'),
minOffset: 4,
maxOffset: 5,
items: [],
}, {
start: moment('2020-05-07 10:00'),
end: moment('2020-05-07 12:00'),
minOffset: 1,
maxOffset: 4,
items: [],
}, {
start: moment('2020-05-07 17:00'),
end: moment('2020-05-07 18:00'),
minOffset: 1,
maxOffset: 4,
items: [1],
}];
const appointments = [{
data: { offset: 0, start: moment('2020-05-07 08:00') },
Expand All @@ -2315,6 +2300,12 @@ describe('Appointments helpers', () => {
data: { offset: 2, start: moment('2020-05-07 11:00') },
}, {
data: { offset: 3, start: moment('2020-05-07 13:00') },
}, {
data: { offset: 3, start: moment('2020-05-07 11:00') },
overlappingSubTreeRoot: true,
}, {
data: { offset: 2, start: moment('2020-05-07 17:00') },
overlappingSubTreeRoot: true,
}];

expect(findBlockIndexByAppointment(blocks, appointments[0]))
Expand All @@ -2326,7 +2317,11 @@ describe('Appointments helpers', () => {
expect(findBlockIndexByAppointment(blocks, appointments[3]))
.toBe(2);
expect(findBlockIndexByAppointment(blocks, appointments[4]))
.toBe(0);
.toBe(-1);
expect(findBlockIndexByAppointment(blocks, appointments[5]))
.toBe(2);
expect(findBlockIndexByAppointment(blocks, appointments[6]))
.toBe(-1);
});
});

Expand Down
42 changes: 22 additions & 20 deletions packages/dx-scheduler-core/src/plugins/appointments/helpers.ts
Expand Up @@ -647,31 +647,31 @@ export const groupAppointmentsIntoBlocks: GroupAppointmentsIntoBlocksFn =
const { blocks: nextBlocks, appointments } = items.reduce((acc, appointment, index) => {
const blocks = acc.blocks.slice();
const {
treeDepth, data, overlappingSubTreeRoots, overlappingSubTreeRoot,
treeDepth, data, overlappingSubTreeRoots,
} = appointment;
const { offset, start, end } = data;
let blockIndex = findBlockIndexByAppointment(blocks, appointment);

if (overlappingSubTreeRoots.length !== 0) {
if (!overlappingSubTreeRoot) {
blocks.push({
start, end, minOffset: offset, maxOffset: offset + treeDepth,
size: treeDepth + 1, items: [], endForChildren: end,
});
}
overlappingSubTreeRoots.forEach((subTreeRootIndex) => {
const subTreeRoot = items[subTreeRootIndex];
const { data: subTreeRootData } = subTreeRoot;
blocks.push({
start: subTreeRootData.start, end,
minOffset: subTreeRootData.offset, maxOffset: offset - 1,
size: calculateBlockSizeByEndDate(items, subTreeRoot, end), items: [],
endForChildren: subTreeRootData.end,
});
if (blockIndex < 0) {
blocks.push({
start, end, minOffset: offset, maxOffset: offset + treeDepth,
size: treeDepth + 1, items: [], endForChildren: end,
});
blockIndex = blocks.length - 1;
}

const blockIndex = findBlockIndexByAppointment(blocks, appointment);
blocks[blockIndex].items.push(index);

overlappingSubTreeRoots.forEach((subTreeRootIndex) => {
const subTreeRoot = items[subTreeRootIndex];
const { data: subTreeRootData } = subTreeRoot;
blocks.push({
start: subTreeRootData.start, end,
minOffset: subTreeRootData.offset, maxOffset: offset - 1,
size: calculateBlockSizeByEndDate(items, subTreeRoot, end), items: [],
endForChildren: subTreeRootData.end,
});
});
const appointmentInBlock = { ...appointment, blockIndex };

return {
Expand Down Expand Up @@ -729,10 +729,12 @@ export const findBlockIndexByAppointment: FindBlockIndexByAppointmentFn = (
const { start, offset } = appointment.data;

let blockIndex = blocks.length - 1;
while (blockIndex > 0) {
while (blockIndex >= 0) {
const currentBlock = blocks[blockIndex];
if (intervalIncludes(currentBlock.start, currentBlock.end, start)
&& offset >= currentBlock.minOffset && offset <= currentBlock.maxOffset
&& offset >= currentBlock.minOffset && offset <= currentBlock.maxOffset &&
(!appointment.overlappingSubTreeRoot ||
appointment.overlappingSubTreeRoot && !currentBlock.items.length)
) {
break;
}
Expand Down

0 comments on commit d2fdbb8

Please sign in to comment.