From 597ded15e27e56973e2a052f5ee38dc5280ccbe4 Mon Sep 17 00:00:00 2001 From: lindtvedtsebastian <46536611+lindtvedtsebastian@users.noreply.github.com> Date: Tue, 28 May 2024 12:44:12 +0200 Subject: [PATCH] chore(refresh): common function for fetching backend url (#1526) --- .../[id]/components/RefreshButton/actions.ts | 18 ++++++++---------- next-tavla/pages/[id].tsx | 8 ++------ next-tavla/src/Shared/utils/index.ts | 5 +++++ 3 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 next-tavla/src/Shared/utils/index.ts diff --git a/next-tavla/app/(admin)/edit/[id]/components/RefreshButton/actions.ts b/next-tavla/app/(admin)/edit/[id]/components/RefreshButton/actions.ts index 81569ebba..f2e8c1be6 100644 --- a/next-tavla/app/(admin)/edit/[id]/components/RefreshButton/actions.ts +++ b/next-tavla/app/(admin)/edit/[id]/components/RefreshButton/actions.ts @@ -3,21 +3,19 @@ import { hasBoardEditorAccess } from 'app/(admin)/utils/firebase' import { redirect } from 'next/navigation' import { TBoard } from 'types/settings' +import { getBackendUrl } from 'utils/index' export async function refreshBoard(board: TBoard) { const access = await hasBoardEditorAccess(board.id) if (!access) return redirect('/') - const res = await fetch( - `${process.env.BACKEND_SERVICE_HOST}/refresh/${board.id}`, - { - method: 'POST', - body: JSON.stringify(board), - headers: { - Authorization: `Bearer ${process.env.BACKEND_API_KEY}`, - 'Content-Type': 'application/json', - }, + const res = await fetch(`${getBackendUrl()}/refresh/${board.id}`, { + method: 'POST', + body: JSON.stringify(board), + headers: { + Authorization: `Bearer ${process.env.BACKEND_API_KEY}`, + 'Content-Type': 'application/json', }, - ) + }) return res.ok } diff --git a/next-tavla/pages/[id].tsx b/next-tavla/pages/[id].tsx index 5d7a9e786..45150b69a 100644 --- a/next-tavla/pages/[id].tsx +++ b/next-tavla/pages/[id].tsx @@ -7,6 +7,7 @@ import { } from 'Board/scenarios/Board/firebase' import { Footer } from 'components/Footer' import { useRefresh } from 'hooks/useRefresh' +import { getBackendUrl } from 'utils/index' export async function getServerSideProps({ params, @@ -23,16 +24,11 @@ export async function getServerSideProps({ } } - const backend_url = - process.env.KUB_ENV === 'prd' - ? 'https://tavla-api.entur.no' - : 'https://tavla-api.dev.entur.no' - return { props: { board, organization: await getOrganizationWithBoard(id), - backend_url, + backend_url: getBackendUrl(), }, } } diff --git a/next-tavla/src/Shared/utils/index.ts b/next-tavla/src/Shared/utils/index.ts new file mode 100644 index 000000000..2702106cf --- /dev/null +++ b/next-tavla/src/Shared/utils/index.ts @@ -0,0 +1,5 @@ +export function getBackendUrl() { + return process.env.KUB_ENV === 'prd' + ? 'https://tavla-api.entur.no' + : 'https://tavla-api.dev.entur.no' +}