Skip to content

Commit 4f25d1e

Browse files
authored
Merge pull request #89 from kartverket/feature/Fix-filter-error
Fikset bug at filtere med mellomrom tilbakestilte view
2 parents 73bae58 + acdc37b commit 4f25d1e

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/components/routes/Home.jsx

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Dependencies
2-
import React, { useEffect, useRef} from "react";
2+
import React, { useEffect, useRef } from "react";
33
import { Helmet } from "react-helmet-async";
44
import { useDispatch, useSelector } from "react-redux";
5-
import { Link, useRouteLoaderData, useSearchParams } from "react-router-dom";
5+
import { Link, useLocation, useNavigate, useRouteLoaderData, useSearchParams } from "react-router-dom";
66
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
77
import { BulletListIcon, MenuGridIcon } from "@navikt/aksel-icons";
88
import { usePostHog } from "@posthog/react";
@@ -24,21 +24,33 @@ import { ErrorBoundary } from "@/components/ErrorBoundary";
2424

2525
// Stylesheets
2626
import style from "./Home.module.scss";
27-
import {Alert} from "@digdir/designsystemet-react";
27+
import { Alert } from "@digdir/designsystemet-react";
2828

2929
const Home = () => {
3030
const dispatch = useDispatch();
3131
const { searchData, params } = useRouteLoaderData("root");
3232
const posthog = usePostHog();
3333
const lastCapturedSearchString = useRef(null);
34-
const [searchParams, setSearchParams] = useSearchParams();
34+
const [searchParams] = useSearchParams();
35+
const location = useLocation();
36+
const navigate = useNavigate();
3537

36-
//ViewMode - Grid/List
38+
// ViewMode - Grid/List
3739
const viewMode = searchParams.get("view") === "list" ? "list" : "grid";
40+
3841
const setMode = (mode) => {
39-
const next = new URLSearchParams(searchParams);
40-
next.set("view", mode);
41-
setSearchParams(next);
42+
const currentSearch = location.search || "";
43+
let nextSearch;
44+
45+
if (!currentSearch) {
46+
nextSearch = `?view=${mode}`;
47+
} else if (/[?&]view=/.test(currentSearch)) {
48+
nextSearch = currentSearch.replace(/([?&]view=)[^&]*/, `$1${mode}`);
49+
} else {
50+
nextSearch = `${currentSearch}&view=${mode}`;
51+
}
52+
53+
navigate(`${location.pathname}${nextSearch}`);
4254
};
4355

4456
const ViewModeToggle = () => (
@@ -48,15 +60,15 @@ const Home = () => {
4860
className={viewMode === "list" ? style.active : ""}
4961
onClick={() => setMode("list")}
5062
>
51-
<BulletListIcon title="Listevisning"/>
63+
<BulletListIcon title="Listevisning" />
5264
</button>
5365

5466
<button
5567
type="button"
5668
className={viewMode === "grid" ? style.active : ""}
5769
onClick={() => setMode("grid")}
5870
>
59-
<MenuGridIcon title="Gridvisning"/>
71+
<MenuGridIcon title="Gridvisning" />
6072
</button>
6173
</div>
6274
);
@@ -85,9 +97,9 @@ const Home = () => {
8597
}
8698
}, [searchData, params, posthog]);
8799

100+
// Todo fix problem navigation from MainNavigationContainer https://medium.com/@fabrizio.azzarri/fixing-the-next-js-15-react-19-removechild-dom-error-a33b57cbc3b1
88101
useEffect(() => {
89102
const isLoggedIn = !!auth?.user?.access_token?.length;
90-
// Todo fix problem navigation from MainNavigationContainer https://medium.com/@fabrizio.azzarri/fixing-the-next-js-15-react-19-removechild-dom-error-a33b57cbc3b1
91103
if (isLoggedIn) {
92104
GnShortcutButton.setup("gn-shortcut-button", {
93105
getAuthToken: () => {
@@ -98,14 +110,14 @@ const Home = () => {
98110
}
99111
}, [auth]);
100112

101-
102113
const clearSearchPath = viewMode === "list" ? "/?view=list" : "/";
103114

104115
const renderSearchQuery = () => {
105116
let searchResultsText = "";
106117
const hasSearchResults = searchData?.results && Object.keys(searchData.results)?.length;
107118
const numFound = searchData?.results?.metadata?.NumFound || 0;
108119
if (!hasSearchResults) return <h1>Kartkatalogen</h1>;
120+
109121
if (params.searchResultsType === "metadata") {
110122
if (searchData?.searchString?.length && searchData?.results?.metadata) {
111123
const resourceVariables = [
@@ -122,6 +134,7 @@ const Home = () => {
122134
getResource("SearchResultsCountText", "Søk på {0} ga {1} treff i {2}", resourceVariables)
123135
);
124136
}
137+
125138
return (
126139
<div className={style.activeContent}>
127140
<div className={style.searchResultContainer}>
@@ -159,6 +172,7 @@ const Home = () => {
159172
getResource("SearchResultsCountText", "Søk på {0} ga {1} treff i {2}", resourceVariables)
160173
);
161174
}
175+
162176
return (
163177
<div className={style.searchResultContainer}>
164178
<span className={searchData?.searchString !== "" ? style.searchResultInformation : ""}>
@@ -193,6 +207,7 @@ const Home = () => {
193207
url: "/"
194208
}
195209
];
210+
196211
return (
197212
<div>
198213
<Helmet>
@@ -209,15 +224,20 @@ const Home = () => {
209224
/>
210225
<meta name="keywords" content="kartverket, geonorge, kartkatalog, kartkatalogen" />
211226
</Helmet>
227+
212228
<breadcrumb-list id="breadcrumb-list" breadcrumbs={JSON.stringify(breadcrumbs)}></breadcrumb-list>
229+
213230
<div id="main-content">
214231
<gn-shortcut-button language={selectedLanguage} environment={environment?.environment}></gn-shortcut-button>
232+
215233
<div className={style.alertWrapper}>
216-
<Alert className={style.alertBanner} data-color='info'>
234+
<Alert className={style.alertBanner} data-color="info">
217235
Vi prøver ut endringer i visningen av Kartkatalogen! Det vil komme mindre justeringer fortløpende.
218-
Innholdet er det samme. Gi oss gjerne tilbakemelding <a target="_blank" href="https://response.questback.com/kartverket/yupv1rvfve">her</a>.
236+
Innholdet er det samme. Gi oss gjerne tilbakemelding{" "}
237+
<a target="_blank" href="https://response.questback.com/kartverket/yupv1rvfve">her</a>.
219238
</Alert>
220239
</div>
240+
221241
<header className={style.header}>
222242
<div className={style.headerContent}>
223243
<div className={style.headerLeft}>
@@ -229,14 +249,20 @@ const Home = () => {
229249
</heading-text>
230250
)}
231251
<ErrorBoundary>
232-
<SelectedFacets searchData={searchData} searchResultsType={params.searchResultsType} viewMode={viewMode} />
252+
<SelectedFacets
253+
searchData={searchData}
254+
searchResultsType={params.searchResultsType}
255+
viewMode={viewMode}
256+
/>
233257
</ErrorBoundary>
234258
</div>
259+
235260
<div className={style.headerActions}>
236261
<ViewModeToggle />
237262
</div>
238263
</div>
239264
</header>
265+
240266
<ErrorBoundary>
241267
<SearchResults searchData={searchData} searchResultsType={params.searchResultsType} viewMode={viewMode} />
242268
</ErrorBoundary>
@@ -245,4 +271,4 @@ const Home = () => {
245271
);
246272
};
247273

248-
export default Home;
274+
export default Home;

0 commit comments

Comments
 (0)