From ed987f542626c0d7b7c5cb4ef18beeeace67885f Mon Sep 17 00:00:00 2001 From: vantezzen Date: Fri, 23 Aug 2019 12:57:46 +0200 Subject: Use API URL from store for workspaces and announcements --- src/features/announcements/api.js | 4 ++-- src/features/apiBase.js | 24 ++++++++++++++++++++++++ src/features/workspaces/api.js | 10 +++++----- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/features/apiBase.js (limited to 'src') diff --git a/src/features/announcements/api.js b/src/features/announcements/api.js index a581bd8de..0741c0f87 100644 --- a/src/features/announcements/api.js +++ b/src/features/announcements/api.js @@ -1,6 +1,6 @@ import { remote } from 'electron'; import Request from '../../stores/lib/Request'; -import { API, API_VERSION } from '../../environment'; +import apiBase from '../apiBase' const debug = require('debug')('Franz:feature:announcements:api'); @@ -21,7 +21,7 @@ export const announcementsApi = { async getAnnouncement(version) { debug('fetching release announcement from api'); - const url = `${API}/${API_VERSION}/announcements/${version}`; + const url = `${apiBase()}/announcements/${version}`; const response = await window.fetch(url, { method: 'GET' }); if (!response.ok) return null; return response.json(); diff --git a/src/features/apiBase.js b/src/features/apiBase.js new file mode 100644 index 000000000..961414668 --- /dev/null +++ b/src/features/apiBase.js @@ -0,0 +1,24 @@ +/** + * Get API base URL from store + */ +import { + API_VERSION +} from '../environment'; + +const apiBase = () => { + let url; + if (!window.ferdi.stores.settings) { + // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded + url = 'https://localhost:9999'; + } else if (window.ferdi.stores.settings.all.app.server) { + // Load URL from store + url = window.ferdi.stores.settings.all.app.server; + } else { + // Use default server url + url = SERVER_URL; + } + + return `${url}/${API_VERSION}`; +} + +export default apiBase \ No newline at end of file diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js index 0ec20c9ea..ff0e54a7a 100644 --- a/src/features/workspaces/api.js +++ b/src/features/workspaces/api.js @@ -1,14 +1,14 @@ import { pick } from 'lodash'; import { sendAuthRequest } from '../../api/utils/auth'; -import { API, API_VERSION } from '../../environment'; import Request from '../../stores/lib/Request'; import Workspace from './models/Workspace'; +import apiBase from '../apiBase' const debug = require('debug')('Franz:feature:workspaces:api'); export const workspaceApi = { getUserWorkspaces: async () => { - const url = `${API}/${API_VERSION}/workspace`; + const url = `${apiBase()}/workspace`; debug('getUserWorkspaces GET', url); const result = await sendAuthRequest(url, { method: 'GET' }); debug('getUserWorkspaces RESULT', result); @@ -18,7 +18,7 @@ export const workspaceApi = { }, createWorkspace: async (name) => { - const url = `${API}/${API_VERSION}/workspace`; + const url = `${apiBase()}/workspace`; const options = { method: 'POST', body: JSON.stringify({ name }), @@ -31,7 +31,7 @@ export const workspaceApi = { }, deleteWorkspace: async (workspace) => { - const url = `${API}/${API_VERSION}/workspace/${workspace.id}`; + const url = `${apiBase()}/workspace/${workspace.id}`; debug('deleteWorkspace DELETE', url); const result = await sendAuthRequest(url, { method: 'DELETE' }); debug('deleteWorkspace RESULT', result); @@ -40,7 +40,7 @@ export const workspaceApi = { }, updateWorkspace: async (workspace) => { - const url = `${API}/${API_VERSION}/workspace/${workspace.id}`; + const url = `${apiBase()}/workspace/${workspace.id}`; const options = { method: 'PUT', body: JSON.stringify(pick(workspace, ['name', 'services'])), -- cgit v1.2.3-70-g09d2