Skip to content

Commit ef68087

Browse files
authored
feat filter panel custom hour any (#2547)
* feat-filter-panel-custom-hour-any * remove dup lines * refactor to work with DB’s HH:mm:ss format, instead rely on getNextDateForDay. * refactor
1 parent 4c4808e commit ef68087

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

client/src/components/FoodSeeker/SearchResults/ResultsFilters/FilterPanel.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ const OPEN_TIME_DROPDOWNS = {
345345
time: {
346346
placeholder: "Open Time",
347347
labels: [
348+
"Any",
348349
"12:00AM",
349350
"01:00AM",
350351
"02:00AM",

client/src/helpers/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,31 @@ export const getNextDateForDay = (dayInput, timeInput, targetTimezone) => {
221221
const dayMap = { SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6 };
222222
const targetDay = dayMap[dayInput.toUpperCase()];
223223

224-
// Parse the input time
225-
let time = dayjs(timeInput, "hh:mmA");
224+
if (timeInput === "Any") {
225+
let currentDate = dayjs().tz(targetTimezone);
226+
let currentDay = currentDate.day();
227+
228+
let dayDifference = targetDay - currentDay;
229+
if (dayDifference < 0) dayDifference += 7;
230+
231+
return currentDate.add(dayDifference, "day").startOf("day");
232+
}
226233

227-
// Get the current date and time in the target timezone
234+
let time = dayjs(timeInput, "hh:mmA");
228235
let currentDate = dayjs().tz(targetTimezone);
229236
let currentDay = currentDate.day();
230237

231-
// Calculate the difference in days
232238
let dayDifference = targetDay - currentDay;
233239
if (dayDifference < 0) dayDifference += 7;
234240
if (dayDifference === 0 && currentDate.hour() > time.hour()) {
235241
dayDifference += 7;
236242
}
237243

238-
// Set the target date and time
239-
let targetDate = currentDate
244+
return currentDate
240245
.add(dayDifference, "day")
241246
.set("hour", time.hour())
242247
.set("minute", time.minute())
243248
.set("second", 0);
244-
245-
return targetDate;
246249
};
247250

248251
export const getDayTimeNow = () => {

client/src/hooks/useOrganizationBests.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { stakeholdersDaysHours } from "../components/FoodSeeker/SearchResults/StakeholderPreview/StakeholderPreview";
1414
import * as analytics from "../services/analytics";
1515
import * as stakeholderService from "../services/stakeholder-best-service";
16+
import dayjs from "dayjs";
1617

1718
const sortOrganizations = (a, b) => {
1819
if (
@@ -71,23 +72,35 @@ export default function useOrganizationBests() {
7172
}
7273

7374
if (filters.showActiveOnly) {
74-
// filter out inactive, inactiveTemporary stakeholders
7575
filteredStakeholders = filteredStakeholders.filter((stakeholder) => {
7676
return !stakeholder.inactive && !stakeholder.inactiveTemporary;
7777
});
7878
}
7979

8080
const { day, time } = filters.openTimeFilter;
81-
if (day !== "" && time !== "") {
81+
if (day || (time && time !== "Any")) {
8282
filteredStakeholders = filteredStakeholders.filter((stakeholder) => {
83-
const nextDateForDay = getNextDateForDay(day, time, tenantTimeZone);
84-
const hours = stakeholdersDaysHours(
85-
stakeholder,
86-
tenantTimeZone,
87-
nextDateForDay
88-
);
89-
90-
return !!hours;
83+
return stakeholder.hours?.some((h) => {
84+
const dayMatch = day ? h.day_of_week.toUpperCase() === day : true;
85+
86+
const timeMatch =
87+
!time || time === "Any"
88+
? true
89+
: (() => {
90+
const openTime = dayjs(h.open, "HH:mm:ss");
91+
const closeTime = dayjs(h.close, "HH:mm:ss");
92+
const filterTime = dayjs(time, "hh:mmA");
93+
94+
return (
95+
filterTime.isSame(openTime) ||
96+
filterTime.isSame(closeTime) ||
97+
(filterTime.isAfter(openTime) &&
98+
filterTime.isBefore(closeTime))
99+
);
100+
})();
101+
102+
return dayMatch && timeMatch;
103+
});
91104
});
92105
}
93106
if (filters.orgNameFilter) {

0 commit comments

Comments
 (0)