Skip to content

Commit

Permalink
Merge pull request #166 from ConductionNL/feature/XW-106/JSON-config
Browse files Browse the repository at this point in the history
Feature/xw 106/json config
  • Loading branch information
lencodes authored Nov 14, 2023
2 parents a78a80d + ea27f49 commit 0729655
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 34 deletions.
6 changes: 5 additions & 1 deletion pwa/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ require("dotenv").config({
});

module.exports = {
/**
* We do NOT want to set the pathPrefix when we're using a DNS; it's only needed on gh-pages
* We CAN NOT set the pathPrefix when we're using the JSON-config files (due to needing access to window)
*/
pathPrefix:
process.env.USE_GITHUB_REPOSITORY_NAME_AS_PATH_PREFIX === "true"
? `/${process.env.GITHUB_REPOSITORY_NAME}`
: "", // we do NOT want to set the prefix if we're using an DNS
: "",
plugins: [
{
resolve: `gatsby-plugin-layout`,
Expand Down
12 changes: 4 additions & 8 deletions pwa/src/apiService/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type TSendFunction = (
export default class APIService {
public get BaseClient(): AxiosInstance {
return axios.create({
baseURL: process.env.GATSBY_API_BASE_URL,
baseURL: window.sessionStorage.getItem("API_BASE_URL") ?? "",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Expand All @@ -35,7 +35,7 @@ export default class APIService {

public get FilterCountClient(): AxiosInstance {
return axios.create({
baseURL: process.env.GATSBY_API_BASE_URL,
baseURL: window.sessionStorage.getItem("API_BASE_URL") ?? "",
headers: {
Accept: "application/json+aggregations",
"Content-Type": "application/json",
Expand All @@ -45,17 +45,13 @@ export default class APIService {

public get FooterContentClient(): AxiosInstance {
return axios.create({
baseURL: removeFileNameFromUrl(
process.env.GATSBY_FOOTER_CONTENT !== undefined && process.env.GATSBY_FOOTER_CONTENT.length !== 0
? process.env.GATSBY_FOOTER_CONTENT
: DEFAULT_FOOTER_CONTENT_URL,
),
baseURL: removeFileNameFromUrl(window.sessionStorage.getItem("FOOTER_CONTENT") ?? DEFAULT_FOOTER_CONTENT_URL),
});
}

public get MarkdownClient(): AxiosInstance {
return axios.create({
baseURL: process.env.GATSBY_BASE_URL ?? undefined,
baseURL: window.sessionStorage.getItem("BASE_URL") ?? undefined,
headers: {
Accept: "application/vnd.github.html",
},
Expand Down
4 changes: 2 additions & 2 deletions pwa/src/apiService/resources/filterCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default class FilterCount {
public getCategoryCount = async (): Promise<any> => {
let endpoint = "/openWOO?_queries[]=Categorie";

if (process.env.GATSBY_OIDN_NUMBER) {
endpoint += `&oidn=${process.env.GATSBY_OIDN_NUMBER}`;
if (window.sessionStorage.getItem("OIDN_NUMBER")) {
endpoint += `&oidn=${window.sessionStorage.getItem("OIDN_NUMBER")}`;
}

const { data } = await this._send(this._instance, "GET", endpoint);
Expand Down
4 changes: 2 additions & 2 deletions pwa/src/apiService/resources/openWoo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default class OpenWoo {
filters,
)}&_order[Publicatiedatum]=desc&_limit=${limit}&_page=${currentPage}`;

if (process.env.GATSBY_OIDN_NUMBER) {
endpoint += `&oidn=${process.env.GATSBY_OIDN_NUMBER}`;
if (window.sessionStorage.getItem("OIDN_NUMBER")) {
endpoint += `&oidn=${window.sessionStorage.getItem("OIDN_NUMBER")}`;
}

const { data } = await this._send(this._instance, "GET", endpoint);
Expand Down
6 changes: 1 addition & 5 deletions pwa/src/hooks/footerContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { DEFAULT_FOOTER_CONTENT_URL } from "../templates/templateParts/footer/Fo
export const useFooterContent = () => {
const API: APIService | null = React.useContext(APIContext);

const fileName = getFileNameFromUrl(
process.env.GATSBY_FOOTER_CONTENT !== undefined && process.env.GATSBY_FOOTER_CONTENT.length !== 0
? process.env.GATSBY_FOOTER_CONTENT
: DEFAULT_FOOTER_CONTENT_URL,
);
const fileName = getFileNameFromUrl(window.sessionStorage.getItem("FOOTER_CONTENT") ?? DEFAULT_FOOTER_CONTENT_URL);

const getContent = () =>
useQuery<any, Error>(["contents", fileName], () => API?.FooterContent.getContent(fileName), {
Expand Down
2 changes: 1 addition & 1 deletion pwa/src/hooks/htmlParser/anchor/getAnchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const handleInternalLinks = (props: any, targetFile: string, location: string, d
if (!directoryFound) {
const hrefWithLeadingSlash = !props.href.startsWith("/") ? `/${props.href}` : props.href;

open(`${process.env.GATSBY_GITHUB_REPOSITORY_URL}/blob/master${hrefWithLeadingSlash}`);
open(`${window.sessionStorage.getItem("GITHUB_REPOSITORY_URL") ?? ""}/blob/master${hrefWithLeadingSlash}`);
}

return; // ensure no other flow is triggered
Expand Down
2 changes: 1 addition & 1 deletion pwa/src/hooks/htmlParser/image/getImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getImage = (props: any) => {
let src = props.src;

if (!props.src.includes("https://" || "http://")) {
const sessionUrl = process.env.GATSBY_GITHUB_REPOSITORY_URL;
const sessionUrl = window.sessionStorage.getItem("GITHUB_REPOSITORY_URL");
const url = sessionUrl?.replace("https://github.com/", "");

src = `https://raw.githubusercontent.com/${url}/master/docs/features/${props.src}`;
Expand Down
2 changes: 1 addition & 1 deletion pwa/src/hooks/useMarkdownDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useMarkdownDirectories = () => {
const [directories, setDirectories] = React.useState<TMarkdownDirectory[]>([]);

React.useEffect(() => {
const markdownDirectoryPathsString: string | undefined = process.env.GATSBY_GITHUB_DOCS_DIRECTORY_PATHS;
const markdownDirectoryPathsString: string | null = window.sessionStorage.getItem("GITHUB_DOCS_DIRECTORY_PATHS");

if (!markdownDirectoryPathsString) return;

Expand Down
6 changes: 3 additions & 3 deletions pwa/src/layout/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export const Head: React.FC = () => {
lang: currentLanguage,
}}
bodyAttributes={{
class: process.env.GATSBY_NL_DESIGN_THEME_CLASSNAME,
class: window.sessionStorage.getItem("NL_DESIGN_THEME_CLASSNAME"),
}}
>
<title>{`Woo | ${process.env.GATSBY_ORGANISATION_NAME} | ${
<title>{`Woo | ${window.sessionStorage.getItem("ORGANISATION_NAME")} | ${
getPageTitle(translatedCrumbs, gatsbyContext.location) ?? "Error"
}`}</title>
<link rel="icon" type="svg" href={process.env.GATSBY_FAVICON_URL} />
<link rel="icon" type="svg" href={window.sessionStorage.getItem("FAVICON_URL") ?? ""} />
</Helmet>
);
};
5 changes: 5 additions & 0 deletions pwa/src/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { fas } from "@fortawesome/free-solid-svg-icons";
import { fab } from "@fortawesome/free-brands-svg-icons";
import { far } from "@fortawesome/free-regular-svg-icons";
import { IconPack, library } from "@fortawesome/fontawesome-svg-core";
import { initiateEnvironment } from "../services/initiateEnvironment";

interface LayoutProps {
children: React.ReactNode;
Expand All @@ -25,6 +26,10 @@ const Layout: React.FC<LayoutProps> = ({ children, pageContext, location }) => {

library.add(fas, fab as IconPack, far as IconPack);

React.useEffect(() => {
initiateEnvironment();
}, []);

React.useEffect(() => {
setAPI(new APIService());
}, [pageContext]);
Expand Down
11 changes: 11 additions & 0 deletions pwa/src/services/getConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Conduction from "./../../static/configFiles/conduction.json";
import Epe from "./../../static/configFiles/epe.json";

export const getConfig = (domain: string): Record<string, any> | undefined => {
switch (domain) {
case "open.epe.nl":
return Epe as Record<string, any>;
default:
return Conduction as Record<string, any>;
}
};
33 changes: 33 additions & 0 deletions pwa/src/services/initiateEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getConfig } from "./getConfig";

export const initiateEnvironment = () => {
const varsAvailable = process.env.GATSBY_ENV_VARS_SET === "true";

if (varsAvailable) {
window.sessionStorage.setItem("API_BASE_URL", process.env.GATSBY_API_BASE_URL ?? "");
window.sessionStorage.setItem("NL_DESIGN_THEME_CLASSNAME", process.env.GATSBY_NL_DESIGN_THEME_CLASSNAME ?? "");
window.sessionStorage.setItem("FAVICON_URL", process.env.GATSBY_FAVICON_URL ?? "");
window.sessionStorage.setItem("ORGANISATION_NAME", process.env.GATSBY_ORGANISATION_NAME ?? "");
window.sessionStorage.setItem("JUMBOTRON_IMAGE_URL", process.env.GATSBY_JUMBOTRON_IMAGE_URL ?? "");
window.sessionStorage.setItem("FOOTER_LOGO_HREF", process.env.GATSBY_FOOTER_LOGO_HREF ?? "");
window.sessionStorage.setItem("FOOTER_CONTENT", process.env.FOOTER_CONTENT ?? "");
window.sessionStorage.setItem("FOOTER_CONTENT_HEADER", process.env.GATSBY_FOOTER_CONTENT_HEADER ?? "");
window.sessionStorage.setItem("OIDN_NUMBER", process.env.GATSBY_OIDN_NUMBER ?? "");

return; // all vars are set in sessionStorage, nothing else to do
}

const config = getConfig(window.location.hostname);

if (!config) return; // no config found, nothing else to do

window.sessionStorage.setItem("API_BASE_URL", config.GATSBY_API_BASE_URL ?? "");
window.sessionStorage.setItem("NL_DESIGN_THEME_CLASSNAME", config.GATSBY_NL_DESIGN_THEME_CLASSNAME ?? "");
window.sessionStorage.setItem("FAVICON_URL", config.GATSBY_FAVICON_URL ?? "");
window.sessionStorage.setItem("ORGANISATION_NAME", config.GATSBY_ORGANISATION_NAME ?? "");
window.sessionStorage.setItem("JUMBOTRON_IMAGE_URL", config.GATSBY_JUMBOTRON_IMAGE_URL ?? "");
window.sessionStorage.setItem("FOOTER_LOGO_HREF", config.GATSBY_FOOTER_LOGO_HREF ?? "");
window.sessionStorage.setItem("FOOTER_CONTENT", config.GATSBY_FOOTER_CONTENT ?? "");
window.sessionStorage.setItem("FOOTER_CONTENT_HEADER", config.GATSBY_FOOTER_CONTENT_HEADER ?? "");
window.sessionStorage.setItem("OIDN_NUMBER", config.GATSBY_OIDN_NUMBER ?? "");
};
7 changes: 4 additions & 3 deletions pwa/src/templates/jumbotronTemplate/JumbotronTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ export const JumbotronTemplate: React.FC = () => {
<div
aria-label={t("Jumbotron")}
role="contentinfo"
style={{ backgroundImage: `url(${process.env.GATSBY_JUMBOTRON_IMAGE_URL})` }}
style={{ backgroundImage: `url("${window.sessionStorage.getItem("JUMBOTRON_IMAGE_URL")}")` }}
className={styles.wrapper}
>
<Page>
<PageContent>
<CardWrapper aria-label={t("Jumbotron card")} role="contentinfo" className={styles.card}>
<Heading1 className={styles.title}>
{t("Woo-publications of")} {process.env.GATSBY_ORGANISATION_NAME}
{t("Woo-publications of")} {window.sessionStorage.getItem("ORGANISATION_NAME")}
</Heading1>

<Paragraph className={styles.description}>
{t("On this page you will find the Woo-publications of")} {process.env.GATSBY_ORGANISATION_NAME}
{t("On this page you will find the Woo-publications of")}{" "}
{window.sessionStorage.getItem("ORGANISATION_NAME")}
</Paragraph>
</CardWrapper>
</PageContent>
Expand Down
14 changes: 8 additions & 6 deletions pwa/src/templates/templateParts/footer/FooterTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export const FooterTemplate: React.FC = () => {
// }, []);

React.useEffect(() => {
if (!process.env.GATSBY_FOOTER_CONTENT) return;
if (!window.sessionStorage.getItem("FOOTER_CONTENT")) return;

try {
setFooterContent(JSON.parse(process.env.GATSBY_FOOTER_CONTENT));
setFooterContent(JSON.parse(window.sessionStorage.getItem("FOOTER_CONTENT") ?? ""));
} catch {
console.warn("Could not parse footer content.");
}
}, [process.env.GATSBY_FOOTER_CONTENT]);
}, [window.sessionStorage.getItem("FOOTER_CONTENT")]);

return (
<PageFooter className={styles.footer}>
Expand All @@ -79,11 +79,13 @@ export const FooterTemplate: React.FC = () => {
</div>

<div className={styles.logoAndConduction}>
{process.env.GATSBY_FOOTER_LOGO_URL !== "false" && (
{window.sessionStorage.getItem("FOOTER_LOGO_URL") !== "false" && (
<Logo
variant="footer"
onClick={() =>
process.env.GATSBY_FOOTER_LOGO_HREF ? open(process.env.GATSBY_FOOTER_LOGO_HREF) : navigate("/")
window.sessionStorage.getItem("FOOTER_LOGO_HREF")
? open(window.sessionStorage.getItem("FOOTER_LOGO_HREF") ?? "")
: navigate("/")
}
/>
)}
Expand All @@ -100,7 +102,7 @@ const DynamicSection: React.FC<{ content: TDynamicContentItem }> = ({ content })

return (
<section>
<DynamicSectionHeading heading={process.env.GATSBY_FOOTER_CONTENT_HEADER} {...{ content }} />
<DynamicSectionHeading heading={window.sessionStorage.getItem("FOOTER_CONTENT_HEADER") ?? ""} {...{ content }} />

{content.items.map((item, idx) => (
<div key={idx} className={styles.dynamicSectionContent}>
Expand Down
3 changes: 3 additions & 0 deletions pwa/static/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ GATSBY_FOOTER_CONTENT_HEADER=""

#OIDN
GATSBY_OIDN_NUMBER=""

# Deployment option, if you're using this .env file DO NOT REMOVE OR EDIT THIS
GATSBY_ENV_VARS_SET="true"
11 changes: 11 additions & 0 deletions pwa/static/configFiles/conduction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"GATSBY_API_BASE_URL": "https://api.gateway.commonground.nu/api",
"GATSBY_NL_DESIGN_THEME_CLASSNAME": "conduction-theme",
"GATSBY_FAVICON_URL": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pgo8IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDIwMDEwOTA0Ly9FTiIKICAgICJodHRwOi8vd3d3LnczLm9yZy9UUi8yMDAxL1JFQy1TVkctMjAwMTA5MDQvRFREL3N2ZzEwLmR0ZCI+CjxzdmcgdmVyc2lvbj0iMS4wIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgICAgd2lk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KMjMgMCA4IC0xMTMgNzIgLTEyNiA3MiAtNSAwIC0zOSAtMTcgLTc2IC0zOHoiLz4KICAgIDwvZz4KPC9zdmc+Cg==",
"GATSBY_ORGANISATION_NAME": "Conduction",
"GATSBY_JUMBOTRON_IMAGE_URL": "https://www.conduction.nl/wp-content/uploads/2021/07/cropped-Conduction_HOME_0000_afb1-1.png",
"GATSBY_FOOTER_LOGO_HREF": "https://www.conduction.nl/",
"GATSBY_FOOTER_CONTENT": "https://raw.githubusercontent.com/ConductionNL/woo-website-template/main/pwa/src/templates/templateParts/footer/FooterContent.json",
"GATSBY_FOOTER_CONTENT_HEADER": "",
"GATSBY_OIDN_NUMBER": ""
}
11 changes: 11 additions & 0 deletions pwa/static/configFiles/epe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"GATSBY_API_BASE_URL": "https://api.gateway.commonground.nu/api",
"GATSBY_NL_DESIGN_THEME_CLASSNAME": "epe-theme",
"GATSBY_FAVICON_URL": "https://www.epe.nl/sites/default/themes/custom/toptasks_sub_theme/favicon/favicon-32x32.png",
"GATSBY_ORGANISATION_NAME": "Gemeente Epe",
"GATSBY_JUMBOTRON_IMAGE_URL": "https://www.epe.nl/sites/default/files/styles/hero_medium/public/2023-09/Koeien.jpg?h=18223889&itok=BSberuIC",
"GATSBY_FOOTER_LOGO_HREF": "https://www.epe.nl/",
"GATSBY_FOOTER_CONTENT": "https://raw.githubusercontent.com/ConductionNL/woo-website-epe/main/FooterContent.json",
"GATSBY_FOOTER_CONTENT_HEADER": "heading-3",
"GATSBY_OIDN_NUMBER": "00000001001104524000"
}
3 changes: 2 additions & 1 deletion pwa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["./src/**/*"]
}

0 comments on commit 0729655

Please sign in to comment.