-
Notifications
You must be signed in to change notification settings - Fork 0
Use sveltekit-search-params
to simplify query string manipulation
#168
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ad23b1
Use `sveltekit-search-params` to simplify query string manipulation
techniq 53270f8
Progress on converting DateRangeSeelector to use sveltekit-search-params
techniq 59a940e
Update DateRangeSelector to use sveltekit-search-params to manage URL…
techniq b7f4756
Update all query param configs utils to manage keys as well. Set dat…
techniq 1d277d3
Add `getSearchParamValues()` util to simplify using sveltekit-search-…
techniq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 9 additions & 13 deletions
22
frontend/src/lib/components/MetricTypeToggle/MetricTypeToggle.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import { ssp } from 'sveltekit-search-params'; | ||
|
||
import { | ||
DATE_RANGE_PARAM, | ||
DATE_RANGE_START_PARAM, | ||
DATE_RANGE_END_PARAM, | ||
PAST_1_WEEK, | ||
getDateRangeByValue | ||
} from '$lib/constants/date-ranges'; | ||
import { getSearchParamValues } from './search-params'; | ||
|
||
export function getDateRange(range: string): [number, number] { | ||
const dateRange = getDateRangeByValue(range); | ||
return dateRange ? dateRange.getRange() : getDateRangeByValue(PAST_1_WEEK)!.getRange(); | ||
} | ||
|
||
export function parseDateRangeParams(searchParams: URLSearchParams) { | ||
const dateRangeValue = searchParams.get(DATE_RANGE_PARAM); | ||
const startParam = searchParams.get(DATE_RANGE_START_PARAM); | ||
const endParam = searchParams.get(DATE_RANGE_END_PARAM); | ||
export function getDateRangeParamsConfig() { | ||
return { | ||
[DATE_RANGE_PARAM]: ssp.string(PAST_1_WEEK), | ||
[DATE_RANGE_START_PARAM]: ssp.number(), | ||
[DATE_RANGE_END_PARAM]: ssp.number() | ||
}; | ||
} | ||
|
||
let startTimestamp: number; | ||
let endTimestamp: number; | ||
export function parseDateRangeParams(searchParams: URLSearchParams) { | ||
const paramsConfig = getDateRangeParamsConfig(); | ||
const paramValues = getSearchParamValues(searchParams, paramsConfig); | ||
|
||
if (startParam && endParam) { | ||
startTimestamp = Number(startParam); | ||
endTimestamp = Number(endParam); | ||
} else { | ||
[startTimestamp, endTimestamp] = getDateRange(dateRangeValue || PAST_1_WEEK); | ||
if (paramValues[DATE_RANGE_START_PARAM] == null || paramValues[DATE_RANGE_END_PARAM] == null) { | ||
const [start, end] = getDateRange(paramValues[DATE_RANGE_PARAM] || PAST_1_WEEK); | ||
paramValues[DATE_RANGE_START_PARAM] = start; | ||
paramValues[DATE_RANGE_END_PARAM] = end; | ||
} | ||
|
||
return { | ||
dateRangeValue: dateRangeValue || PAST_1_WEEK, | ||
startTimestamp, | ||
endTimestamp | ||
dateRangeValue: paramValues[DATE_RANGE_PARAM], | ||
startTimestamp: paramValues[DATE_RANGE_START_PARAM], | ||
endTimestamp: paramValues[DATE_RANGE_END_PARAM] | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { EncodeAndDecodeOptions } from 'sveltekit-search-params/sveltekit-search-params'; | ||
|
||
/** Get URLSearchParam values from `sveltekit-search-params` config. | ||
* Mostly useful server-side (use `queryParameters()` client side) | ||
**/ | ||
export function getSearchParamValues( | ||
searchParams: URLSearchParams, | ||
paramsConfig: Record<string, EncodeAndDecodeOptions> | ||
) { | ||
const paramEntries = Object.entries(paramsConfig).map(([paramName, paramConfig]) => { | ||
const value = searchParams.get(paramName); | ||
let decodedValue = paramConfig.decode(value) ?? paramConfig.defaultValue; | ||
return [paramName, decodedValue]; | ||
}); | ||
return Object.fromEntries(paramEntries); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add timestamp validation
Ensure start timestamp is before end timestamp.
📝 Committable suggestion