Skip to content

Commit

Permalink
[accel-web] merge key and keys of sortUrl() args
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Jan 6, 2025
1 parent 5745339 commit 1c6651e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
18 changes: 9 additions & 9 deletions packages/accel-web/src/SortLink.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { Direction, sortUrl } from "./sort.js";
type Props = {
q: Search<any>;
key: string;
key: string | string[];
defaultOrder?: Direction;
keys?: string[];
asc?: string;
desc?: string;
} & astroHTML.JSX.LinkHTMLAttributes;
const { q, key, defaultOrder, keys, asc, desc, ...linkAttrs } = Astro.props;
const { q, key, defaultOrder, asc, desc, ...linkAttrs } = Astro.props;
const url = Astro.request.url;
const sortOrder = url.includes(`${key}+desc`) ? "desc" : url.includes(`${key}+asc`) ? "asc" : "";
const href = sortUrl(q, key, { request: Astro.request, defaultOrder, keys });
const key0 = Array.isArray(key) ? key[0] : key;
const direction = url.includes(`${key0}+desc`) ? "desc" : url.includes(`${key0}+asc`) ? "asc" : "";
const href = sortUrl(q, key, { request: Astro.request, defaultOrder });
---

<a {...linkAttrs} {href}>
<slot>
<span class="sort-link__text">{q.model.humanAttributeName(key)}</span>
<span class="sort-link__text">{q.model.humanAttributeName(key0)}</span>
</slot>
{
sortOrder != "" && (
<span class={`sort-link__icon sort-link__icon--${sortOrder}`}>
{sortOrder === "asc" ? asc ?? "" : desc ?? ""}
direction != "" && (
<span class={`sort-link__icon sort-link__icon--${direction}`}>
{direction === "asc" ? asc ?? "" : desc ?? ""}
</span>
)
}
Expand Down
9 changes: 4 additions & 5 deletions packages/accel-web/src/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ export type Direction = "asc" | "desc";
* Generates a URL for sorting based on the provided search query and sorting options.
*
* @param q - The search query object.
* @param key - The primary key to sort by.
* @param key - The attribute(s) to sort by.
* @param options - Optional sorting options.
* @param options.defaultOrder - The default sorting order if not specified (default is "asc").
* @param options.keys - An array of keys to sort by, each key can include a direction (e.g., "name desc").
* @param options.request - An optional request object containing the URL to update.
* @returns URL with the sorting parameters.
*/
export const sortUrl = (
q: Search<any>,
key: string,
options?: { defaultOrder?: Direction; keys?: string[]; request?: Request }
key: string | string[],
options?: { defaultOrder?: Direction; request?: Request }
) => {
const keys = options?.keys ?? [key];
const keys = [key].flat();
const defaultOrder = options?.defaultOrder ?? "asc";
const oldsorts = buildCurrentSorts(q);
const sorts = new Map<string, Direction>();
Expand Down
2 changes: 1 addition & 1 deletion packages/accel-web/tests/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test("sortUrl", () => {
}
{
const q = new Search(SampleModel, { s: ["name desc", "id desc"] });
const url = sortUrl(q, "name", { keys: ["name", "id asc"] });
const url = sortUrl(q, ["name", "id asc"]);
expect(decodeURI(url)).toBe("?q.s[]=name+asc&q.s[]=id+asc");
}
{
Expand Down
5 changes: 1 addition & 4 deletions tests/models/model/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ test("sortUrl()", async () => {
}
{
const q = new Search(User, { s: ["age desc", "id desc"] });
const url = sortUrl(q, "name", {
keys: ["age", "id asc"],
request,
});
const url = sortUrl(q, ["age", "id asc"], { request });
expect(decodeURI(url)).toBe("http://localhost/?q.s[]=age+asc&q.s[]=id+asc");
expect(await fetchIdsFromUrl(url)).toEqual([2, 3, 1, 4]);
}
Expand Down

0 comments on commit 1c6651e

Please sign in to comment.