diff --git a/app/actions/corpora.js b/app/actions/corpora.js index 00331fc..9433fe3 100644 --- a/app/actions/corpora.js +++ b/app/actions/corpora.js @@ -46,11 +46,17 @@ export const fetchCorpora = (serverUrl) => (dispatch) => { dispatch(requestCorpora(serverUrl)) return jsonrpc(serverUrl)('list_corpus') - .then((res) => dispatch(receiveCorpora(serverUrl, res))) - .catch((error) => dispatch({ - type: FETCH_CORPORA_FAILURE, - payload: { error, serverUrl } - })) + .then((res) => { + console.log("SUCCESS", res); + return dispatch(receiveCorpora(serverUrl, res)) + }) + .catch((error) => { + console.log("ERROR", res); + return dispatch({ + type: FETCH_CORPORA_FAILURE, + payload: { error, serverUrl } + }) + }) } export const requestServerStatus = createAction(FETCH_SERVER_STATUS_REQUEST, (serverUrl) => ({ serverUrl })) diff --git a/app/containers/Login/ServerForm.js b/app/containers/Login/ServerForm.js index f419611..baccccb 100644 --- a/app/containers/Login/ServerForm.js +++ b/app/containers/Login/ServerForm.js @@ -130,8 +130,27 @@ const ServerForm = ({ setErrors([]) saveAndRedirect() }, () => { - setSubmitting(false) - setErrors(['error.server-url']) + // Try alternative url ending with /api/ if missing and fix it automatically if working + const altUrl = server.url.replace(/(\/|api)*$/, '/api/') + console.log("API server url", server.url, "seems unaccessible, trying to rewrite it:", altUrl) + if (! /\/api\/$/.test(server.url)) { + fetchCorpora(altUrl) + .then(() => { + if (fullConfig && typeof fullConfig === 'object') { + fullConfig.url = altUrl + } + data.url = altUrl + setSubmitting(true) + setErrors([]) + saveAndRedirect() + }, () => { + setSubmitting(false) + setErrors(['error.server-url']) + }) + } else { + setSubmitting(false) + setErrors(['error.server-url']) + } }) } @@ -151,7 +170,7 @@ const ServerForm = ({ { ...data } if (!server.password) delete server.password - if (!server.home) server.home = server.url.replace(/[/-]api\/?$/, '') + server.home = server.url.replace(/[/-]+api\/+?$/, '/') return server }