From 2b467b8452f9e5d50d392d0efe136557aba544f5 Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Sat, 13 Mar 2021 05:21:09 -0500 Subject: [PATCH] fix(web): api context should pass it's current hostname into API URL selector --- packages/web/src/api-context/ApiContext.tsx | 6 ++-- .../web/src/api-context/getDefaultApiUrl.ts | 28 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/web/src/api-context/ApiContext.tsx b/packages/web/src/api-context/ApiContext.tsx index 439f9ab..192b7df 100644 --- a/packages/web/src/api-context/ApiContext.tsx +++ b/packages/web/src/api-context/ApiContext.tsx @@ -8,7 +8,7 @@ type ApiContextData = { }; export const ApiContext = React.createContext({ - apiUrl: getDefaultApiUrl(), + apiUrl: getDefaultApiUrl(window.location.hostname), setApiUrl: () => {}, fetch: async () => { return new Response(); @@ -18,7 +18,9 @@ export const ApiContext = React.createContext({ export const useApiContext = () => React.useContext(ApiContext); export const ApiContextProvider = (props: { children: React.ReactNode }) => { - const [apiUrl, setApiUrl] = React.useState(getDefaultApiUrl()); + const [apiUrl, setApiUrl] = React.useState( + getDefaultApiUrl(window.location.hostname) + ); const apiContextData: ApiContextData = { apiUrl, diff --git a/packages/web/src/api-context/getDefaultApiUrl.ts b/packages/web/src/api-context/getDefaultApiUrl.ts index 3ba10e6..b17e62c 100644 --- a/packages/web/src/api-context/getDefaultApiUrl.ts +++ b/packages/web/src/api-context/getDefaultApiUrl.ts @@ -1,18 +1,16 @@ import * as memoizeOne from 'memoize-one'; -export const getDefaultApiUrl = memoizeOne.default( - (host: string = window.location.hostname) => { - if (/^roleypoly.com$/.test(host)) { - return 'https://api-prod.roleypoly.com'; - } else if ( - /roleypoly\.pages\.dev$/.test(host) || - /^stage.roleypoly.com$/.test(host) - ) { - return 'https://api-stage.roleypoly.com'; - } else if (/\blocalhost|127\.0\.0\.1\b/.test(host)) { - return 'http://localhost:6609'; - } else { - return 'https://api-prod.roleypoly.com'; - } +export const getDefaultApiUrl = memoizeOne.default((host: string) => { + if (/^roleypoly.com$/.test(host)) { + return 'https://api-prod.roleypoly.com'; + } else if ( + /roleypoly\.pages\.dev$/.test(host) || + /^stage.roleypoly.com$/.test(host) + ) { + return 'https://api-stage.roleypoly.com'; + } else if (/\blocalhost|127\.0\.0\.1\b/.test(host)) { + return 'http://localhost:6609'; + } else { + return 'https://api-prod.roleypoly.com'; } -); +});