2020-10-11 12:13:35 +02:00
|
|
|
<template>
|
|
|
|
<div class="api-config">
|
|
|
|
<div v-if="configureApi">
|
2021-06-24 01:24:57 +02:00
|
|
|
<label class="label" for="api-url">{{ $t('apiConfig.url') }}</label>
|
2020-10-11 12:13:35 +02:00
|
|
|
<div class="field has-addons">
|
|
|
|
<div class="control is-expanded">
|
|
|
|
<input
|
2021-01-17 18:57:57 +01:00
|
|
|
class="input"
|
|
|
|
id="api-url"
|
2021-06-24 01:24:57 +02:00
|
|
|
:placeholder="$t('apiConfig.urlPlaceholder')"
|
2020-10-11 12:13:35 +02:00
|
|
|
required
|
|
|
|
type="url"
|
|
|
|
v-focus
|
|
|
|
v-model="apiUrl"
|
|
|
|
@keyup.enter="setApiUrl"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="control">
|
2021-10-17 18:52:05 +02:00
|
|
|
<x-button @click="setApiUrl" :disabled="apiUrl === '' || null">
|
2021-06-24 01:24:57 +02:00
|
|
|
{{ $t('apiConfig.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-08-20 15:38:16 +02:00
|
|
|
<i18n-t keypath="apiConfig.signInOn">
|
2021-09-08 23:48:30 +02:00
|
|
|
<span class="url" v-tooltip="apiUrl"> {{ apiDomain }} </span>
|
2021-08-20 15:38:16 +02:00
|
|
|
</i18n-t>
|
2021-11-13 20:49:02 +01:00
|
|
|
<br/>
|
2021-06-24 01:24:57 +02:00
|
|
|
<a @click="() => (configureApi = true)">{{ $t('apiConfig.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>
|
2021-11-13 20:49:02 +01:00
|
|
|
import {parseURL} from 'ufo'
|
|
|
|
import {checkAndSetApiUrl} from '@/helpers/checkAndSetApiUrl'
|
2021-09-08 23:48:30 +02:00
|
|
|
|
2020-10-11 12:13:35 +02:00
|
|
|
export default {
|
|
|
|
name: 'apiConfig',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
configureApi: false,
|
2021-09-08 23:48:30 +02:00
|
|
|
apiUrl: window.API_URL,
|
2020-10-11 12:13:35 +02:00
|
|
|
errorMsg: '',
|
|
|
|
successMsg: '',
|
|
|
|
}
|
|
|
|
},
|
2021-08-23 21:18:12 +02:00
|
|
|
emits: ['foundApi'],
|
2020-10-11 12:13:35 +02:00
|
|
|
created() {
|
|
|
|
if (this.apiUrl === '') {
|
|
|
|
this.configureApi = true
|
|
|
|
}
|
|
|
|
},
|
2021-09-08 23:48:30 +02:00
|
|
|
computed: {
|
2020-10-11 12:13:35 +02:00
|
|
|
apiDomain() {
|
2021-11-14 17:16:05 +01:00
|
|
|
return parseURL(this.apiUrl).host || parseURL(window.location.href).host
|
2020-10-11 12:13:35 +02:00
|
|
|
},
|
2021-09-08 23:48:30 +02:00
|
|
|
},
|
2021-11-13 20:49:02 +01:00
|
|
|
props: {
|
|
|
|
configureOpen: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
configureOpen: {
|
|
|
|
handler(value) {
|
|
|
|
this.configureApi = value
|
|
|
|
},
|
|
|
|
immediate: true,
|
|
|
|
},
|
|
|
|
},
|
2021-09-08 23:48:30 +02:00
|
|
|
methods: {
|
2021-11-13 20:49:02 +01:00
|
|
|
async setApiUrl() {
|
2020-10-11 12:13:35 +02:00
|
|
|
if (this.apiUrl === '') {
|
2021-11-13 20:49:02 +01:00
|
|
|
// Don't try to check and set an empty url
|
|
|
|
this.errorMsg = this.$t('apiConfig.urlRequired')
|
2020-10-11 12:13:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 20:49:02 +01:00
|
|
|
try {
|
|
|
|
const url = await checkAndSetApiUrl(this.apiUrl)
|
2020-10-11 12:13:35 +02:00
|
|
|
|
2021-11-13 20:49:02 +01:00
|
|
|
if (url === '') {
|
|
|
|
// If the config setter function could not figure out a url
|
|
|
|
throw new Error('URL cannot be empty.')
|
|
|
|
}
|
2020-10-11 12:13:35 +02:00
|
|
|
|
2021-11-13 20:49:02 +01:00
|
|
|
// Set it + save it to local storage to save us the hoops
|
|
|
|
this.errorMsg = ''
|
|
|
|
this.successMsg = this.$t('apiConfig.success', {domain: this.apiDomain})
|
|
|
|
this.configureApi = false
|
|
|
|
this.apiUrl = url
|
|
|
|
this.$emit('foundApi', this.apiUrl)
|
|
|
|
} catch (e) {
|
|
|
|
// Still not found, url is still invalid
|
|
|
|
this.successMsg = ''
|
|
|
|
this.errorMsg = this.$t('apiConfig.error', {domain: this.apiDomain})
|
|
|
|
}
|
2020-10-11 12:13:35 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|
2021-10-18 14:19:45 +02:00
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.api-config {
|
2021-11-13 20:49:02 +01:00
|
|
|
margin-bottom: .75rem;
|
2021-10-18 14:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.api-url-info {
|
2021-11-13 20:49:02 +01:00
|
|
|
font-size: .9rem;
|
|
|
|
text-align: right;
|
|
|
|
}
|
2021-10-18 14:19:45 +02:00
|
|
|
|
2021-11-13 20:49:02 +01:00
|
|
|
.url {
|
|
|
|
border-bottom: 1px dashed $primary;
|
2021-10-18 14:19:45 +02:00
|
|
|
}
|
|
|
|
</style>
|