Skip to content

Commit

Permalink
SUL23-597 Fix hour displays for next open date/time (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Aug 12, 2024
1 parent c032c35 commit 6af5374
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/components/node/stanford-event/page-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => {
start.getDate() === end.getDate() &&
start.getFullYear() === end.getFullYear()
) {
dateTimeString = start.toLocaleDateString("en-us", {weekday: "long"})
dateTimeString = start.toLocaleDateString("en-us", {weekday: "long", timeZone: "America/Los_Angeles"})
dateTimeString +=
", " +
start.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})

if (start.getHours() !== 0 || start.getMinutes() !== 0 || end.getHours() !== 23 || end.getMinutes() !== 59) {
Expand All @@ -37,12 +38,14 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => {
start.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
timeZone: "America/Los_Angeles",
}) +
" - " +
end.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
timeZoneName: "short",
timeZone: "America/Los_Angeles",
})
} else {
dateTimeString +=
Expand All @@ -51,24 +54,27 @@ const StanfordEvent = async ({node, ...props}: {node: NodeStanfordEvent}) => {
hour: "numeric",
minute: "numeric",
timeZoneName: "short",
timeZone: "America/Los_Angeles",
})
}
}
} else {
// Multiple days
dateTimeString = start.toLocaleDateString("en-us", {weekday: "long"})
dateTimeString = start.toLocaleDateString("en-us", {weekday: "long", timeZone: "America/Los_Angeles"})
dateTimeString +=
", " +
start.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
}) +
" - " +
end.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})
}

Expand Down
1 change: 1 addition & 0 deletions src/components/node/stanford-news/page-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const StanfordNews = async ({node, ...props}: {node: NodeStanfordNews}) => {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/node/stanford-page/page-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const StanfordPage = async ({node}: {node: NodeStanfordPage}) => {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/node/stanford-person/page-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const StanfordPerson = async ({node, ...props}: {node: NodeStanfordPerson}) => {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/node/sul-library/page-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const SulLibrary = async ({node, ...props}: {node: NodeSulLibrary}) => {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "America/Los_Angeles",
})

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,14 @@ const BranchHours = ({hoursId}: {hoursId: string}) => {
new Date(dayHours.opens_at).toLocaleTimeString("en-us", {
hour: "numeric",
minute: "2-digit",
timeZone: "America/Los_Angeles",
})}
-
{dayHours.closes_at &&
new Date(dayHours.closes_at).toLocaleTimeString("en-us", {
hour: "numeric",
minute: "2-digit",
timeZone: "America/Los_Angeles",
})}
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,14 @@ const BranchHours = ({hoursId}: {hoursId: string}) => {
new Date(dayHours.opens_at).toLocaleTimeString("en-us", {
hour: "numeric",
minute: "2-digit",
timeZone: "America/Los_Angeles",
})}
-
{dayHours.closes_at &&
new Date(dayHours.closes_at).toLocaleTimeString("en-us", {
hour: "numeric",
minute: "2-digit",
timeZone: "America/Los_Angeles",
})}
</>
)}
Expand Down
45 changes: 31 additions & 14 deletions src/lib/hooks/useTodayLibraryHours.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useLibraryHours, {DayHours, LocationHours} from "@/lib/hooks/useLibraryHours"
import {getLibrarySelectOptions, HoursSelectOption} from "@/components/node/sul-library/library-select-options"
import {useIsClient} from "usehooks-ts"

type HoursProps = {
closedAllDay: boolean
Expand All @@ -13,7 +14,7 @@ type HoursProps = {

const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => {
const libraryHours = useLibraryHours<LocationHours>(branchId)
if (!libraryHours || !libraryHours?.primaryHours) {
if (!useIsClient() || !libraryHours || !libraryHours?.primaryHours) {
return
}

Expand All @@ -38,7 +39,7 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => {
if (!todayHours.closed && todayHours.opens_at && todayHours.closes_at) {
openTime = new Date(todayHours.opens_at)
closeTime = new Date(todayHours.closes_at)
isOpen = rightNow > openTime && rightNow < closeTime
isOpen = rightNow >= openTime && rightNow < closeTime
}

const closingTime = closeTime
Expand All @@ -65,26 +66,35 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => {

// If the location is open, no need to return the next open time.
if (!isOpen) {
const futureHours = libraryHours.primaryHours.filter(
const futureOpenHours = libraryHours.primaryHours.filter(
dayHours => dayHours.opens_at && new Date(dayHours.opens_at) > rightNow
)
const futureHourDateTime = new Date()

for (let i = 0; i < futureHours.length; i++) {
if (new Date(futureHours[i].opens_at as string).getDate() === rightNow.getDate()) {
nextOpenDateTime = new Date(futureHours[i].opens_at as string)
i = futureHours.length + 1
for (let i = 0; i < futureOpenHours.length; i++) {
if (futureOpenHours[i].opens_at) {
nextOpenDateTime = new Date(futureOpenHours[i].opens_at as string)
i = futureOpenHours.length + 1
}
futureHourDateTime.setDate(futureHourDateTime.getDate() + 1)
}

if (nextOpenDateTime) {
const format: Intl.DateTimeFormatOptions = {hour: "numeric"}
const format: Intl.DateTimeFormatOptions = {hour: "numeric", timeZone: "America/Los_Angeles"}

if (rightNow.getDay() + 2 <= nextOpenDateTime.getDate())
nextOpeningTime = nextOpenDateTime.toLocaleDateString("en-us", {weekday: "long"})
if (rightNow.getDate() + 1 === nextOpenDateTime.getDate()) nextOpeningTime = "tomorrow"
if (rightNow.getDate() === nextOpenDateTime.getDate()) nextOpeningTime = "today"
// Default to the show the day of the week.
nextOpeningTime = nextOpenDateTime.toLocaleDateString("en-us", {
weekday: "long",
timeZone: "America/Los_Angeles",
})

// Next open date is tomorrow. Check for tomorrow date and if it's the last day of the month.
if (
getDate(rightNow) + 1 === getDate(nextOpenDateTime) ||
(getMonth(rightNow) != getMonth(nextOpenDateTime) && getDate(nextOpenDateTime) === 1)
)
nextOpeningTime = "tomorrow"

// Next open date is today.
if (getDate(rightNow) === getDate(nextOpenDateTime)) nextOpeningTime = "today"

if (nextOpenDateTime.getMinutes() !== 0) format.minute = "2-digit"

Expand All @@ -95,4 +105,11 @@ const useTodayLibraryHours = (branchId?: string): HoursProps | undefined => {
return {closedAllDay, isOpen, openingTime, closingTime, selectOptions, afterClose, nextOpeningTime}
}

const getDate = (dateTime: Date) => {
return parseInt(dateTime.toLocaleDateString("en-us", {day: "numeric", timeZone: "America/Los_Angeles"}))
}
const getMonth = (dateTime: Date) => {
return parseInt(dateTime.toLocaleDateString("en-us", {month: "numeric", timeZone: "America/Los_Angeles"}))
}

export default useTodayLibraryHours

0 comments on commit 6af5374

Please sign in to comment.