diff --git a/frontend/src/services/http-client/http-client.js b/frontend/src/services/http-client/http-client.js index 4ea725d2f..a427a46e3 100644 --- a/frontend/src/services/http-client/http-client.js +++ b/frontend/src/services/http-client/http-client.js @@ -15,6 +15,27 @@ export class HttpClient { } } + _baseUrl; + + setBaseUrl(baseUrl) { + if (typeof this._baseUrl !== 'undefined') { + throw new Error('HttpClient baseUrl is already set'); + } + + this._baseUrl = baseUrl; + } + + _defaultUrl; + + setDefaultUrl(defaultUrl) { + if (typeof this._defaultUrl !== 'undefined') { + throw new Error('HttpClient defaultUrl is already set'); + } + + this._defaultUrl = defaultUrl; + } + + get(args) { args.method = 'GET'; return this._fetch(args); @@ -28,12 +49,17 @@ export class HttpClient { } _fetch(options) { + options.url = options.url || this._defaultUrl; + + if (this._baseUrl && options.url && options.url.substring(0,1) === '/' && options.url.substring(0,2) !== '//') + options.url = this._baseUrl + (this._baseUrl.substring(this._baseUrl.substring.length - 1) === '/' ? '' : '/') + options.url.substring(1,0); + const requestOptions = { credentials: 'same-origin', ...options, }; - return fetch(options.url, requestOptions) + return fetch(options.url || window.location.href, requestOptions) .then( (response) => { this._responseInterceptors.forEach((interceptor) => interceptor(response, options)); diff --git a/frontend/src/utils/exam-correct/exam-correct.js b/frontend/src/utils/exam-correct/exam-correct.js index 80b0592d7..edee3bc47 100644 --- a/frontend/src/utils/exam-correct/exam-correct.js +++ b/frontend/src/utils/exam-correct/exam-correct.js @@ -7,7 +7,6 @@ import moment from 'moment'; import './exam-correct.sass'; -const EXAM_CORRECT_URL_POST = 'correct'; const EXAM_CORRECT_HEADERS = { 'Content-Type': HttpClient.ACCEPT.JSON, 'Accept': HttpClient.ACCEPT.JSON, @@ -198,7 +197,6 @@ export class ExamCorrect { const body = this._toRequestBody(this._userInput.value); this._app.httpClient.post({ - url: EXAM_CORRECT_URL_POST, headers: EXAM_CORRECT_HEADERS, body: JSON.stringify(body), }).then( @@ -290,7 +288,6 @@ export class ExamCorrect { const body = this._toRequestBody(userId || user, results, result); this._app.httpClient.post({ - url: EXAM_CORRECT_URL_POST, headers: EXAM_CORRECT_HEADERS, body: JSON.stringify(body), }).then( @@ -522,7 +519,6 @@ export class ExamCorrect { const body = this._toRequestBody(listItem.getAttribute(EXAM_CORRECT_USER_ATTR), results.partResults, results.result); this._app.httpClient.post({ - url: EXAM_CORRECT_URL_POST, headers: EXAM_CORRECT_HEADERS, body: JSON.stringify(body), }).then( diff --git a/src/Foundation.hs b/src/Foundation.hs index 52fbda5c0..7297a70be 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -1830,6 +1830,8 @@ siteLayout' headingOverride widget = do mcurrentRoute <- getCurrentRoute let currentHandler = classifyHandler <$> mcurrentRoute + currentApproot' <- siteApproot <$> getYesod <*> (reqWaiRequest <$> getRequest) + -- Get the breadcrumbs, as defined in the YesodBreadcrumbs instance. let breadcrumbs' mcRoute = do @@ -2095,6 +2097,11 @@ siteLayout' headingOverride widget = do pc <- widgetToPageContent $ do webpackLinks_main StaticR toWidget $(juliusFile "templates/i18n.julius") + whenIsJust currentApproot' $ \currentApproot -> + toWidget $(juliusFile "templates/approot.julius") + whenIsJust mcurrentRoute $ \currentRoute' -> do + currentRoute <- toTextUrl currentRoute' + toWidget $(juliusFile "templates/current-route.julius") wellKnownHtmlLinks $(widgetFile "default-layout") diff --git a/templates/approot.julius b/templates/approot.julius new file mode 100644 index 000000000..d18fc41ec --- /dev/null +++ b/templates/approot.julius @@ -0,0 +1,5 @@ +if (window.App && window.App.httpClient) { + window.App.httpClient.setBaseUrl(#{currentApproot}); +} else { + throw new Error('HttpClient service is missing!'); +} diff --git a/templates/current-route.julius b/templates/current-route.julius new file mode 100644 index 000000000..5a5ee6632 --- /dev/null +++ b/templates/current-route.julius @@ -0,0 +1,5 @@ +if (window.App && window.App.httpClient) { + window.App.httpClient.setDefaultUrl(#{currentRoute}); +} else { + throw new Error('HttpClient service is missing!'); +} diff --git a/templates/i18n.julius b/templates/i18n.julius index 690f8da89..7b523c9e9 100644 --- a/templates/i18n.julius +++ b/templates/i18n.julius @@ -1,6 +1,6 @@ -if (window.App) { +if (window.App && window.App.i18n) { window.App.i18n.addMany(#{frontendI18n}); window.App.i18n.setDatetimeLocale(#{frontendDatetimeLocale}); } else { - throw new Error('I18n JavaScript service is missing!'); + throw new Error('I18n service is missing!'); }