diff --git a/src/App.js b/src/App.js index 79999c2..8e565f5 100644 --- a/src/App.js +++ b/src/App.js @@ -1,11 +1,11 @@ -import React, {useState} from 'react'; -import {Route, Routes} from "react-router-dom"; +import React, { useState, useEffect } from 'react'; +import { Route, Routes } from "react-router-dom"; import './index.css'; import Header from "./shared/header/Header"; import TreeView from "./tree/Tree"; -import {createTheme, ThemeProvider} from '@mui/material/styles'; -import {generateSearchQuery} from "./tree/functions/generateSearchQuery"; -import {ApiRoutes, getServerUrl} from "./server"; +import { createTheme, ThemeProvider } from '@mui/material/styles'; +import { generateSearchQuery } from "./tree/functions/generateSearchQuery"; +import { ApiRoutes, getServerUrl } from "./server"; const appTheme = createTheme({ palette: { @@ -14,11 +14,12 @@ const appTheme = createTheme({ }); function App() { - const [searchKey, setSearchKey] = useState(""); const [searchResults, setSearchResults] = useState(null); const [loadedEntity, setLoadedEntity] = useState(null); const [isLoading, setIsLoading] = useState(true); + const [accessToken, setAccessToken] = useState(null); + const app_props = { searchKey, setSearchKey, searchResults, setSearchResults, @@ -27,41 +28,103 @@ function App() { getSearchResults, getEntity }; + useEffect(() => { + getAccessToken(); + }, []); + + async function getAccessToken() { + // Token request URL, defaults to "/". Used to obtain the access token for OAuth 2.0. + const tokenEndpoint = window?.configs?.tokenEndpoint ? window.configs.tokenEndpoint : "/"; +. + // OAuth 2.0 client ID, defaults to "id". Identifies the application. + const clientId = window?.configs?.clientId ? window.configs.clientId : "id"; + + // OAuth 2.0 client secret, defaults to "secret". Authenticates the application. + const clientSecret = window?.configs?.clientSecret ? window.configs.clientSecret : "secret"; + + try { + const response = await fetch(tokenEndpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + grant_type: 'client_credentials', + client_id: clientId, + client_secret: clientSecret, + }), + }); + + if (!response.ok) { + throw new Error('Failed to get access token'); + } + + const data = await response.json(); + setAccessToken(data.access_token); + } catch (error) { + console.error('Error getting access token:', error); + } + } - function getSearchResults(searchInput) { + async function getSearchResults(searchInput) { setIsLoading(true); - if (searchInput.length > 1) { + if (searchInput.length > 1 && accessToken) { let searchUrl = generateSearchQuery(searchInput); searchUrl += '&limit=0'; - fetch(searchUrl, { - method: 'GET' - }).then(results => { - return results.json(); - }).then(data => { + try { + const response = await fetch(searchUrl, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json' + } + }); + + if (!response.ok) { + throw new Error('Search request failed'); + } + + const data = await response.json(); setSearchResults(data); - }).then( - setIsLoading(false) - ); + } catch (error) { + console.error('Error in getSearchResults:', error); + } finally { + setIsLoading(false); + } + } else { + setIsLoading(false); } - } - function getEntity(title, callback) { + async function getEntity(title, callback) { setIsLoading(true); - fetch(getServerUrl(ApiRoutes.entity) + title, { - method: 'GET' - }).then(results => { - if (results.status === 200) { - return results.json(); + if (accessToken) { + try { + const response = await fetch(getServerUrl(ApiRoutes.entity) + title, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json' + } + }); + + if (response.status === 200) { + const data = await response.json(); + setLoadedEntity(data); + callback(); + } else { + setLoadedEntity(null); + } + } catch (error) { + console.error('Error in getEntity:', error); + setLoadedEntity(null); + } finally { + setIsLoading(false); } - return null - }).then(data => { - setLoadedEntity(data); - callback(); - }).then( - setIsLoading(false) - ) + } else { + setIsLoading(false); + } } return (