Skip to content

Commit 53270f8

Browse files
committed
Progress on converting DateRangeSeelector to use sveltekit-search-params
1 parent 6ad23b1 commit 53270f8

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

frontend/src/lib/components/DateRangeSelector/DateRangeSelector.svelte

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<script lang="ts">
2+
import { untrack } from 'svelte';
3+
import { queryParameters, ssp } from 'sveltekit-search-params';
4+
import type { DateRange } from 'bits-ui';
5+
import { DateFormatter, getLocalTimeZone, fromAbsolute } from '@internationalized/date';
6+
27
import {
38
DATE_RANGE_PARAM,
49
DATE_RANGES,
@@ -11,15 +16,21 @@
1116
import { Button } from '$lib/components/ui/button';
1217
import { Popover, PopoverContent, PopoverTrigger } from '$lib/components/ui/popover';
1318
import IconCalendarDateRange from '~icons/heroicons/calendar-date-range-16-solid';
14-
import type { DateRange } from 'bits-ui';
15-
import { DateFormatter, getLocalTimeZone, fromAbsolute } from '@internationalized/date';
19+
1620
import { cn } from '$lib/utils';
1721
import { RangeCalendar } from '$lib/components/ui/range-calendar/index';
18-
import { goto } from '$app/navigation';
1922
import { page } from '$app/stores';
20-
import { untrack } from 'svelte';
2123
import { parseDateRangeParams } from '$lib/util/date-ranges';
2224
25+
const params = queryParameters(
26+
{
27+
[DATE_RANGE_PARAM]: ssp.string(),
28+
[DATE_RANGE_START_PARAM]: ssp.number(),
29+
[DATE_RANGE_END_PARAM]: ssp.number()
30+
},
31+
{ pushHistory: false, showDefaults: false }
32+
);
33+
2334
const df = new DateFormatter('en-US', {
2435
dateStyle: 'medium'
2536
});
@@ -42,24 +53,21 @@
4253
if (newSelectedDateRange && newSelectedDateRange.start && newSelectedDateRange.end) {
4354
const startDate = newSelectedDateRange.start.toDate(getLocalTimeZone()).getTime();
4455
const endDate = newSelectedDateRange.end.toDate(getLocalTimeZone()).getTime();
45-
updateURLParams(CUSTOM, startDate.toString(), endDate.toString());
56+
updateURLParams(CUSTOM, startDate, endDate);
4657
calendarDateRangePopoverOpen = false;
4758
}
4859
}
4960
50-
function updateURLParams(range: string, start?: string, end?: string) {
51-
const url = new URL($page.url);
52-
url.searchParams.set(DATE_RANGE_PARAM, range);
61+
function updateURLParams(range: string, start?: number, end?: number) {
62+
params[DATE_RANGE_PARAM] = range;
5363
5464
if (range === CUSTOM && start && end) {
55-
url.searchParams.set(DATE_RANGE_START_PARAM, start);
56-
url.searchParams.set(DATE_RANGE_END_PARAM, end);
65+
params[DATE_RANGE_START_PARAM] = start;
66+
params[DATE_RANGE_END_PARAM] = end;
5767
} else {
58-
url.searchParams.delete(DATE_RANGE_START_PARAM);
59-
url.searchParams.delete(DATE_RANGE_END_PARAM);
68+
params[DATE_RANGE_START_PARAM] = null;
69+
params[DATE_RANGE_END_PARAM] = null;
6070
}
61-
62-
goto(url.toString(), { replaceState: true });
6371
}
6472
6573
$effect(() => {
@@ -68,11 +76,21 @@
6876
const { dateRangeValue, startTimestamp, endTimestamp } = parseDateRangeParams(
6977
url.searchParams
7078
);
79+
// console.log({ dateRangeValue, startTimestamp, endTimestamp });
80+
// console.log(JSON.parse(JSON.stringify(params)));
81+
82+
// TODO: Use `params` and handle converting non-custom ranges (ex. "Past 1 Month")
7183
selectDateRange = getDateRangeByValue(dateRangeValue);
7284
calendarDateRange = {
7385
start: fromAbsolute(Number(startTimestamp), getLocalTimeZone()),
7486
end: fromAbsolute(Number(endTimestamp), getLocalTimeZone())
7587
};
88+
89+
// selectDateRange = getDateRangeByValue(params[DATE_RANGE_PARAM]);
90+
// calendarDateRange = {
91+
// start: fromAbsolute(params[DATE_RANGE_START_PARAM], getLocalTimeZone()),
92+
// end: fromAbsolute(params[DATE_RANGE_END_PARAM], getLocalTimeZone())
93+
// };
7694
});
7795
});
7896

frontend/src/lib/constants/date-ranges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ export const DATE_RANGES: DateRange[] = [
6161

6262
export type DateRangeOption = (typeof DATE_RANGES)[number];
6363

64-
export function getDateRangeByValue(value: string): DateRange | undefined {
64+
export function getDateRangeByValue(value: string | null): DateRange | undefined {
6565
return DATE_RANGES.find((range) => range.value === value);
6666
}

0 commit comments

Comments
 (0)