dedupe date pills when event occurs several times the same day
This commit is contained in:
@ -5,9 +5,11 @@ import {
|
|||||||
compareAsc,
|
compareAsc,
|
||||||
addDays,
|
addDays,
|
||||||
isSameDay,
|
isSameDay,
|
||||||
|
startOfDay,
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
import { nb } from "date-fns/locale";
|
import { nb } from "date-fns/locale";
|
||||||
import { toZonedTime, format } from "date-fns-tz";
|
import { toZonedTime, format } from "date-fns-tz";
|
||||||
|
import { unique } from "./common";
|
||||||
|
|
||||||
const timeZone = "Europe/Oslo";
|
const timeZone = "Europe/Oslo";
|
||||||
|
|
||||||
@ -66,17 +68,19 @@ export function isConsecutiveDays(a: Date, b: Date): boolean {
|
|||||||
|
|
||||||
export function groupConsecutiveDates(dates: string[]): string[][] {
|
export function groupConsecutiveDates(dates: string[]): string[][] {
|
||||||
const groupedDates: string[][] = [];
|
const groupedDates: string[][] = [];
|
||||||
|
const uniqueDays = unique(dates.map(date => format(startOfDay(toLocalTime(date)), 'yyyy-MM-dd')))
|
||||||
let group: string[] = [];
|
let group: string[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < dates.length; i++) {
|
for (let i = 0; i < uniqueDays.length; i++) {
|
||||||
if (
|
if (
|
||||||
i === 0 ||
|
i === 0 ||
|
||||||
isConsecutiveDays(toLocalTime(dates[i - 1]), toLocalTime(dates[i]))
|
isSameDay(uniqueDays[i - 1], uniqueDays[i]) ||
|
||||||
|
isConsecutiveDays(uniqueDays[i - 1], uniqueDays[i])
|
||||||
) {
|
) {
|
||||||
group.push(dates[i]);
|
group.push(uniqueDays[i]);
|
||||||
} else {
|
} else {
|
||||||
groupedDates.push(group);
|
groupedDates.push(group);
|
||||||
group = [dates[i]];
|
group = [uniqueDays[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user