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

[ZMA-1042] Fix time picker when using the app after closing hour #6

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions src/pages/cart/time-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { displayDate, displayHalfAnHourTimeRange } from "utils/date";
import { matchStatusBarColor } from "utils/device";
import { Picker } from "zmp-ui";

// Opening hours: 7:00 - 21:00
const OPENING_HOUR = 7;
const CLOSING_HOUR = 21;

export const TimePicker: FC = () => {
const [date, setDate] = useState(+new Date());
const [time, setTime] = useRecoilState(selectedDeliveryTimeState);

const availableDates = useMemo(() => {
const days: Date[] = [];
const today = new Date();
for (let i = 0; i < 5; i++) {
for (let i = today.getHours() >= CLOSING_HOUR ? 1 : 0; i < 5; i++) {
const nextDay = new Date(today);
nextDay.setDate(today.getDate() + i);
days.push(nextDay);
Expand All @@ -30,14 +34,13 @@ export const TimePicker: FC = () => {
time.setHours(now.getHours());
time.setMinutes(minutes);
} else {
// Starting time is 7:00
time.setHours(7);
time.setHours(OPENING_HOUR);
time.setMinutes(0);
}
time.setSeconds(0);
time.setMilliseconds(0);
const endTime = new Date();
endTime.setHours(21);
endTime.setHours(CLOSING_HOUR);
endTime.setMinutes(0);
endTime.setSeconds(0);
endTime.setMilliseconds(0);
Expand Down Expand Up @@ -65,7 +68,7 @@ export const TimePicker: FC = () => {
formatPickedValueDisplay={({ date, time }) =>
date && time
? `${displayHalfAnHourTimeRange(new Date(time.value))}, ${displayDate(
new Date(date.value),
new Date(date.value)
)}`
: `Chọn thời gian`
}
Expand Down
23 changes: 18 additions & 5 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
export function isToday(date: Date) {
const today = new Date();
function areSameDay(date1: Date, date2: Date): boolean {
return (
date.getFullYear() === today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate()
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
}

export function isToday(date: Date): boolean {
const today = new Date();
return areSameDay(date, today);
}

export function isTomorrow(date: Date): boolean {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return areSameDay(date, tomorrow);
}

export function displayTime(date: Date) {
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
Expand All @@ -26,5 +36,8 @@ export function displayDate(date: Date, hint?: boolean) {
if (hint && isToday(date)) {
return `Hôm nay - ${day}/${month}/${year}`;
}
if (hint && isTomorrow(date)) {
return `Ngày mai - ${day}/${month}/${year}`;
}
return `${day}/${month}/${year}`;
}
Loading