Skip to content

Commit 6893921

Browse files
authored
Fix issues from backend tests (#195)
* Improve error handling for search results * Set user after OIDC callback * Set CSRF header on POST requests * Bump version
1 parent 33b4806 commit 6893921

File tree

7 files changed

+19
-5
lines changed

7 files changed

+19
-5
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "data-portal-ui",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"private": true,
55
"dependencies": {
66
"@fortawesome/fontawesome-svg-core": "~6.4.2",

src/api/browse.ts

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export const querySearchService: getDatasetsSearchRespType = async (
5454
try {
5555
const response = await fetchJson(url, "POST", payload);
5656
const data = await response.json();
57+
if (response.status !== 200) {
58+
throw new Error(`Status code ${response.status} in search response`);
59+
}
5760
callbackFunc(data);
5861
} catch (error) {
5962
showFetchDataError();

src/components/browse/browse.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const Browse = () => {
6363
var dsCount: number = 0;
6464

6565
if (searchResults !== null) {
66-
if (searchResults.hits.length > 0 || searchResults.count === -1) {
66+
if ((searchResults.hits && searchResults.hits.length > 0) || searchResults.count === -1) {
6767
dsList = searchResults.hits;
6868
facetList = searchResults.facets;
6969
dsCount = searchResults.count;

src/components/browse/dataset/datasetHeader.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const DatasetHeader = (props: DatasetHeaderProps) => {
3838

3939
var listOfAllFacets: FacetModel[] | null = null;
4040
if (searchResults !== null) {
41-
if (searchResults.hits.length > 0 || searchResults.count === -1) {
41+
if ((searchResults.hits && searchResults.hits.length > 0) || searchResults.count === -1) {
4242
listOfAllFacets = searchResults.facets;
4343
} else {
4444
listOfAllFacets = [];

src/services/auth.ts

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class AuthService {
174174
if (!user) {
175175
showMessage({ type: "error", title: "Login failed" });
176176
}
177+
this.setUser(user);
177178
return user;
178179
}
179180

src/utils/utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { authService } from "../services/auth";
2+
13
/**
24
* Checks and cleans up URL if last character is or is not forward slash
35
* Duplicate of utils version because of infinite recursion error, needs refactoring
@@ -99,6 +101,14 @@ export const fetchJson = async (
99101
if (CLIENT_URL) {
100102
headers["Origin"] = CLIENT_URL.hostname;
101103
}
104+
if (method.match(/^POST|PUT|PATCH|DELETE$/) &&
105+
!additionalHeaders?.hasOwnProperty("X-Authorization")) {
106+
const user = await authService.getUser();
107+
const csrf = user?.csrf;
108+
if (csrf) {
109+
headers["X-CSRF-Token"] = csrf;
110+
}
111+
}
102112
if (additionalHeaders) Object.assign(headers, additionalHeaders);
103113
const body = payload ? JSON.stringify(payload) : undefined;
104114
try {

0 commit comments

Comments
 (0)