-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85558da
Implement a new library for opening hours
Dlurak 8cb53a7
Run prettier
Dlurak e0c51cf
Fix a problem with a file path
Dlurak dee4808
Address some reviews
Dlurak 32c10fe
Address the latest review
Dlurak 1f8913a
Merge remote-tracking branch 'origin/master' into openingHours
zbycz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/components/FeaturePanel/renderers/openingHours/complex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import OpeningHours from 'opening_hours'; | ||
import { isInRange } from './utils'; | ||
import { Address, 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, | ||
lat: number, | ||
lon: number, | ||
address: Address, | ||
) => { | ||
const oh = new OpeningHours(value, { | ||
lat, | ||
lon, | ||
address, | ||
}); | ||
|
||
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())), | ||
}; | ||
}; |
15 changes: 15 additions & 0 deletions
15
src/components/FeaturePanel/renderers/openingHours/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parseComplexOpeningHours } from './complex'; | ||
import { Address } from './types'; | ||
|
||
export const parseOpeningHours = ( | ||
value: string, | ||
lat: number, | ||
lon: number, | ||
address: Address, | ||
): ReturnType<typeof parseComplexOpeningHours> | null => { | ||
try { | ||
return parseComplexOpeningHours(value, lat, lon, address); | ||
} catch { | ||
return null; | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
src/components/FeaturePanel/renderers/openingHours/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface SimpleOpeningHoursTable { | ||
su: string[]; | ||
mo: string[]; | ||
tu: string[]; | ||
we: string[]; | ||
th: string[]; | ||
fr: string[]; | ||
sa: string[]; | ||
ph: string[]; | ||
} | ||
|
||
export type Address = { | ||
country_code: string; | ||
state: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is probably a typo
center[0], center[0]
, right?To prevent mistakes like these, do you think you can change the signature of
parseOpeningHours()
to acceptLonLat
type instead of two parameters?