From 379ef603138871a69da9e6ce58920b882cc6ae66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Fri, 16 Sep 2022 10:55:16 +0200 Subject: [PATCH 1/3] extract hardcoded contextPath to a constant --- src/App.js | 19 ++++++++++--------- src/components/ParsedLink.js | 7 ++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/App.js b/src/App.js index b2a4b71..378e099 100644 --- a/src/App.js +++ b/src/App.js @@ -56,6 +56,7 @@ function jsonToArray(json) { } function App() { + const appContextPath = "/free-programming-books-search/"; // keeps the state of the json const [data, setData] = useState(undefined); // put all books into one array. uses more memory, but search is faster and less complex @@ -89,13 +90,13 @@ function App() { async function fetchData() { try { setQueries(queryString.parse(document.location.search)); - if (queries.lang) { - if (queries.lang === "langs" || queries.lang === "subjects") { - changeParameter("lang.code", "en"); - } else { - changeParameter("lang.code", queries.lang); - } - } + if (queries.lang) { + if (queries.lang === "langs" || queries.lang === "subjects") { + changeParameter("lang.code", "en"); + } else { + changeParameter("lang.code", queries.lang); + } + } // setLoading(true); let result = await axios.get( "https://raw.githubusercontent.com/EbookFoundation/free-programming-books-search/main/fpb.json" @@ -201,7 +202,7 @@ function App() { // lang: entry.item.lang, // section: entry.item.section, // title: `List of all ${section} books in ${entry.item.lang.name}`, - // url: `/free-programming-books-search?sect=books&lang=${langCode}&file=free-programming-books-${langCode}#${section}`, + // url: `${appContextPath}?sect=books&lang=${langCode}&file=free-programming-books-${langCode}#${section}`, // samePage: true, // }, // }; @@ -253,7 +254,7 @@ function App() {

- free-programming-books + free-programming-books

diff --git a/src/components/ParsedLink.js b/src/components/ParsedLink.js index e0eb2f5..c608e6e 100644 --- a/src/components/ParsedLink.js +++ b/src/components/ParsedLink.js @@ -1,6 +1,7 @@ import React, { useState, useEffect } from "react"; function ParsedLink({ children, sect, href, id }) { + const appContextPath = "/free-programming-books-search/"; const [folder, setFolder] = useState(null); const [file, setFile] = useState(null); @@ -29,11 +30,11 @@ function ParsedLink({ children, sect, href, id }) { }, [href]); if (folder && file) { - return {children}; + return {children}; } else if (file) { - return {children}; + return {children}; } else { // Go to the homepage when there's a bad relative URL - return {children} + return {children} } } From 25ae1e328eb43712f51f0e67b797afa7c2aa8872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Fri, 16 Sep 2022 11:16:39 +0200 Subject: [PATCH 2/3] use `process.env.PUBLIC_URL` as appContextPath --- src/App.js | 2 +- src/components/ParsedLink.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 378e099..20b2781 100644 --- a/src/App.js +++ b/src/App.js @@ -56,7 +56,7 @@ function jsonToArray(json) { } function App() { - const appContextPath = "/free-programming-books-search/"; + const appContextPath = process.env.PUBLIC_URL + "/"; // keeps the state of the json const [data, setData] = useState(undefined); // put all books into one array. uses more memory, but search is faster and less complex diff --git a/src/components/ParsedLink.js b/src/components/ParsedLink.js index c608e6e..2410c2d 100644 --- a/src/components/ParsedLink.js +++ b/src/components/ParsedLink.js @@ -1,7 +1,7 @@ import React, { useState, useEffect } from "react"; function ParsedLink({ children, sect, href, id }) { - const appContextPath = "/free-programming-books-search/"; + const appContextPath = process.env.PUBLIC_URL + "/"; const [folder, setFolder] = useState(null); const [file, setFile] = useState(null); From df53426eb8b2655c4c214c6a457282dd510162c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Fri, 16 Sep 2022 11:17:50 +0200 Subject: [PATCH 3/3] fix: remove extra ampersand --- src/components/ParsedLink.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ParsedLink.js b/src/components/ParsedLink.js index 2410c2d..bf8a523 100644 --- a/src/components/ParsedLink.js +++ b/src/components/ParsedLink.js @@ -30,7 +30,7 @@ function ParsedLink({ children, sect, href, id }) { }, [href]); if (folder && file) { - return {children}; + return {children}; } else if (file) { return {children}; } else { // Go to the homepage when there's a bad relative URL