From 9ca8cb08d662361187245d8a5e23a93dd02f6623 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Fri, 10 Nov 2023 19:31:32 +0100 Subject: [PATCH 1/2] Add support for BASE_URL to the management react interface (#815) --- rdmo/core/assets/js/api/BaseApi.js | 12 +++++++----- rdmo/core/assets/js/utils/baseUrl.js | 2 ++ rdmo/management/assets/js/reducers/configReducer.js | 4 +++- 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 rdmo/core/assets/js/utils/baseUrl.js diff --git a/rdmo/core/assets/js/api/BaseApi.js b/rdmo/core/assets/js/api/BaseApi.js index 0f12feacb9..c2d81e9473 100644 --- a/rdmo/core/assets/js/api/BaseApi.js +++ b/rdmo/core/assets/js/api/BaseApi.js @@ -1,6 +1,8 @@ import Cookies from 'js-cookie' import isUndefined from 'lodash/isUndefined' +import baseUrl from '../utils/baseUrl' + function ApiError(statusText, status) { this.status = status this.statusText = statusText @@ -16,7 +18,7 @@ function ValidationError(errors) { class BaseApi { static get(url) { - return fetch(url).catch(error => { + return fetch(baseUrl + url).catch(error => { throw new ApiError(error.message) }).then(response => { if (response.ok) { @@ -28,7 +30,7 @@ class BaseApi { } static post(url, data) { - return fetch(url, { + return fetch(baseUrl + url, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -51,7 +53,7 @@ class BaseApi { } static put(url, data) { - return fetch(url, { + return fetch(baseUrl + url, { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -74,7 +76,7 @@ class BaseApi { } static delete(url) { - return fetch(url, { + return fetch(baseUrl + url, { method: 'DELETE', headers: { 'Content-Type': 'application/json', @@ -99,7 +101,7 @@ class BaseApi { var formData = new FormData() formData.append('file', file) - return fetch(url, { + return fetch(baseUrl + url, { method: 'POST', headers: { 'X-CSRFToken': Cookies.get('csrftoken') diff --git a/rdmo/core/assets/js/utils/baseUrl.js b/rdmo/core/assets/js/utils/baseUrl.js new file mode 100644 index 0000000000..22a17df960 --- /dev/null +++ b/rdmo/core/assets/js/utils/baseUrl.js @@ -0,0 +1,2 @@ +// take the baseurl from the of the django template +export default document.querySelector('meta[name="baseurl"]').content.replace(/\/+$/, '') diff --git a/rdmo/management/assets/js/reducers/configReducer.js b/rdmo/management/assets/js/reducers/configReducer.js index 12f8196864..74d26091d5 100644 --- a/rdmo/management/assets/js/reducers/configReducer.js +++ b/rdmo/management/assets/js/reducers/configReducer.js @@ -1,7 +1,9 @@ import set from 'lodash/set' +import rdmoBaseUrl from 'rdmo/core/assets/js/utils/baseUrl' + const initialState = { - baseUrl: '/management/', + baseUrl: rdmoBaseUrl + '/management/', settings: {}, filter: {}, display: {} From bbccddfa66ec27f02c9eacabb2d19ac28a3d2506 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Thu, 16 Nov 2023 16:29:26 +0100 Subject: [PATCH 2/2] Rename variable --- rdmo/management/assets/js/reducers/configReducer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rdmo/management/assets/js/reducers/configReducer.js b/rdmo/management/assets/js/reducers/configReducer.js index 74d26091d5..4eb2500380 100644 --- a/rdmo/management/assets/js/reducers/configReducer.js +++ b/rdmo/management/assets/js/reducers/configReducer.js @@ -1,9 +1,9 @@ import set from 'lodash/set' -import rdmoBaseUrl from 'rdmo/core/assets/js/utils/baseUrl' +import baseUrl from 'rdmo/core/assets/js/utils/baseUrl' const initialState = { - baseUrl: rdmoBaseUrl + '/management/', + baseUrl: baseUrl + '/management/', settings: {}, filter: {}, display: {}