2020-10-11 12:13:35 +02:00
|
|
|
<template>
|
|
|
|
<div class="api-config">
|
|
|
|
<div v-if="configureApi">
|
|
|
|
<label class="label" for="api-url">Vikunja URL</label>
|
|
|
|
<div class="field has-addons">
|
|
|
|
<div class="control is-expanded">
|
|
|
|
<input
|
2021-01-17 18:57:57 +01:00
|
|
|
class="input"
|
|
|
|
id="api-url"
|
2020-10-11 12:13:35 +02:00
|
|
|
placeholder="eg. https://localhost:3456"
|
|
|
|
required
|
|
|
|
type="url"
|
|
|
|
v-focus
|
|
|
|
v-model="apiUrl"
|
|
|
|
@keyup.enter="setApiUrl"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="control">
|
2021-01-17 18:57:57 +01:00
|
|
|
<x-button @click="setApiUrl" :disabled="apiUrl === ''">
|
2020-10-11 12:13:35 +02:00
|
|
|
Change
|
2021-01-17 18:57:57 +01:00
|
|
|
</x-button>
|
2020-10-11 12:13:35 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="api-url-info" v-else>
|
2021-01-17 18:57:57 +01:00
|
|
|
Sign in to your Vikunja account on
|
|
|
|
<span v-tooltip="apiUrl"> {{ apiDomain() }} </span>
|
|
|
|
<br />
|
|
|
|
<a @click="() => (configureApi = true)">change</a>
|
2020-10-11 12:13:35 +02:00
|
|
|
</div>
|
|
|
|
|
2021-01-17 18:57:57 +01:00
|
|
|
<div
|
|
|
|
class="notification is-success mt-2"
|
|
|
|
v-if="successMsg !== '' && errorMsg === ''"
|
|
|
|
>
|
2020-10-11 12:13:35 +02:00
|
|
|
{{ successMsg }}
|
|
|
|
</div>
|
2021-01-17 18:57:57 +01:00
|
|
|
<div
|
|
|
|
class="notification is-danger mt-2"
|
|
|
|
v-if="errorMsg !== '' && successMsg === ''"
|
|
|
|
>
|
2020-10-11 12:13:35 +02:00
|
|
|
{{ errorMsg }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
name: 'apiConfig',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
configureApi: false,
|
|
|
|
apiUrl: '',
|
|
|
|
errorMsg: '',
|
|
|
|
successMsg: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.apiUrl = window.API_URL
|
|
|
|
if (this.apiUrl === '') {
|
|
|
|
this.configureApi = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
apiDomain() {
|
|
|
|
if (window.API_URL.startsWith('/api/v1')) {
|
|
|
|
return window.location.host
|
|
|
|
}
|
2021-01-17 18:57:57 +01:00
|
|
|
const urlParts = window.API_URL.replace('http://', '')
|
|
|
|
.replace('https://', '')
|
|
|
|
.split(/[/?#]/)
|
2020-10-11 12:13:35 +02:00
|
|
|
return urlParts[0]
|
|
|
|
},
|
|
|
|
setApiUrl() {
|
|
|
|
if (this.apiUrl === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let urlToCheck = this.apiUrl
|
|
|
|
|
|
|
|
// Check if the url has an http prefix
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.startsWith('http://') &&
|
|
|
|
!urlToCheck.startsWith('https://')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck = `http://${urlToCheck}`
|
|
|
|
}
|
|
|
|
|
|
|
|
urlToCheck = new URL(urlToCheck)
|
|
|
|
const origUrlToCheck = urlToCheck
|
|
|
|
|
|
|
|
const oldUrl = window.API_URL
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
|
|
|
|
// Check if the api is reachable at the provided url
|
2021-01-17 18:57:57 +01:00
|
|
|
this.$store
|
|
|
|
.dispatch('config/update')
|
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at /api/v1 and http
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it has a port and if not check if it is reachable at https
|
|
|
|
if (urlToCheck.protocol === 'http:') {
|
|
|
|
urlToCheck.protocol = 'https:'
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at /api/v1 and https
|
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at port 3456 and https
|
|
|
|
if (urlToCheck.port !== 3456) {
|
|
|
|
urlToCheck.protocol = 'https:'
|
|
|
|
urlToCheck.port = 3456
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at :3456 and /api/v1 and https
|
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at port 3456 and http
|
|
|
|
if (urlToCheck.port !== 3456) {
|
|
|
|
urlToCheck.protocol = 'http:'
|
|
|
|
urlToCheck.port = 3456
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.catch((e) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
// Check if it is reachable at :3456 and /api/v1 and http
|
|
|
|
urlToCheck.pathname = origUrlToCheck.pathname
|
2021-01-17 18:57:57 +01:00
|
|
|
if (
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1') &&
|
|
|
|
!urlToCheck.pathname.endsWith('/api/v1/')
|
|
|
|
) {
|
2020-10-11 12:13:35 +02:00
|
|
|
urlToCheck.pathname = `${urlToCheck.pathname}api/v1`
|
|
|
|
window.API_URL = urlToCheck.toString()
|
|
|
|
return this.$store.dispatch('config/update')
|
|
|
|
}
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// Still not found, url is still invalid
|
|
|
|
this.successMsg = ''
|
|
|
|
this.errorMsg = `Could not find or use Vikunja installation at "${this.apiDomain()}".`
|
|
|
|
window.API_URL = oldUrl
|
|
|
|
})
|
2021-01-17 18:57:57 +01:00
|
|
|
.then((r) => {
|
2020-10-11 12:13:35 +02:00
|
|
|
if (typeof r !== 'undefined') {
|
|
|
|
// Set it + save it to local storage to save us the hoops
|
|
|
|
this.errorMsg = ''
|
|
|
|
this.successMsg = `Using Vikunja installation at "${this.apiDomain()}".`
|
|
|
|
localStorage.setItem('API_URL', window.API_URL)
|
|
|
|
this.configureApi = false
|
|
|
|
this.apiUrl = window.API_URL
|
2021-04-21 09:44:40 +02:00
|
|
|
this.$emit('foundApi', this.apiUrl)
|
2020-10-11 12:13:35 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|