Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a new library for opening hours #330

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"next-codegrid": "^1.0.2",
"next-cookies": "^2.0.3",
"next-pwa": "^5.2.21",
"opening_hours": "^3.8.0",
"osm-auth": "^2.4.0",
"react": "^18.2.0",
"react-custom-scrollbars": "^4.2.1",
Expand Down
24 changes: 3 additions & 21 deletions src/components/FeaturePanel/renderers/OpeningHoursRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import React from 'react';
import { SimpleOpeningHours } from 'simple-opening-hours';
import styled from 'styled-components';
import AccessTime from '@material-ui/icons/AccessTime';
import { useToggleState } from '../../helpers';
import { t } from '../../../services/intl';
import { ToggleButton } from '../helpers/ToggleButton';

interface SimpleOpeningHoursTable {
su: string[];
mo: string[];
tu: string[];
we: string[];
th: string[];
fr: string[];
sa: string[];
ph: string[];
}

const parseOpeningHours = (value) => {
const sanitized = value.match(/^[0-9:]+-[0-9:]+$/) ? `Mo-Su ${value}` : value;
const opening = new SimpleOpeningHours(sanitized);
const daysTable = opening.getTable() as SimpleOpeningHoursTable;
const isOpen = opening.isOpenNow();
return { daysTable, isOpen };
};
import { parseOpeningHours } from './openingHours';
import { SimpleOpeningHoursTable } from './openingHours/types';

const Table = styled.table`
margin: 1em;
Expand All @@ -39,7 +21,7 @@ const Table = styled.table`
// const weekDays = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'];
const weekDays = t('opening_hours.days_su_mo_tu_we_th_fr_sa').split('|');

const formatTimes = (times) =>
const formatTimes = (times: string[]) =>
times.length ? times.map((x) => x.replace(/:00/g, '')).join(', ') : '-';

const formatDescription = (isOpen: boolean, days: SimpleOpeningHoursTable) => {
Expand Down
53 changes: 53 additions & 0 deletions src/components/FeaturePanel/renderers/openingHours/complex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import OpeningHours from 'opening_hours';
import { isInRange } from './utils';
import { SimpleOpeningHoursTable } from './types';

type Weekday = keyof SimpleOpeningHoursTable;
const weekdays: Weekday[] = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'ph'];
const weekdayMappings: Record<string, Weekday> = {
Sun: 'su',
Mon: 'mo',
Tue: 'tu',
Wed: 'we',
Thu: 'th',
Fri: 'fr',
Sat: 'sa',
};

const fmtDate = (d: Date) =>
d.toLocaleTimeString(undefined, { hour: 'numeric', minute: 'numeric' });

export const parseComplexOpeningHours = (value: string) => {
const oh = new OpeningHours(value);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add the feature.countryCode here? would it help to find the holidays?

You can get it by useFeatureContext() in <OpeningHoursRenderer>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though it wouldn't work without a state but I just checked and it works quite good with only the country and coordinates (which are used for sunsets)


const today = new Date();
today.setHours(0, 0, 0, 0);

const oneWeekLater = new Date(today);
oneWeekLater.setDate(oneWeekLater.getDate() + 7);

const intervals = oh.getOpenIntervals(today, oneWeekLater);
const grouped = weekdays.map((w) => {
const daysIntervals = intervals.filter(
([from]) =>
w === weekdayMappings[from.toLocaleString('en', { weekday: 'short' })],
);

return [w, daysIntervals] as const;
});

const daysTable = Object.fromEntries(
grouped.map((entry) => {
const strings = entry[1].map(
([from, due]) => `${fmtDate(from)}-${fmtDate(due)}`,
);

return [entry[0], strings] as const;
}),
) as unknown as SimpleOpeningHoursTable;

return {
daysTable,
isOpen: intervals.some(([from, due]) => isInRange([from, due], new Date())),
};
};
10 changes: 10 additions & 0 deletions src/components/FeaturePanel/renderers/openingHours/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { parseComplexOpeningHours } from './complex';
import { parseSimpleOpeningHours } from './simple';

export const parseOpeningHours = (value: string) => {
try {
return parseComplexOpeningHours(value);
} catch {
return parseSimpleOpeningHours(value);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect this exception more often? I would say we can discard the simple-opening-hours lib altogether. If there is an exception, just return null and log the exception to sentry.. what do you think?

If we do this it can also simplify the <OpeningHoursRenderer> component to tailor it exactly to the new lib needs.

}
};
10 changes: 10 additions & 0 deletions src/components/FeaturePanel/renderers/openingHours/simple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SimpleOpeningHours } from 'simple-opening-hours';
import { SimpleOpeningHoursTable } from './types';

export const parseSimpleOpeningHours = (value: string) => {
const sanitized = value.match(/^[0-9:]+-[0-9:]+$/) ? `Mo-Su ${value}` : value;
const opening = new SimpleOpeningHours(sanitized);
const daysTable = opening.getTable() as SimpleOpeningHoursTable;
const isOpen = opening.isOpenNow();
return { daysTable, isOpen };
};
10 changes: 10 additions & 0 deletions src/components/FeaturePanel/renderers/openingHours/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface SimpleOpeningHoursTable {
su: string[];
mo: string[];
tu: string[];
we: string[];
th: string[];
fr: string[];
sa: string[];
ph: string[];
}
2 changes: 2 additions & 0 deletions src/components/FeaturePanel/renderers/openingHours/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isInRange = ([startDate, endDate]: [Date, Date], date: Date) =>
date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime();
40 changes: 40 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,13 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.17.2", "@babel/runtime@^7.19.0":
version "7.24.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.24.5.tgz#230946857c053a36ccc66e1dd03b17dd0c4ed02c"
integrity sha512-Nms86NXrsaeU9vbBJKni6gXiEXZ4CVpYVzEjDH9Sb8vmZ3UljyA1GSOJl/6LGPO8EHLuSF9H+IxNXHPX8QHJ4g==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.12.13", "@babel/template@^7.18.10", "@babel/template@^7.20.7":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
Expand Down Expand Up @@ -4203,6 +4210,20 @@ hyphenate-style-name@^1.0.3:
resolved "https://registry.yarnpkg.com/hyphenate-style-name/-/hyphenate-style-name-1.0.4.tgz#691879af8e220aea5750e8827db4ef62a54e361d"
integrity sha512-ygGZLjmXfPHj+ZWh6LwbC37l43MhfztxetbFCoYTM2VjkIUpeHgSNn7QIyVFj7YQ1Wl9Cbw5sholVJPzWvC2MQ==

i18next-browser-languagedetector@^6.1.4:
version "6.1.8"
resolved "https://registry.yarnpkg.com/i18next-browser-languagedetector/-/i18next-browser-languagedetector-6.1.8.tgz#8e9c61b32a4dfe9b959b38bc9d2a8b95f799b27c"
integrity sha512-Svm+MduCElO0Meqpj1kJAriTC6OhI41VhlT/A0UPjGoPZBhAHIaGE5EfsHlTpgdH09UVX7rcc72pSDDBeKSQQA==
dependencies:
"@babel/runtime" "^7.19.0"

i18next@^21.8.3:
version "21.10.0"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-21.10.0.tgz#85429af55fdca4858345d0e16b584ec29520197d"
integrity sha512-YeuIBmFsGjUfO3qBmMOc0rQaun4mIpGKET5WDwvu8lU7gvwpcariZLNtL0Fzj+zazcHUrlXHiptcFhBMFaxzfg==
dependencies:
"@babel/runtime" "^7.17.2"

iconv-lite@0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -5938,6 +5959,15 @@ opencollective-postinstall@^2.0.2:
resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz#7a0fff978f6dbfa4d006238fbac98ed4198c3259"
integrity sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==

opening_hours@^3.8.0:
version "3.8.0"
resolved "https://registry.yarnpkg.com/opening_hours/-/opening_hours-3.8.0.tgz#e6d0a0bfd4e0f2cb8f62321e468dcaa8a798bd79"
integrity sha512-bRJroECQSe/itVcNmC3j9PPicxn/LBowdd1Hi+4Aa7hCswdt7w81WHfUwrEMbtk1BBYmGJEbSepl8oYYPviSuA==
dependencies:
i18next "^21.8.3"
i18next-browser-languagedetector "^6.1.4"
suncalc "^1.9.0"

optionator@^0.8.1:
version "0.8.3"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.3.tgz#84fa1d036fe9d3c7e21d99884b601167ec8fb495"
Expand Down Expand Up @@ -6475,6 +6505,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.4:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regenerator-transform@^0.15.1:
version "0.15.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.15.1.tgz#f6c4e99fc1b4591f780db2586328e4d9a9d8dc56"
Expand Down Expand Up @@ -7134,6 +7169,11 @@ stylis@3.5.4:
resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.5.4.tgz#f665f25f5e299cf3d64654ab949a57c768b73fbe"
integrity sha512-8/3pSmthWM7lsPBKv7NXkzn2Uc9W7NotcwGNpJaa3k7WMM1XDCA4MgT5k/8BIexd5ydZdboXtU90XH9Ec4Bv/Q==

suncalc@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/suncalc/-/suncalc-1.9.0.tgz#26212353fae61edb287c2d558fc4932ecf0e1532"
integrity sha512-vMJ8Byp1uIPoj+wb9c1AdK4jpkSKVAywgHX0lqY7zt6+EWRRC3Z+0Ucfjy/0yxTVO1hwwchZe4uoFNqrIC24+A==

supercluster@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/supercluster/-/supercluster-8.0.1.tgz#9946ba123538e9e9ab15de472531f604e7372df5"
Expand Down
Loading