Skip to content

Commit

Permalink
fix: tower filter better
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Patek committed Feb 10, 2025
1 parent 10ac2ea commit c001170
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
18 changes: 7 additions & 11 deletions app/(with-navbar)/(with-footer)/rozhledny/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@ import Filter from "@/components/towers/Filter";
import Pagination from "@/components/towers/Pagination";
import Results from "@/components/towers/Results";
import ResultsSkeleton from "@/components/towers/ResultsSkeleton";
import { TowersSearchParams } from "@/types/TowersSearchParams";
import { TowersFilter } from "@/utils/TowersFilter";
import { Suspense } from "react";

const PER_PAGE = 20;

async function TowersPage(props: {
searchParams?: Promise<{ query?: string; page?: number; county?: string; province?: string; location?: string; sort?: string }>;
}) {
async function TowersPage(props: { searchParams?: Promise<TowersSearchParams> }) {
const searchParams = await props.searchParams;
const query = searchParams?.query || "";
const page = searchParams?.page || 1;
const county = searchParams?.county || "";
const province = searchParams?.province || "";
const sort = searchParams?.sort || "";
let location = null;

let location = null;
if (searchParams?.location) {
const [latitude, longitude] = searchParams.location.split(",");
location = { lat: parseFloat(latitude), lng: parseFloat(longitude) };
}

let filter_by = "";
if (province) filter_by += `province:=${province}`;
if (county) filter_by += filter_by ? ` && county:=${county}` : `county:=${county}`;
const filter = new TowersFilter(searchParams);

let sort_by = "name:asc";
if (sort === "distance" && location) sort_by = `gps(${location.lat}, ${location.lng}):asc`;
Expand All @@ -34,7 +30,7 @@ async function TowersPage(props: {
q: query,
limit: PER_PAGE,
offset: (page - 1) * PER_PAGE,
filter_by,
filter_by: filter.getFilterString(),
sort_by,
});

Expand All @@ -47,7 +43,7 @@ async function TowersPage(props: {
</article>
<Filter />
<Pagination totalPages={totalPages} />
<Suspense key={query + province + county} fallback={<ResultsSkeleton />}>
<Suspense key={query} fallback={<ResultsSkeleton />}>
<Results towers={towers} />
</Suspense>
{totalPages > 1 ? <Pagination totalPages={totalPages} /> : null}
Expand Down
9 changes: 9 additions & 0 deletions types/TowersSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type TowersSearchParams = {
query?: string;
page?: number;
county?: string;
province?: string;
location?: string;
sort?: string;
showFilter?: string;
};
34 changes: 34 additions & 0 deletions utils/TowersFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { OpeningHoursForbiddenType } from "@/types/OpeningHours";
import { TowersSearchParams } from "@/types/TowersSearchParams";

export class TowersFilter {
private filters: string[] = [];

constructor(searchParams: TowersSearchParams) {
const province = searchParams?.province || "";
const county = searchParams?.county || "";
const showFilter = searchParams?.showFilter || "";

if (province) this.addFilter(`province:=${province}`);
if (county) this.addFilter(`county:=${county}`);

switch (showFilter) {
case "showOnlyGone":
this.addFilter(`openingHours.forbiddenType:=${OpeningHoursForbiddenType.Gone}`);
break;
case "showAll":
break;
default:
this.addFilter(`openingHours.forbiddenType:!=${OpeningHoursForbiddenType.Gone}`);
break;
}
}

public addFilter(filter: string): void {
this.filters.push(filter);
}

public getFilterString(): string {
return this.filters.join(" && ");
}
}

0 comments on commit c001170

Please sign in to comment.