From 92641274c380b1bd8daff2fde81f87e348f126eb Mon Sep 17 00:00:00 2001 From: mvvasilev Date: Thu, 26 Oct 2023 21:14:15 +0300 Subject: [PATCH] Switch api location based on development or production environment --- paste-eater-frontend/src/Layout.js | 8 ++++++++ paste-eater-frontend/src/pages/Home.js | 4 ++-- paste-eater-frontend/src/pages/Paste.js | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/paste-eater-frontend/src/Layout.js b/paste-eater-frontend/src/Layout.js index 27c68a6..aa3ff8e 100644 --- a/paste-eater-frontend/src/Layout.js +++ b/paste-eater-frontend/src/Layout.js @@ -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; \ No newline at end of file diff --git a/paste-eater-frontend/src/pages/Home.js b/paste-eater-frontend/src/pages/Home.js index f9ec27c..c377f06 100644 --- a/paste-eater-frontend/src/pages/Home.js +++ b/paste-eater-frontend/src/pages/Home.js @@ -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) => { diff --git a/paste-eater-frontend/src/pages/Paste.js b/paste-eater-frontend/src/pages/Paste.js index 4a01b2e..ea80de0 100644 --- a/paste-eater-frontend/src/pages/Paste.js +++ b/paste-eater-frontend/src/pages/Paste.js @@ -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()