Skip to content

Commit

Permalink
feat: Keep third level of browse filters in URL
Browse files Browse the repository at this point in the history
bprusinowski committed Sep 24, 2024
1 parent b90330f commit b671e89
Showing 6 changed files with 41 additions and 7 deletions.
28 changes: 24 additions & 4 deletions app/browser/context.tsx
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ export const getBrowseParamsFromQuery = (
"iri",
"subtype",
"subiri",
"subsubtype",
"subsubiri",
"topic",
"includeDrafts",
"order",
@@ -44,8 +46,17 @@ export const getBrowseParamsFromQuery = (
(v) => (Array.isArray(v) ? v[0] : v)
);

const { type, iri, subtype, subiri, topic, includeDrafts, ...values } =
rawValues;
const {
type,
iri,
subtype,
subiri,
subsubtype,
subsubiri,
topic,
includeDrafts,
...values
} = rawValues;
const previous = values.previous ? JSON.parse(values.previous) : undefined;

return pickBy(
@@ -55,6 +66,8 @@ export const getBrowseParamsFromQuery = (
iri: iri ?? previous?.iri,
subtype: subtype ?? previous?.subtype,
subiri: subiri ?? previous?.subiri,
subsubtype: subsubtype ?? previous?.subsubtype,
subsubiri: subsubiri ?? previous?.subsubiri,
topic: topic ?? previous?.topic,
includeDrafts: includeDrafts ? JSON.parse(includeDrafts) : undefined,
},
@@ -63,7 +76,8 @@ export const getBrowseParamsFromQuery = (
};

export const buildURLFromBrowseState = (browseState: BrowseParams) => {
const { type, iri, subtype, subiri, ...queryParams } = browseState;
const { type, iri, subtype, subiri, subsubtype, subsubiri, ...queryParams } =
browseState;

const typePart =
type && iri
@@ -73,8 +87,14 @@ export const buildURLFromBrowseState = (browseState: BrowseParams) => {
subtype && subiri
? `${encodeURIComponent(subtype)}/${encodeURIComponent(subiri)}`
: undefined;
const subsubtypePart =
subsubtype && subsubiri
? `${encodeURIComponent(subsubtype)}/${encodeURIComponent(subsubiri)}`
: undefined;

const pathname = ["/browse", typePart, subtypePart].filter(Boolean).join("/");
const pathname = ["/browse", typePart, subtypePart, subsubtypePart]
.filter(Boolean)
.join("/");
return {
pathname,
query: queryParams,
9 changes: 6 additions & 3 deletions app/browser/filters.tsx
Original file line number Diff line number Diff line change
@@ -21,10 +21,11 @@ export type BrowseFilter =

export const getFiltersFromParams = (params: BrowseParams) => {
const filters: BrowseFilter[] = [];
const { type, subtype, iri, subiri, topic } = params;
const { type, subtype, subsubtype, iri, subiri, subsubiri, topic } = params;
for (const [t, i] of [
[type, iri],
[subtype, subiri],
[subsubtype, subsubiri],
]) {
if (t && i && (t === "theme" || t === "organization" || t === "termset")) {
const __typename = (() => {
@@ -58,14 +59,16 @@ export const getParamsFromFilters = (filters: BrowseFilter[]) => {
const params: BrowseParams = {
type: undefined,
subtype: undefined,
subsubtype: undefined,
iri: undefined,
subiri: undefined,
subsubiri: undefined,
topic: undefined,
};
let i = 0;
for (const filter of filters) {
const typeAttr = i === 0 ? "type" : ("subtype" as const);
const iriAttr = i === 0 ? "iri" : ("subiri" as const);
const typeAttr = i === 0 ? "type" : i === 1 ? "subtype" : "subsubtype";
const iriAttr = i === 0 ? "iri" : i === 1 ? "subiri" : "subsubiri";
switch (filter.__typename) {
case "DataCubeTheme":
params[typeAttr] = "theme";
3 changes: 3 additions & 0 deletions app/pages/browse/[type]/[iri]/[subtype].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DatasetBrowser } from "@/pages/browse";

export default DatasetBrowser;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DatasetBrowser } from "@/pages/browse";

export default DatasetBrowser;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DatasetBrowser } from "@/pages/browse";

export default DatasetBrowser;
2 changes: 2 additions & 0 deletions app/pages/browse/index.tsx
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@ import { SearchCubeResultOrder } from "@/graphql/query-hooks";
export type BrowseParams = {
type?: "theme" | "organization" | "dataset" | "termset";
subtype?: "theme" | "organization" | "termset";
subsubtype?: "theme" | "organization" | "termset";
iri?: string;
subiri?: string;
subsubiri?: string;
topic?: string;
search?: string;
order?: SearchCubeResultOrder;

0 comments on commit b671e89

Please sign in to comment.