fix(events): seven-date default period and picker from today (WealthLedger/frontend#63)
This commit is contained in:
@@ -38,11 +38,11 @@ describe("calendar dates", () => {
|
||||
expect(moscowDate(new Date("2026-12-31T21:00:00Z"))).toBe("2027-01-01");
|
||||
});
|
||||
|
||||
it("defaults to the last 7 days including today", () => {
|
||||
it("defaults to 7 calendar dates starting with today", () => {
|
||||
const [from, to] = defaultPeriod();
|
||||
|
||||
expect(calendarDate.safeParse(from).success).toBe(true);
|
||||
expect(to).toBe(moscowDate(new Date()));
|
||||
expect(from).toBe(moscowDate(new Date()));
|
||||
expect((Date.parse(to) - Date.parse(from)) / 86_400_000).toBe(6);
|
||||
expect(from <= to).toBe(true);
|
||||
});
|
||||
|
||||
@@ -9,8 +9,7 @@ function isValidPeriod(from: null | string, to: null | string) {
|
||||
);
|
||||
}
|
||||
|
||||
const DAY_MS = 86_400_000;
|
||||
const DEFAULT_PERIOD_DAYS = 6;
|
||||
const DEFAULT_PERIOD_LAST_INDEX = 6;
|
||||
|
||||
const moscowFormatter = new Intl.DateTimeFormat("en-CA", {
|
||||
day: "2-digit",
|
||||
@@ -25,12 +24,17 @@ function compareDateDesc(a: null | string, b: null | string) {
|
||||
return (b ?? "").localeCompare(a ?? "");
|
||||
}
|
||||
|
||||
// Last 7 days including today. An empty period always means this range.
|
||||
// 7 calendar dates starting with today (Moscow calendar arithmetic).
|
||||
function defaultPeriod(): [string, string] {
|
||||
const to = new Date();
|
||||
const from = new Date(to.getTime() - DEFAULT_PERIOD_DAYS * DAY_MS);
|
||||
const from = moscowDate(new Date());
|
||||
|
||||
return [moscowDate(from), moscowDate(to)];
|
||||
const [year = 1970, month = 0, day = 1] = from.split("-").map(Number);
|
||||
const noon = 12;
|
||||
const lastDay = day + DEFAULT_PERIOD_LAST_INDEX;
|
||||
const end = new Date(Date.UTC(year, month - 1, lastDay, noon));
|
||||
const to = moscowDate(end);
|
||||
|
||||
return [from, to];
|
||||
}
|
||||
|
||||
function moscowDate(value: Date): string {
|
||||
|
||||
Reference in New Issue
Block a user