105 lines
3.0 KiB
JavaScript
105 lines
3.0 KiB
JavaScript
const daysOfWeek = [
|
|
'Sunday',
|
|
'Monday',
|
|
'Tuesday',
|
|
'Wednesday',
|
|
'Thursday',
|
|
'Friday',
|
|
'Saturday',
|
|
];
|
|
|
|
// Function to find the next available time
|
|
const findNextAvailableTime = (
|
|
workingHours,
|
|
currentDayOfWeek,
|
|
currentHour,
|
|
currentMinutes
|
|
) => {
|
|
let nextAvailableDays = [];
|
|
let nextAvailableTime = null;
|
|
|
|
// Find the next available time for today
|
|
const nextAvailableTimeToday = workingHours.find(
|
|
workingHour =>
|
|
currentDayOfWeek === workingHour.day_of_week &&
|
|
!workingHour.closed_all_day &&
|
|
(currentHour < workingHour.close_hour ||
|
|
(currentHour === workingHour.close_hour &&
|
|
currentMinutes < workingHour.close_minutes))
|
|
);
|
|
|
|
if (nextAvailableTimeToday) {
|
|
nextAvailableTime = {
|
|
hour: nextAvailableTimeToday.open_hour,
|
|
minutes: nextAvailableTimeToday.open_minutes,
|
|
day: 'today',
|
|
};
|
|
}
|
|
|
|
// If no available time for today, find the next available time for the next day(s)
|
|
if (nextAvailableTime === null) {
|
|
nextAvailableDays = workingHours.filter(hours => !hours.closed_all_day);
|
|
|
|
for (let i = 0; i < 7; i += 1) {
|
|
const nextDayIndex = (currentDayOfWeek + 1 + i) % 7;
|
|
const nextDay = nextAvailableDays.find(
|
|
hours => hours.day_of_week === nextDayIndex
|
|
);
|
|
if (nextDay) {
|
|
nextAvailableTime = {
|
|
hour: nextDay.open_hour,
|
|
minutes: nextDay.open_minutes,
|
|
day: nextDay.day_of_week,
|
|
};
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return nextAvailableTime;
|
|
};
|
|
|
|
// Function to get the next availability message
|
|
export const getNextAvailabilityMessage = (workingHours, currentTime) => {
|
|
const currentDayOfWeek = currentTime.getDay(); // 0 for Sunday, 1 for Monday, ...
|
|
const currentHour = currentTime.getHours();
|
|
const currentMinutes = currentTime.getMinutes();
|
|
const nextAvailableTime = findNextAvailableTime(
|
|
workingHours,
|
|
currentDayOfWeek,
|
|
currentHour,
|
|
currentMinutes
|
|
);
|
|
|
|
// Calculate the time difference
|
|
if (nextAvailableTime !== null) {
|
|
const differenceInMinutes =
|
|
nextAvailableTime.hour * 60 +
|
|
nextAvailableTime.minutes -
|
|
(currentHour * 60 + currentMinutes);
|
|
const differenceInHours = Math.floor(differenceInMinutes / 60);
|
|
|
|
// Check if the next available time is tomorrow
|
|
if (nextAvailableTime.day === currentDayOfWeek + 1) {
|
|
return `We will be back online tomorrow`;
|
|
}
|
|
|
|
// Check if the next available time is on a specific day
|
|
if (nextAvailableTime.day !== 'today') {
|
|
return `We will be back online on ${daysOfWeek[nextAvailableTime.day]}`;
|
|
}
|
|
|
|
// Generate appropriate message based on time difference
|
|
if (differenceInMinutes <= 1) {
|
|
return 'We will be back online in less than a minute.';
|
|
}
|
|
if (differenceInMinutes < 60) {
|
|
return `We will be back online in ${differenceInMinutes} minutes.`;
|
|
}
|
|
if (differenceInMinutes < 1440) {
|
|
return `We will be back online in ${differenceInHours} hours`;
|
|
}
|
|
}
|
|
return 'No available time found in working hours.';
|
|
};
|