fix(web): api context should pass it's current hostname into API URL selector

This commit is contained in:
41666 2021-03-13 05:21:09 -05:00
parent eb2537ae35
commit 2b467b8452
2 changed files with 17 additions and 17 deletions

View file

@ -8,7 +8,7 @@ type ApiContextData = {
};
export const ApiContext = React.createContext<ApiContextData>({
apiUrl: getDefaultApiUrl(),
apiUrl: getDefaultApiUrl(window.location.hostname),
setApiUrl: () => {},
fetch: async () => {
return new Response();
@ -18,7 +18,9 @@ export const ApiContext = React.createContext<ApiContextData>({
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,

View file

@ -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';
}
);
});