Switch api location based on development or production environment

This commit is contained in:
Miroslav Vasilev 2023-10-26 21:14:15 +03:00
parent 3b94fa4ea8
commit 92641274c3
3 changed files with 12 additions and 4 deletions

View file

@ -27,4 +27,12 @@ function Layout({children}) {
);
}
export function apiLocation() {
if (process.env.NODE_ENV === "development") {
return "http://localhost:8000";
}
return window.location.origin;
}
export default Layout;

View file

@ -1,5 +1,5 @@
import React, { useState } from "react";
import Layout from "../Layout";
import Layout, { apiLocation } from "../Layout";
import Topbar from "../components/Topbar";
import MonacoEditor from 'react-monaco-editor';
import fetchEditorThemes from '../EditorThemes';
@ -62,7 +62,7 @@ export default function HomePage() {
return;
}
let promise = fetch(`${window.location.origin}/api/paste`, {
let promise = fetch(`${apiLocation()}/api/paste`, {
method: "POST",
body: JSON.stringify(paste)
}).then((response) => {

View file

@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react";
import Layout from "../Layout";
import Layout, { apiLocation } from "../Layout";
import Topbar from "../components/Topbar";
import { useParams } from "react-router-dom";
import MonacoEditor from 'react-monaco-editor';
@ -18,7 +18,7 @@ export default function PastePage() {
let [ editor, setEditor ] = useState({});
useEffect(() => {
fetch(`${window.location.origin}/api/paste/${pasteId}`)
fetch(`${apiLocation()}/api/paste/${pasteId}`)
.then(response => {
if (response.ok) {
return response.json()