Skip to content

Commit

Permalink
better handle urls input into the server form: guess automatically /a…
Browse files Browse the repository at this point in the history
…pi/ variant if regular web url used + try to fix some cases of bad home urls

(closes #210)
  • Loading branch information
boogheta committed Jan 14, 2022
1 parent 708209e commit f12dcdb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
16 changes: 11 additions & 5 deletions app/actions/corpora.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down
25 changes: 22 additions & 3 deletions app/containers/Login/ServerForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}
})
}

Expand All @@ -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
}
Expand Down

0 comments on commit f12dcdb

Please sign in to comment.