fix(events): seven-date default period and picker from today (WealthLedger/frontend#63)
This commit is contained in:
@@ -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