add support for opening hours that automatically change over time
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { isToday, isAfter, parse } from "date-fns";
|
||||
import { isToday, isAfter, parse, compareAsc } from "date-fns";
|
||||
import { nb } from "date-fns/locale";
|
||||
import { toZonedTime, format } from "date-fns-tz";
|
||||
|
||||
@ -47,3 +47,7 @@ export function isTodayOrFuture(
|
||||
const zonedDate = toLocalTime(date);
|
||||
return isToday(zonedDate) || isAfter(zonedDate, zonedNow);
|
||||
}
|
||||
|
||||
export function compareDates(a: Date | string, b: Date | string) {
|
||||
return compareAsc(new Date(a), new Date(b));
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
import {
|
||||
compareAsc,
|
||||
endOfWeek,
|
||||
startOfToday,
|
||||
startOfWeek,
|
||||
@ -8,10 +7,10 @@ import {
|
||||
addWeeks,
|
||||
parseISO,
|
||||
} from "date-fns";
|
||||
import { toLocalTime, formatDate } from "./date";
|
||||
import { toLocalTime, formatDate, compareDates } from "./date";
|
||||
import { graphql } from "@/gql";
|
||||
import { EventFragment, EventCategory, EventOccurrence } from "@/gql/graphql";
|
||||
import { PIG_NAMES, PigName, randomElement } from "@/lib/common";
|
||||
import { EventFragment, EventOccurrence } from "@/gql/graphql";
|
||||
import { PIG_NAMES, randomElement } from "@/lib/common";
|
||||
|
||||
export type {
|
||||
EventFragment,
|
||||
@ -134,10 +133,6 @@ export function getSingularEvents(events: EventFragment[]): SingularEvent[] {
|
||||
.flat();
|
||||
}
|
||||
|
||||
export function compareDates(a: Date | string, b: Date | string) {
|
||||
return compareAsc(new Date(a), new Date(b));
|
||||
}
|
||||
|
||||
export function sortSingularEvents(events: SingularEvent[]) {
|
||||
return events.sort((a, b) =>
|
||||
compareDates(a.occurrence.start, b.occurrence.start)
|
||||
|
258
web/src/lib/openinghours.ts
Normal file
258
web/src/lib/openinghours.ts
Normal file
@ -0,0 +1,258 @@
|
||||
import {
|
||||
startOfToday,
|
||||
isAfter,
|
||||
parseISO,
|
||||
isSameDay,
|
||||
compareDesc,
|
||||
} from "date-fns";
|
||||
|
||||
import { graphql } from "@/gql";
|
||||
import {
|
||||
OpeningHoursRangeBlock,
|
||||
OpeningHoursSet,
|
||||
OpeningHoursWeekBlock,
|
||||
} from "@/gql/graphql";
|
||||
import { getClient } from "@/app/client";
|
||||
|
||||
const MISSING_OPENING_HOURS = {
|
||||
name: "Åpningstider mangler",
|
||||
effectiveFrom: "",
|
||||
effectiveTo: null,
|
||||
announcement: "Åpningstider mangler",
|
||||
items: [],
|
||||
};
|
||||
|
||||
const WEEKDAYS = [
|
||||
"monday",
|
||||
"tuesday",
|
||||
"wednesday",
|
||||
"thursday",
|
||||
"friday",
|
||||
"saturday",
|
||||
"sunday",
|
||||
];
|
||||
const WEEKDAYS_NORWEGIAN = [
|
||||
"mandag",
|
||||
"tirsdag",
|
||||
"onsdag",
|
||||
"torsdag",
|
||||
"fredag",
|
||||
"øørdag",
|
||||
"søndag",
|
||||
];
|
||||
|
||||
const openingHoursQuery = graphql(`
|
||||
query openingHoursSets {
|
||||
openingHoursSets {
|
||||
...OpeningHoursSetFragment
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
export async function fetchOpeningHoursSets() {
|
||||
const { data, error } = await getClient().query(openingHoursQuery, {});
|
||||
const sets = (data?.openingHoursSets ?? []) as OpeningHoursSet[];
|
||||
return sets;
|
||||
}
|
||||
|
||||
export async function getOpeningHours() {
|
||||
const today = startOfToday();
|
||||
const sets = await fetchOpeningHoursSets();
|
||||
const validSets = sets
|
||||
.filter((set) => {
|
||||
const from = parseISO(set.effectiveFrom);
|
||||
return isAfter(today, from) || isSameDay(today, from);
|
||||
})
|
||||
.filter((set) => {
|
||||
if (!set.effectiveTo) {
|
||||
return true;
|
||||
}
|
||||
const to = parseISO(set.effectiveTo);
|
||||
return isAfter(to, today) || isSameDay(today, to);
|
||||
});
|
||||
if (validSets.length === 0) {
|
||||
return MISSING_OPENING_HOURS as OpeningHoursSet;
|
||||
}
|
||||
if (validSets.length === 1) {
|
||||
return validSets[0];
|
||||
}
|
||||
// pick the set that msot recently took effect
|
||||
return validSets.sort((a, b) =>
|
||||
compareDesc(a.effectiveFrom, b.effectiveFrom)
|
||||
)[0];
|
||||
}
|
||||
|
||||
type OpeningHoursGroup = {
|
||||
days: string[];
|
||||
timeFrom: string | null;
|
||||
timeTo: string | null;
|
||||
custom: string | null;
|
||||
};
|
||||
|
||||
type OpeningHoursPerDay = Record<string, OpeningHoursRangeBlock>
|
||||
|
||||
export function groupOpeningHours(week: OpeningHoursPerDay): OpeningHoursGroup[] {
|
||||
const grouped: OpeningHoursGroup[] = [];
|
||||
let previous: string | null = null;
|
||||
|
||||
for (const day of WEEKDAYS) {
|
||||
if (!week.hasOwnProperty(day)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const hours = week[day];
|
||||
if (
|
||||
hours === null ||
|
||||
previous === null ||
|
||||
week[previous]?.timeFrom !== hours.timeFrom ||
|
||||
week[previous]?.timeTo !== hours.timeTo ||
|
||||
week[previous]?.custom !== hours.custom
|
||||
) {
|
||||
grouped.push({
|
||||
days: [day],
|
||||
timeFrom: hours.timeFrom ?? null,
|
||||
timeTo: hours.timeTo ?? null,
|
||||
custom: hours.custom ?? null,
|
||||
});
|
||||
} else {
|
||||
grouped[grouped.length - 1].days.push(day);
|
||||
}
|
||||
previous = day;
|
||||
}
|
||||
return grouped;
|
||||
}
|
||||
|
||||
export type PrettyOpeningHours = {
|
||||
range: string;
|
||||
time?: string;
|
||||
custom?: string;
|
||||
};
|
||||
|
||||
function formatGroupedHours(
|
||||
grouped: OpeningHoursGroup[]
|
||||
): PrettyOpeningHours[] {
|
||||
return grouped.map((group) => {
|
||||
const startDayIndex = WEEKDAYS.indexOf(group.days[0]);
|
||||
const endDayIndex = WEEKDAYS.indexOf(group.days[group.days.length - 1]);
|
||||
|
||||
const startDayName = WEEKDAYS_NORWEGIAN[startDayIndex];
|
||||
const endDayName =
|
||||
group.days.length > 1 ? WEEKDAYS_NORWEGIAN[endDayIndex] : "";
|
||||
|
||||
const rangeName = startDayName + (endDayName ? " - " + endDayName : "");
|
||||
|
||||
const formattedRange = {
|
||||
range: rangeName,
|
||||
...(group.timeFrom && group.timeTo
|
||||
? {
|
||||
time: `${group.timeFrom.slice(0, 5)} - ${group.timeTo.slice(0, 5)}`,
|
||||
}
|
||||
: {}),
|
||||
...(group.custom ? { custom: group.custom } : {}),
|
||||
};
|
||||
|
||||
return formattedRange;
|
||||
});
|
||||
}
|
||||
|
||||
export function getOpeningHoursForFunction(
|
||||
openingHours: OpeningHoursSet,
|
||||
name: string
|
||||
) {
|
||||
const item = openingHours.items?.find((x) => x?.function === name);
|
||||
if (!item || !Array.isArray(item?.week) || item?.week.length !== 1) {
|
||||
return;
|
||||
}
|
||||
const week = item.week[0] as OpeningHoursWeekBlock;
|
||||
return week;
|
||||
}
|
||||
|
||||
export function getPrettyOpeningHoursForFunction(
|
||||
openingHours: OpeningHoursSet,
|
||||
name: string
|
||||
) {
|
||||
const week = getOpeningHoursForFunction(openingHours, name);
|
||||
if (!week) {
|
||||
return [];
|
||||
}
|
||||
// just trying to satisfy the type checker, this is crap
|
||||
const perDay: OpeningHoursPerDay = {
|
||||
monday: week.monday as OpeningHoursRangeBlock,
|
||||
tuesday: week.tuesday as OpeningHoursRangeBlock,
|
||||
wednesday: week.wednesday as OpeningHoursRangeBlock,
|
||||
thursday: week.thursday as OpeningHoursRangeBlock,
|
||||
friday: week.friday as OpeningHoursRangeBlock,
|
||||
saturday: week.friday as OpeningHoursRangeBlock,
|
||||
sunday: week.friday as OpeningHoursRangeBlock,
|
||||
}
|
||||
const grouped = groupOpeningHours(perDay);
|
||||
return formatGroupedHours(grouped);
|
||||
}
|
||||
|
||||
const OpeningHoursSetFragmentDefinition = graphql(`
|
||||
fragment OpeningHoursSetFragment on OpeningHoursSet {
|
||||
name
|
||||
effectiveFrom
|
||||
effectiveTo
|
||||
announcement
|
||||
items {
|
||||
id
|
||||
function
|
||||
week {
|
||||
id
|
||||
blockType
|
||||
... on OpeningHoursWeekBlock {
|
||||
...OpeningHoursWeekBlock
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
const OpeningHoursRangeBlockFragmentDefinition = graphql(`
|
||||
fragment OpeningHoursRangeBlock on OpeningHoursRangeBlock {
|
||||
timeFrom
|
||||
timeTo
|
||||
custom
|
||||
}
|
||||
`);
|
||||
|
||||
const OpeningHoursWeekBlockFragmentDefinition = graphql(`
|
||||
fragment OpeningHoursWeekBlock on OpeningHoursWeekBlock {
|
||||
monday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
tuesday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
wednesday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
thursday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
friday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
saturday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
sunday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
...OpeningHoursRangeBlock
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
Reference in New Issue
Block a user