-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78df8de
commit b385d32
Showing
7 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,30 @@ | ||
import Select from "./Select"; | ||
|
||
type SortSelectProps = { | ||
value?: string; | ||
}; | ||
|
||
export default function SortSelect({ value }: SortSelectProps) { | ||
return ( | ||
<Select | ||
value={value} | ||
options={{ | ||
relevance: "Relevance", | ||
newest: "Newest first", | ||
oldest: "Oldest first", | ||
}} | ||
onChange={(event) => { | ||
const searchParams = new URLSearchParams(window.location.search); | ||
const value = event.currentTarget.value; | ||
|
||
if (value) { | ||
searchParams.set("sort", value); | ||
} else { | ||
searchParams.delete("sort"); | ||
} | ||
|
||
window.location.search = searchParams.toString(); | ||
}} | ||
/> | ||
); | ||
} |
This file contains 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,18 @@ | ||
import { strict as assert } from "node:assert"; | ||
import { describe, it } from "node:test"; | ||
import { oneOf } from "./validation"; | ||
|
||
describe("oneOf", () => { | ||
it("returns value if it is allowed", () => { | ||
assert.strictEqual(oneOf("foo", ["foo", "bar"]), "foo"); | ||
assert.strictEqual(oneOf("foo", ["foo", "bar"], "bar"), "foo"); | ||
}); | ||
|
||
it("returns fallback if value is not allowed", () => { | ||
assert.strictEqual(oneOf("foo", ["bar", "baz"], "bar"), "bar"); | ||
}); | ||
|
||
it("returns null if value is not allowed and no fallback is defined", () => { | ||
assert.strictEqual(oneOf("foo", ["bar", "baz"]), null); | ||
}); | ||
}); |
This file contains 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,21 @@ | ||
export function oneOf<T extends readonly string[], F extends string>( | ||
value: string, | ||
allowed: T, | ||
fallback: F, | ||
): T[number] | F; | ||
export function oneOf<T extends readonly string[]>( | ||
value: string, | ||
allowed: T, | ||
fallback: never, | ||
): T[number] | null; | ||
export function oneOf<T extends readonly string[]>( | ||
value: string, | ||
allowed: T, | ||
fallback?: string, | ||
) { | ||
if (!allowed.includes(value)) { | ||
return fallback || null; | ||
} | ||
|
||
return value; | ||
} |
This file contains 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,12 @@ | ||
.search-page__info { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
font-size: var(--text-sm); | ||
color: var(--color-text-light); | ||
} | ||
|
||
.search-page__sort { | ||
display: flex; | ||
align-items: center; | ||
} |
This file contains 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